1

我在我的 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"}
4

2 回答 2

1

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

它可能不应该是“_design/beer”,而应该是“beer”。

于 2015-08-03T06:01:24.377 回答
0

我犯了一个非常愚蠢的错误,我在啤酒样本桶上创建视图,但在代码中我正在对桶 1 桶进行查询,所以出错了。

因此,请确保您查询的是在其上创建视图的同一存储桶。感谢您的支持。

于 2015-08-16T15:34:50.190 回答