Retrieving list of all keys from bucket ( Couchbase Java SDK 1.4)



Hi all,

I believe a similar question was posted before. But my question is specific to Java SDK version 1.4 as to whether there is a way (be it straightforward or a hack) to retrieve a list of all keys from a bucket. I see that couchbase provides N1QL queries in the higher SDKs for this purpose. But for various reasons, our team decided to continue using the older version for now. I need this feature to autosuggest a list of keys based on user’s keyword search via a REST api call. Any recommendation on how to achieve this with couchbase Java SDK 1.4?



You could create a view, which will emit all the keys, and iterate on this view

View view = client.getView("test", "my_all_keys");
Query query = new Query();
ViewResponse result = client.query(view, query);
for(ViewRow row : result) {


Thanks avsej. Your way works!! Also had a follow up question. In my project’s dev environment, I am able to create views from the admin console. But, I don’t have access to our couchbase admin console in our live environment. So, I have to figure out a way to create the map function and instance of DesignDocument from my Java application. But, I’m not sure if it’s efficient since I end up creating the view each time I restart the application server. Is there a more efficient/recommended way of creating a view?


You could first retrieve design document

DesignDocument ddoc = client.getDesignDocument("mydoc");

Then (assuming your design document lives in some meta info on the file system), you could calculate hash sum of the all map/reduce functions and while iterating the list, compare sums and re-create design documents only when they really different

StringBuilder sb = new StringBuilder();
for (ViewDesign view : ddoc.getViews()) {
// the same for ddoc.getSpatialViews()

// calculate hash sum of sb.toString();

if (/* calculated sum does not match sum from the jar resources */) {
    ddoc = /* load map/reduce function from resources and construct new DesignDocument */;