[英]How to rename a container in Azure Cosmos DB with Java SQL API?
It look like that it is not possible to rename a container in the Azure Cosmos DB .看起来无法重命名 Azure Cosmos DB 中的容器。 It should be copy to the new container via a bulk operation.它应该通过批量操作复制到新容器中。 How can I do this with the Java SDK?如何使用 Java SDK 做到这一点? Are there any samples for it?有样品吗?
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数据迁移工具是一个很好的工具:教程:使用数据迁移工具将数据迁移到 Azure Cosmos DB
Also, do check out Bulk Executor library for Java , API documentation and samples .另外,请查看Java 、 API 文档和示例的批量执行程序库。
You can use importAll
in BulkExecutor class:您可以在BulkExecutor class 中使用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();
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.棘手的是通知应用程序的所有节点使用新的容器名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.