看起来像这样it is not possible to rename a container in the Azure Cosmos DB 。应通过批量操作将其复制到新容器。如何使用 Java SDK 做到这一点?有 sample 吗?
最佳答案
是的,你说得对。目前无法更改容器名称。据我了解,您想要丢弃旧容器(正如您最初想要重命名的那样)并将数据迁移到新容器。
数据迁移工具是一个很棒的工具: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();
关于java - 如何使用 Java SQL API 重命名 Azure Cosmos DB 中的容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66584455/