4

我在couchbases的网站上使用了你的示例代码,我使用的是java,jdk版本是1.6。通过键设置和获取值是好的,但是在查询视图时,总是出现错误。

这是我的代码:

package src.main.java;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;
import com.couchbase.client.protocol.views.ViewRow;
import java.net.URI;
import java.util.Arrays;
import java.util.List;


    public class HelloWorld {
      public static void main(String[] args) throws Exception {
        // (Subset) of nodes in the cluster to establish a connection
        List<URI> hosts = Arrays.asList(
          new URI("http://192.168.174.128:8091/pools")
        );
        // Name of the Bucket to connect to
        String bucket = "default";
        // Password of the bucket (empty) string if none
        String password = "";
        // Connect to the Cluster
        CouchbaseClient client = new CouchbaseClient(hosts, bucket, password);

        // 1: Load the View infos
        String designDoc = "users";
        String viewName = "by_firstname";
        View view = client.getView(designDoc, viewName);
        // 2: Create a Query object to customize the Query
        Query query = new Query();
        query.setIncludeDocs(true); // Include the full document body

        // 3: Actually Query the View and return the results
        ViewResponse response = client.query(view, query);

        // 4: Iterate over the Data and print out the full document
        for (ViewRow row : response) {
          System.out.println(row.getDocument());
        }
        // Shutting down properly
        client.shutdown();
      }
    }

这是错误日志

2013-08-03 11:17:21.779 ERROR com.couchbase.client.ViewNode$EventLogger:  Connection timed out: [192.168.174.128/192.168.174.128:8092]
Exception in thread "main" java.lang.RuntimeException: Timed out waiting for operation
    at com.couchbase.client.internal.HttpFuture.get(HttpFuture.java:67)
    at com.couchbase.client.CouchbaseClient.getView(CouchbaseClient.java:483)
    at src.main.java.HelloWorld.main(HelloWorld.java:75)
Caused by: java.util.concurrent.TimeoutException: Timed out waiting for operation
    at com.couchbase.client.internal.HttpFuture.waitForAndCheckOperation(HttpFuture.java:85)
    at com.couchbase.client.internal.HttpFuture.get(HttpFuture.java:74)
    at com.couchbase.client.internal.HttpFuture.get(HttpFuture.java:64)
    ... 2 more

从 Web 管理控制台我可以看到我已经发布了视图。在那个控制台中也可以使用。防火墙已关闭,我尝试了 couchbase 2.0.0 社区版和 2.1.1 社区版。

4

1 回答 1

2

我最近遇到了同样的问题。该视图在管理控制台中运行良好,但客户端在调用 getView() 时超时。任何时候获取视图定义的 HTTP 请求超时或失败都可能发生这种情况,这可能/将发生在任何网络上。

getView() 只是获取视图的定义供您查询。根据官方 Couchbase 文档,您实际上应该在应用程序中缓存这些视图定义,而不是在每次需要运行查询时调用 getView()。

每次调用 CouchbaseClient 对象的 .getView(String, String) 方法时,都会向服务器发送一个 HTTP 请求。正如您在前面的部分中所看到的,View 对象只不过是视图的内容的表示。这些信息在生产中不会有太大变化。

作为最佳实践,只调用一次 getView,然后缓存返回的对象并为每个后续查询调用重用它。这样,您不仅可以节省带宽和延迟,还可以减少服务器需要做的工作量。

来源:http ://docs.couchbase.com/couchbase-sdk-java-1.4/#reusing-view-definitions

于 2015-07-01T20:16:00.113 回答