Search code examples
couchbasecouchbase-java-api

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


I want to insert documents into Couchbase as a bulk in Java. So what is the standard procedure for Generating keys for each document in java..?


Solution

  • You could use a Couchbase "counter" document as a form of sequence. Using the reactive approach with the Java SDK, this would go something like this, assuming your batch is a List<JsonObject> with each content to save to 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(); 
    

    Notice we use a KEY_PREFIX when generating the document to be saved, so that there is less risk of collision (otherwise, other documents in the bucket could be named "1" if you do that for multiple types of documents inside the same bucket).

    Also tune the saving method used to your needs (here upsert vs create vs update, TTL, durability requirements, etc...)