问题描述
看起来像it is not possible to rename a container in the Azure Cosmos DB。它应该通过批量操作复制到新容器。如何使用 Java SDK 执行此操作?有样品吗?
解决方法
是的,你说得对。当前无法更改容器名称。据我了解,您想丢弃旧容器(因为您最初想重命名)并将数据迁移到新容器。
数据迁移工具是一个很好的工具:Tutorial: Use Data migration tool to migrate your data to Azure Cosmos DB
另外,请查看 Bulk Executor library for Java、API documentation 和 samples。
您可以在 BulkExecutor 类中使用 importAll
:
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();
,
我通过链接而不是复制所有数据来解决问题。这模拟了容器的重命名。我现在使用两个容器而不是一个容器。第一个只包含第二个容器的名称。
现在我可以构建新版本的容器了。如果我完成了,我会更改保存的容器名称的名称。然后我放下旧容器。
棘手的是通知应用程序的所有节点使用新的容器名称。