在批量插入

时间:2016-01-14 14:16:14

标签: java couchbase batch-insert

我正在向CouchBase批量插入数据并在几分钟后获得此异常:

  

com.couchbase.client.java.error.TemporaryFailureException at   com.couchbase.client.java.CouchbaseAsyncBucket $ 13.call(CouchbaseAsyncBucket.java:445)         在   com.couchbase.client.java.CouchbaseAsyncBucket $ 13.call(CouchbaseAsyncBucket.java:426)         at rx.internal.operators.OperatorMap $ 1.onNext(OperatorMap.java:54)         在rx.observers.Subscribers $ 5.onNext(Subscribers.java:234)at   rx.subjects.SubjectSubscriptionManager $ SubjectObserver.onNext(SubjectSubscriptionManager.java:222)         在rx.subjects.AsyncSubject.onCompleted(AsyncSubject.java:101)at   com.couchbase.client.core.endpoint.AbstractGenericHandler $ 1.call(AbstractGenericHandler.java:265)         在   rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)         在   java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)         在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:180)         在   java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)         在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)         在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)         在java.lang.Thread.run(Thread.java:745)

以下是代码:

    cluster = CouchbaseCluster.create();
    Bucket bucket = cluster.openBucket();

    int numBatchs = 10000;
    int numDocsInBatch = 1000;

    for (int j = 0; j < numBatchs; j++) {
        long start = System.currentTimeMillis();

        List<JsonDocument> documents = new ArrayList<>(numDocsInBatch);
        for (int i = 0; i < numDocsInBatch; i++) {
            String uniqueID = UUID.randomUUID().toString() + "_" + System.currentTimeMillis();

            JsonObject user = JsonObject.
                    empty();

            documents.add(JsonDocument.create(uniqueID, user));
        }

        Observable
                .from(documents)
                .flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
                    @Override
                    public Observable<JsonDocument> call(final JsonDocument docToInsert) {
                        return bucket.async().insert(docToInsert);
                    }
                })
                .last()
                .toBlocking()
                .single();

我做错了什么?

1 个答案:

答案 0 :(得分:2)

TemporaryFailureException通常是因为服务器可能太忙而导致的,通常可以通过退避重试该操作。

供参考:http://developer.couchbase.com/documentation/server/4.0/sdks/java-2.2/documents-bulk.html