Reputation: 177
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.
Upvotes: 7
Views: 6238
Reputation: 2474
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());
}
Upvotes: 11