java - 如何使用 Java SQL API 重命名 Azure Cosmos DB 中的容器?

标签 java azure azure-cosmosdb azure-cosmosdb-sqlapi azure-java-sdk

看起来像这样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 documentationsamples .

您可以在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/

相关文章:

java - 将 WAR 部署到 AWS Tomcat 404 错误

java - 用java保护excel文件

c# - 如何使用用户凭据访问 Azure Key Vault?

azure - 使用 CircleCI 部署到 Azure VM

azure - 处理 DocumentDB 中每秒请求单位 (RU/s) 的峰值

Java(RMI)服务器到服务器的身份验证方法?

java - 如何在 JPanel 中绘制形状?

azure - Elasticsearch Pod 在 Init 状态后失败且没有日志

azure - 使用数据工厂将数据从 Azure cosmos 美国区域迁移到欧洲区域

c# - 使用 C# ASP.NET MVC 显示来自单个数组 JSON(azure Cosmos db) 的数据