簡體   English   中英

如何使用 Java SQL ZDB97423871083819Z Azure Cosmos DB 重命名容器

[英]How to rename a container in Azure Cosmos DB with Java SQL API?

看起來無法重命名 Azure Cosmos DB 中的容器 它應該通過批量操作復制到新容器中。 如何使用 Java SDK 做到這一點? 有樣品嗎?

是的你是對的。 目前無法更改容器名稱。 據我了解,您想丟棄舊容器(因為您最初想重命名)並將數據遷移到新容器。

數據遷移工具是一個很好的工具:教程:使用數據遷移工具將數據遷移到 Azure Cosmos DB

另外,請查看JavaAPI 文檔示例的批量執行程序庫。

您可以在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();

我通過鏈接而不是復制所有數據來解決問題。 這模擬了容器的重命名。 我現在使用兩個容器而不是一個容器。 第一個僅包含第二個容器的名稱。

現在我可以構建新版本的容器了。 如果我完成了,我會更改已保存容器名稱的名稱。 然后我放下舊容器。

棘手的是通知應用程序的所有節點使用新的容器名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM