Reputation: 402
I'm using Java DocumentDb bulk-executor
to bulk import an array of json to Azure Cosmos DB
.
Sample JSON
:
[
{
"SId": "101",
"SName": "ABC"
},
{
"SId": "102",
"SName": "XYZ"
}
]
Sample Code :
(PARTTION_KEY = "\SId")
DocumentCollection collection = Utilities.createEmptyCollectionIfNotExists(client, DATABASE, CONTAINER, PARTITION_KEY, THROUGHPUT);
ArrayList<String> list = new ArrayList<String>();
JSONParser jsonParser = new JSONParser();
FileReader reader = new FileReader("C:\\samplejson.json");
Object obj = jsonParser.parse(reader);
JSONArray jsonArray = (JSONArray) obj;
if (jsonArray != null) {
int len = jsonArray.size();
for (int i=0;i<len;i++){
list.add(jsonArray.get(i).toString());
}
}
client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(30);
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(9);
DocumentBulkExecutor.Builder bulkExecutorBuilder = DocumentBulkExecutor.builder().from(client, DATABASE, CONTAINER,
collection.getPartitionKey(), 20000);
DocumentBulkExecutor bulkExecutor = bulkExecutorBuilder.build();
client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(0);
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);
System.out.println(bulkImportResponse.getNumberOfDocumentsImported());
Now if I have this another JSON :
[
{
"SId": "101, // Item with this SID has already been inserted
"SName": "ABCDEF"
},
{
"SId": "103",
"SName": "PQR"
}
]
I want to insert this JSON to the same container. But it's just stored as a new entry and with a different "id", automatically created by Cosmos DB.
How can I bulk import and overwrite if item on the basis of"SId", if it already exists, at the same time?
Please help!
Upvotes: 0
Views: 1397
Reputation: 637
You need to change the isUpsert flag in your call to importAll to be true. This will enable the Upsert operation which means it will either add a new document if the id doesn't already exist, or it will update an existing document if the id is already there.
Change line:
BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, false, false, null);
To
BulkImportResponse bulkImportResponse = bulkExecutor.importAll(list, true, false, null);
Upvotes: 1