Pregunta

I have a requirement wherein I have get the document from couchbase.

Following in the Map function that I am using for the same -

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

There is no reduce function. Also following is my java code to get the document -

  List<URI> hosts = Arrays.asList(
            new URI("http://<some DNS with port>/pools")
    );

    // Name of the Bucket to connect to
    String bucket = "Test-Sessions";

    // Password of the bucket (empty) string if none
    String password = "";
    //System.setProperty("viewmode", "development");
    // Connect to the Cluster
    CouchbaseClient client = new CouchbaseClient(hosts, bucket, password);


    String designDoc = "sessions";
    String viewName = "by_test";
    View view = client.getView(designDoc, viewName);
    Query query = new Query();
    query.setIncludeDocs(true);
    query.setKey(String.valueOf(122));
    ViewResponse result = client.query(view, query);
    Object object = null;
    for(ViewRow row : result) {
        if(null != row) {
        object = row.getDocument();
        }// deal with the document/data
    }
    System.out.println("Object" + object);

And the data that I have in couchbase is key - "122" and value - "true". But for some reason , I do not get any rows in the ViewResponse. What is going wrong can anyone help?

¿Fue útil?

Solución

I don't understand what you are trying to achieve here, you are using a view to get a document by it's key? Key == 122? Why can't you just do client.get(122) ?

If you just need a list of all the keys in your bucket (of which you can use to pull back all documents via include docs) then make your function like so:

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

The key of the document is always emitted as an ID (viewRow.getId()). You don't need to emit the document, try to emit as little data as possible to keep view sizes small.

If you are needing to manipulate all the documents in your bucket be careful as the size grows, perhaps you'd need to look at pagination to cycle through the results. http://tugdualgrall.blogspot.com.es/

Also once you have the ViewResponse loop over it like so:

for(ViewRow row : result) {
  row.getDocument(); // deal with the document/data
}

You don't need to be doing checks for null on the rows.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top