为什么我的couchbase视图没有返回数据?

时间:2015-07-31 05:07:23

标签: java couchbase couchbase-view

我已经在我的couchbase 4.0 B版本中为windows创建了示例视图。我也发表了我的观点。我通过java程序访问它,但没有得到任何结果,而是用json得到一些错误。

以下是我正在做的完整代码。

创建视图:

function (doc, meta) {
   if(doc.type && doc.type == "beer") {
     emit(doc.name, doc.brewery_id);
   }
}

我使用它的java代码:

package com.couch.base.simple;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.view.AsyncViewResult;
import com.couchbase.client.java.view.AsyncViewRow;
import com.couchbase.client.java.view.Stale;
import com.couchbase.client.java.view.ViewQuery;

public class Client3 {

    public static void main(String[] args) {
        // getView("_design/dev_beer", "_view/by_name");
        // getView( "_design/beer", "_view/by_name" );
        getView( "_design/beer", "by_name" );
    }

 public static ArrayList<AsyncViewRow> getView(String designDoc, String   
 view) {
    Cluster cluster = CouchbaseCluster.create();
    final Bucket bucket = cluster.openBucket("bucket-1");

    ArrayList<AsyncViewRow> result = new ArrayList<AsyncViewRow>();
    final CountDownLatch latch = new CountDownLatch(1);
    System.out.println("METHOD START");

    bucket.async()
            .query(ViewQuery.from(designDoc, view).limit(20)
                    .stale(Stale.FALSE))
            .doOnNext(new Action1<AsyncViewResult>() {
                @Override
                public void call(AsyncViewResult viewResult) {
                    if (!viewResult.success()) {
                        System.out.println(viewResult.error());
                    } else {
                        System.out.println("Query is running!");
                    }
                }
            })
            .flatMap(
                    new Func1<AsyncViewResult, Observable<AsyncViewRow>>() {
                        @Override
                        public Observable<AsyncViewRow> call(
                                AsyncViewResult viewResult) {
                            return viewResult.rows();
                        }
                    }).subscribe(new Subscriber<AsyncViewRow>() {
                @Override
                public void onCompleted() {
                    latch.countDown();
                }

                @Override
                public void onError(Throwable throwable) {
                    System.err.println("Whoops: " + throwable.getMessage());
                }

                @Override
                public void onNext(AsyncViewRow viewRow) {
                    result.add(viewRow);
                }
            });
    try {
        latch.await();
        } catch (InterruptedException e) {
        e.printStackTrace();
     }`enter code here`
     return result;
  }
}

运行此程序时控制台出错:

INFO: CoreEnvironment: {sslEnabled=false, sslKeystoreFile='null',     
sslKeystorePassword='null', queryEnabled=false, queryPort=8093,    
bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, boot
strapHttpDirectPort=8091, bootstrapHttpSslPort=18091, boot
strapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=4,    
computationPoolSize=4, responseBufferSize=16384, requestBufferSize=16384,  
kvServiceEndpoints=1, viewServiceEndpoints=1, queryServiceEndpoints=1, 
ioPool=NioEventLoopGroup, coreScheduler=CoreScheduler, 
eventBus=DefaultEventBus, packageNameAndVersion=couchbase-java-client/2.1.0 
(git: 2.1.0), dcpEnabled=false, retryStrategy=BestEffort, 
maxRequestLifetime=75000, 
retryDelay=com.couchbase.client.core.time.ExponentialDelay@27ddd392, 
reconnectDelay=com.couchbase.client.core.time.ExponentialDelay@19e1023e, 

Jul 31, 2015 10:17:11 AM com.couchbase.client.core.node.CouchbaseNode$5 call   
 INFO: Connected to Node 127.0.0.1
 Jul 31, 2015 10:17:11 AM     
 com.couchbase.client.core.config.DefaultConfigurationProvider$6 call
INFO: Opened bucket bucket-1
 METHOD START
 Jul 31, 2015 10:17:12 AM com.couchbase.client.java.view.ViewRetryHandler    
 shouldRetry
 INFO: Received a View HTTP response code (400) I did not expect, not   
  retrying.

 {"error":"bad_request","reason":"attachments not supported in Couchbase"}

1 个答案:

答案 0 :(得分:1)

基于此(https://forums.couchbase.com/t/use-views-with-the-java-client/2679/3),我猜你的问题是design-doc的名称:

它应该不是&#34; _design / beer&#34;但只是&#34;啤酒&#34;。