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

I am brand new at couchbase. I am using java api and I want to somehow look through all available keys in a bucket. Is it possible? Thanks in advance.

+7
java-api couchbase
source share
1 answer

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

You can create a view in the couchbase web console as follows:

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

This will emit all the keys (the keys will automatically exit anyway, so there is no need to include anything extra).

You can then request a view, as shown below, using java sdk. (Obviously you need to instantiate a 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()); } 
+11
source share

All Articles