如何使用最新的java sdk 3.1.2在couchbase中执行批插入

如何使用最新的java sdk 3.1.2在couchbase中执行批插入,java,couchbase,Java,Couchbase,我能够使用旧版本的JavaSDK(如2.9.7)执行批插入,代码如下 公共作废插入物(收款凭证){ Observable.from(documents).flatMap(new Func1(){ @凌驾 公共可观察调用(最终JsonDocument docToInsert){ return couchbaseConfig.catalogBucket().async().insert(docToInsert) .doOnError((Throwable Throwable)->log.error(

我能够使用旧版本的JavaSDK(如2.9.7)执行批插入,代码如下

公共作废插入物(收款凭证){

Observable.from(documents).flatMap(new Func1(){
@凌驾
公共可观察调用(最终JsonDocument docToInsert){
return couchbaseConfig.catalogBucket().async().insert(docToInsert)
.doOnError((Throwable Throwable)->log.error(
“将文档{}惰性化为cb时发生异常{}”,throwable.getMessage(),
医生服务);
}
}).last().toBlocking().single();
}


我们需要升级到最新版本的java sdk 3.1.2。我无法在couchbase文档方面获得太多帮助。非常感谢下面的任何链接

关于的Couchbase Java SDK 3.x文档假定您熟悉反应式编程,尤其是

要更新SDK 3的代码,您需要:

  • 提供您自己的
    JsonDocument
    class
  • 写入
    集合
    而不是
    存储桶
  • 从RxJava迁移到Reactor(或使用适配器库)
  • 提供您自己的
    JsonDocument
    class sdk3不再需要或提供sdk2的
    JsonDocument
    之类的文档类。您可以随意对文档ID和内容进行建模。以下是一个类,可用于帮助您过渡到SDK 3:

    public class JsonDocument {
      private final String id;
      private final JsonObject content;
    
      public JsonDocument(String id, JsonDocument content) {
        this.id = id;
        this.content = content;
      }
    
      public String getId() { return id;}
    
      public JsonObject getContent() { return content;}
    
      @Override
      public String toString() {
        return "JsonDocument{id='" + id + "', content=" + content + "}";
      }
    } 
    
    写入
    集合
    而不是
    存储桶
    在SDK 3中,每个bucket都有一个或多个“作用域”。每个作用域包含一个或多个“集合”。作用域有助于多租户;可以为每个客户或部署分配自己的范围。收集帮助您组织文档;例如,您可以将
    小部件
    发票
    放在单独的集合中

    每个bucket都有一个默认作用域,该作用域有一个默认集合

    Couchbase Server 7提供了对作用域和集合的支持,但SDK 3要求您现在就考虑它们。以前属于
    Bucket
    类的所有get/insert/remove/etc.方法都已移动到
    集合

    幸运的是,有一种方便的方法可以访问bucket的默认集合

    这对您的代码意味着什么?在SDK 2中,您有:

    AsyncBucket catalog = couchbaseConfig.catalogBucket().async();
    
    在SDK 3中,您可以编写:

    ReactiveCollection catalog = couchbaseConfig.catalogBucket()
        .defaultCollection()
        .reactive();
    
    从RxJava迁移到Reactor 您可能知道,Couchbase Java SDK 2.x使用RxJava作为其反应模型。sdk3使用了Reactor。概念基本相同,但反应原语的名称不同:

    Obseravble
    ->
    Flux

    单声道
    ->
    单声道

    Completable
    ->
    Mono

    这位是你的朋友

    把它们放在一起 假设
    documents
    是上面提到的
    JsonDocument
    类的列表,下面是您的代码在SDK 3中的外观:

    ReactiveCollection destCollection = couchbaseConfig.catalogBucket()
        .defaultCollection()
        .reactive();
    
    Flux.fromIterable(documents)
        .flatMap(docToInsert ->
            destCollection.insert(docToInsert.getId(), docToInsert.getContent())
                .doOnError(throwable -> log.error(
                    "Exception {} occurred while inserting document {} to cb",
                    throwable.getMessage(), docToInsert)))
        .blockLast();
    
    此代码(以及SDK 2版本)的一个问题是,如果任何插入失败,将不会插入其余文档。如果要继续插入其他文档,可以使用
    OneErrorResume()
    而不是
    doError()

    ReactiveCollection destCollection = couchbaseConfig.catalogBucket()
        .defaultCollection()
        .reactive();
    
    Flux.fromIterable(documents)
        .flatMap(docToInsert ->
            destCollection.insert(docToInsert.getId(), docToInsert.getContent())
                .doOnError(throwable -> log.error(
                    "Exception {} occurred while inserting document {} to cb",
                    throwable.getMessage(), docToInsert)))
        .blockLast();
    
    Flux.fromIterable(documents)
        .flatMap(docToInsert ->
            destCollection.insert(docToInsert.getId(), docToInsert.getContent())
                .onErrorResume(throwable -> {
                  log.error("Exception {} occurred while inserting document {} to cb",
                      throwable.getMessage(), docToInsert);
                  return Mono.empty();
                }))
        .blockLast();