代码之家  ›  专栏  ›  技术社区  ›  user1305398

如何使用couchbase中的视图获取文档

  •  2
  • user1305398  · 技术社区  · 11 年前

    我有一个要求,我必须从沙发上拿到文件。

    下面是我正在使用的Map函数-

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

    没有减少功能。下面是我获取文档的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);
    

    我在沙发床上的数据是键-“122”和值-“true”。但由于某些原因,我在ViewResponse中没有得到任何行。出了什么问题,有人能帮忙吗?

    1 回复  |  直到 11 年前
        1
  •  2
  •   scalabilitysolved    11 年前

    我不明白你想在这里实现什么,你是在用视图来获取一个文档?键==122?为什么你就不能做客户端获取(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
    }
    

    您不需要检查行上的空值。

    推荐文章