【问题标题】:How to get the document using view in couchbase如何使用 couchbase 中的视图获取文档
【发布时间】:2014-02-08 15:42:15
【问题描述】:

我有一个要求,我从 couchbase 获取文档。

在我用于相同的 Map 函数之后 -

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

没有reduce函数。以下是我获取文档的 java 代码 -

  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);

而我在 couchbase 中的数据是关键 - “122”和价值 - “真”。但由于某种原因,我在 ViewResponse 中没有得到任何行。有什么问题可以帮忙吗?

【问题讨论】:

    标签: couchbase couchbase-view


    【解决方案1】:

    我不明白你在这里想要达到什么目的,你正在使用视图通过它的键来获取文档?密钥 == 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 检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多