Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract, in a list, all keys from a bucket in couchbase

Tags:

java

couchbase

I am totally new in couchbase. I am using the java api and i want somehow to view all available keys in a bucket. Is this possible? Thanks in advance.

like image 746
stelios.p Avatar asked Jan 10 '14 15:01

stelios.p


1 Answers

It is possible but you will need to create a view to do this (secondary index).

You can create a view in the couchbase webconsole like so:

function (doc, meta) {
  if(meta.type == "json") {
     emit(null);
  }
}

This will emit all the keys (keys are automatically emitted anyway so no need to include anything extra).

Then you can query the view like below using the java sdk. (Obviously you need to instantiate the couchbase client etc)

View view = couchbaseClient.getView("DESIGN_VIEW NAME", "VIEW_NAME");
Query query = new Query();
ViewResponse viewResponse = couchbaseClient.query(view, query);

List<String> keys = new ArrayList<String>();
for (ViewRow viewRow : viewResponse) {
   keys.add(viewRow.getKey());
}
like image 112
scalabilitysolved Avatar answered Nov 14 '22 15:11

scalabilitysolved