首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java.中为每个文档生成密钥的标准过程是什么?

问在java.中为每个文档生成密钥的标准过程是什么?
EN

Stack Overflow用户
提问于 2017-03-28 05:35:36
回答 1查看 51关注 0票数 0

我希望在Java中将文档作为批量插入Couchbase。那么,在java.中为每个文档生成密钥的标准过程是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-30 09:14:01

您可以使用Couchbase“计数器”文档作为序列的形式。使用Java的反应性方法,如果您的批处理是一个包含要保存到Couchbase的每个内容的List<JsonObject>,则这样做如下:

代码语言:javascript
复制
//start with a sequence of contents to save
Observable.from(listOfDocumentContent)
          //for each content, asynchronously generate something...
          .flatMap(content -> bucket.async() //assuming bucket is a `Bucket`
              //atomically generate an increasing value, starting from 0
              .counter("DOCUMENT_KEY_GENERATOR", 1, 0) //use a more relevant document key
              //this gives a `JsonLongDocument`, so extract the number and turn that + the original content into a `JsonDocument` to be saved
              .map(cDoc -> JsonDocument.create(KEY_PREFIX + cDoc.content(), content))
          )
          //next up, asynchronously saving each document we generated in the previous step... You could also use insert since you don't expect the keys to already exist in Couchbase
          .flatMap(docToSave -> bucket.async().upsert(docToSave))
          //this will perform the above asynchronously but wait for the last doc in the batch to finish saving:
          .toBlocking().last(); 

注意,在生成要保存的文档时,我们使用了一个KEY_PREFIX,这样冲突的风险就更小了(否则,存储桶中的其他文档可以命名为"1“,如果您在同一个桶中对多个类型的文档这样做的话)。

还调整了用于您的需求的保存方法(在这里,upsert与create与update、TTL、耐久性要求等)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43061341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档