It look like that it is not possible to rename a container in the Azure Cosmos DB. It should be copy to the new container via a bulk operation. How can I do this with the Java SDK? Are there any samples for it?
-
Do the below comments help? – Harshita Singh Mar 17 '21 at 14:01
2 Answers
Yes, you are right. Changing container name is currently not possible. As I understand, you want to discard old container (as you wanted to rename initially) and migrate data to new one.
Data Migration tool is a great tool to do so: Tutorial: Use Data migration tool to migrate your data to Azure Cosmos DB
Also, do check out Bulk Executor library for Java, API documentation and samples.
You can use importAll
in BulkExecutor class:
ConnectionPolicy connectionPolicy = new ConnectionPolicy();
RetryOptions retryOptions = new RetryOptions();
// Set client's retry options high for initialization
retryOptions.setMaxRetryWaitTimeInSeconds(120);
retryOptions.setMaxRetryAttemptsOnThrottledRequests(100);
connectionPolicy.setRetryOptions(retryOptions);
connectionPolicy.setMaxPoolSize(1000);
DocumentClient client = new DocumentClient(HOST, MASTER_KEY, connectionPolicy, null);
String collectionLink = String.format("/dbs/%s/colls/%s", "mydb", "mycol");
DocumentCollection collection = client.readCollection(collectionLink, null).getResource();
DocumentBulkExecutor executor = DocumentBulkExecutor.builder().from(client, collection,
collection.getPartitionKey(), collectionOfferThroughput).build();
// Set retries to 0 to pass control to bulk executor
client.getConnectionPolicy().getRetryOptions().setMaxRetryWaitTimeInSeconds(0);
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
for(int i = 0; i < 10; i++) {
List documents = documentSource.getMoreDocuments();
BulkImportResponse bulkImportResponse = executor.importAll(documents, false, true, 40);
// Validate that all documents inserted to ensure no failure.
if (bulkImportResponse.getNumberOfDocumentsImported() < documents.size()) {
for(Exception e: bulkImportResponse.getErrors()) {
// Validate why there were some failures.
e.printStackTrace();
}
break;
}
}
executor.close();
client.close();

- 6,405
- 6
- 28
- 69

- 4,590
- 1
- 10
- 13
-
-
Sure, check out Bulk Executor library for Java: https://search.maven.org/search?q=documentdb-bulkexecutor, API documentation: https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.documentdb.bulkexecutor?view=azure-java-stable and samples: https://github.com/Azure/azure-cosmosdb-bulkexecutor-java-getting-started – Harshita Singh Mar 16 '21 at 06:44
-
-
That doesn't really help. It points only to a library which contains again hundreds of settings. – Horcrux7 Mar 18 '21 at 09:31
-
But I guess your initial question was about supportability in Java SDK and samples. hence, shared. – Harshita Singh Mar 18 '21 at 09:34
-
Your samples are only for bulkdelete, bulkimport and bulkupdate. What I had needed is a sample for bulkcopy with the parameters of the source collection and target collection. The main difference is that the data are already in the database. I does not want transfer all the data to the client and back to the database. For such a transfer I does not need an extra library. In the sample I can not see such inner database transfer. Another problem is that the sample start from a command line. I have an com.azure.cosmos.CosmosContainer object and need a rename. This means all details must copy. – Horcrux7 Mar 18 '21 at 10:25
-
I solve the problem by linking instead copying all the data. This simulate an rename of the container. Instead of one container I use two container now. The first contains only the name of the second container.
Now I can build the new version of the container. If I are finish I change the name of the saved container name. Then I drop the old container.
The tricky is to inform all nodes of the app to use the new container name.

- 23,758
- 21
- 98
- 156