为 java 中的每个文档生成密钥的标准程序是什么?

what is the standard procedure for Generating keys for each document in java..?

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

您可以使用 Couchbase "counter" 文档作为序列的一种形式。使用 Java SDK 的反应式方法,这将是这样的,假设您的批次是 List<JsonObject>,每个内容都保存到 Couchbase:

//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 vs create vs update、TTL、耐久性要求等...)