你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

DocumentBulkExecutor Class

  • java.lang.Object
    • com.microsoft.azure.documentdb.bulkexecutor.DocumentBulkExecutor

Implements

java.lang.AutoCloseable

public class DocumentBulkExecutor
implements java.lang.AutoCloseable

Method Summary

Modifier and Type Method and Description
static Builder builder()

Creates a new DocumentBulkExecutor.Builder instance

void close()

Releases any internal resources.

BulkDeleteResponse deleteAll(List<Pair<String,String>> pkIdPairsToDelete)

Executes a bulk delete in the Azure Cosmos DB database service.

BulkImportResponse importAll(Collection<String> documents, boolean isUpsert, boolean disableAutomaticIdGeneration, Integer maxConcurrencyPerPartitionRange)

Executes a bulk import in the Azure Cosmos DB database service.

BulkUpdateResponse mergeAll(Collection<Document> patchDocuments, Integer maxConcurrencyPerPartitionRange)

Executes a bulk update in the Azure Cosmos DB database service with given set of patch documents.

BulkUpdateResponse updateAll(Collection<UpdateItem> updateItems, Integer maxConcurrencyPerPartitionRange)

Executes a bulk update in the Azure Cosmos DB database service.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Method Details

builder

public static DocumentBulkExecutor.Builder builder()

Creates a new DocumentBulkExecutor.Builder instance

Returns:

close

public void close()

Releases any internal resources. It is responsibility of the caller to close DocumentClient.

deleteAll

public BulkDeleteResponse deleteAll(List> pkIdPairsToDelete)

Executes a bulk delete in the Azure Cosmos DB database service.

Parameters:

pkIdPairsToDelete - List of pairs of partition key and id values of documents to delete

Returns:

an instance of BulkDeleteResponse

Throws:

DocumentClientException - if any failure happens

importAll

public BulkImportResponse importAll(Collection documents, boolean isUpsert, boolean disableAutomaticIdGeneration, Integer maxConcurrencyPerPartitionRange)

Executes a bulk import in the Azure Cosmos DB database service.

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();

Parameters:

documents - specifies the collection of JSON-serialized documents to import
isUpsert - indicates whether a document in the supplied collection needs to be overwritten if the id already exists
disableAutomaticIdGeneration - indicates whether the id has to be automatically generated for a document if absent in the supplied collection
maxConcurrencyPerPartitionRange - specifies the maximum degree of concurrency per partition key range (default value is 20 if set to null)

Returns:

an instance of BulkImportResponse

Throws:

DocumentClientException - if any failure happens

mergeAll

public BulkUpdateResponse mergeAll(Collection patchDocuments, Integer maxConcurrencyPerPartitionRange)

Executes a bulk update in the Azure Cosmos DB database service with given set of patch documents.

Parameters:

patchDocuments - which are documents comprising id, partition key values and fields to set with the corresponding values
maxConcurrencyPerPartitionRange - specifies the maximum degree of concurrency per partition key range (default value is 20 if set to null)

Returns:

an instance of BulkUpdateResponse

Throws:

DocumentClientException - if any failure happens

updateAll

public BulkUpdateResponse updateAll(Collection updateItems, Integer maxConcurrencyPerPartitionRange)

Executes a bulk update in the Azure Cosmos DB database service.

Parameters:

updateItems - specifies the collection of update items each of which comprises the list of field update operations to be performed on a document identified by an id and partition key value.
maxConcurrencyPerPartitionRange - specifies the maximum degree of concurrency per partition key range (default value is 20 if set to null)

Returns:

an instance of BulkUpdateResponse

Throws:

DocumentClientException - if any failure happens

Applies to