2

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?

4

1 回答 1

2

我不明白你想在这里实现什么,你正在使用视图通过它的键来获取文档?密钥 == 122?为什么你不能只做 client.get(122) ?

如果您只需要存储桶中所有键的列表(您可以使用其中的键通过 include docs 拉回所有文档),则使您的函数如下所示:

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

文档的键始终作为 ID (viewRow.getId()) 发出。您不需要发出文档,尽量发出尽可能少的数据以保持视图尺寸较小。

如果您需要操作存储桶中的所有文档,请注意随着大小的增加,您可能需要查看分页以循环查看结果。http://tugdualgrall.blogspot.com.es/

同样,一旦您对它进行了 ViewResponse 循环,如下所示:

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

您不需要对行进行 null 检查。

于 2014-02-08T19:48:22.127 回答