Unable to create primary index on couchbase using groovy script

守給你的承諾、 提交于 2019-12-11 15:33:30

问题


I am not able to Create primary index on couchbase using groovy script. Below are the lines of code I used:-

@Grab('com.couchbase.client:java-client:2.2.6')

import java.util.concurrent.CountDownLatch;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.CouchbaseCluster
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
import com.couchbase.client.java.query.SimpleN1qlQuery;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;

CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().connectTimeout(10000).build();
def cluster = CouchbaseCluster.create(env, IPADDRESS);
def bucket = cluster.openBucket(BUCKET_NAME, BUCKET_PASSWORD);
log.info "Connection done"

String queryString = "CREATE PRIMARY INDEX `PrimInd` ON BUCKET_NAME"
bucket.query(N1qlQuery.simple(queryString))

log.info "Primary index created"

It gives me error as below :-

java.lang.RuntimeException: java.util.concurrent.TimeoutException at this line:-

bucket.query(N1qlQuery.simple(queryString))

Connection is being done properly and same query works in couchbase server. So, I think there is problem with my code.

Could you please help me on this?


回答1:


In the Couchbase Java client, the query() method delegates to a Blocking API which uses JavaRx under the covers. The source code for the Blocking API states:

If an error happens inside the Observable, it will be raised as an Exception. If the timeout kicks in, a TimeoutException nested in a RuntimeException is thrown to be fully compatible with the Observable.timeout(long, TimeUnit) behavior.

You're experiencing a TimeoutException nested in a RuntimeException, hence the root cause is that your query is timing out.

DefaultCouchbaseEnvironment defaults to a queryTimeout (the timeout used for N1qlQuery queries) of 75 milli-seconds. You can change this default with the environment builder:

def env = DefaultCouchbaseEnvironment.builder()
    .connectTimeout(10000)
    .queryTimeout(10000) // This is the query timeout
    .build()


来源:https://stackoverflow.com/questions/48986045/unable-to-create-primary-index-on-couchbase-using-groovy-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!