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