Compartir a través de


CosmosBatch Class

  • java.lang.Object
    • com.azure.cosmos.models.CosmosBatch

public final class CosmosBatch

Represents a batch of operations against items with the same PartitionKey in a container that will be performed in a Cosmos manner at the Azure Cosmos DB service.

Use createCosmosBatch(PartitionKey partitionKey) to create an instance of CosmosBatch. Example This example atomically modifies a set of items as a batch.

public class ToDoActivity {
     public final String type;
     public final String id;
     public final String status;
     public ToDoActivity(String type, String id, String status) {
         this.type = type;
         this.id = id;
         this.status = status;
     }
 }

 String activityType = "personal";

 ToDoActivity test1 = new ToDoActivity(activityType, "learning", "ToBeDone");
 ToDoActivity test2 = new ToDoActivity(activityType, "shopping", "Done");
 ToDoActivity test3 = new ToDoActivity(activityType, "swimming", "ToBeDone");

 CosmosBatch batch = CosmosBatch.createCosmosBatch(new Cosmos.PartitionKey(activityType));
 batch.createItemOperation(test1);
 batch.replaceItemOperation(test2.id, test2);
 batch.upsertItemOperation(test3);
 batch.deleteItemOperation("reading");

 CosmosBatchResponse response = container.executeTransactionalBatch(batch);

 if (!response.isSuccessStatusCode()) {
      // Handle and log exception
      return;
 }

 // Look up interested results - e.g., via typed access on operation results

 CosmosBatchOperationResult result = response.get(0);
 ToDoActivity readActivity = result.getItem(ToDoActivity.class);

Example

This example atomically reads a set of items as a batch.

String activityType = "personal";

 CosmosBatch batch = CosmosBatch.createCosmosBatch(new Cosmos.PartitionKey(activityType));
 batch.readItemOperation("playing");
 batch.readItemOperation("walking");
 batch.readItemOperation("jogging");
 batch.readItemOperation("running");

 CosmosBatchResponse response = container.executeTransactionalBatch(batch);
 List resultItems = new ArrayList();

 for (int i = 0; i < response.size(); i++) {
     CosmosBatchOperationResult result = response.get(0);
     resultItems.add(result.getItem(ToDoActivity.class));
 }

See: Limits on CosmosBatch requests.

Method Summary

Modifier and Type Method and Description
CosmosItemOperation createItemOperation(T item)

Adds an operation to create an item into the batch.

CosmosItemOperation createItemOperation(T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to create an item into the batch.

CosmosItemOperation replaceItemOperation(String id, T item)

Adds an operation to replace an item into the batch.

CosmosItemOperation replaceItemOperation(String id, T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to replace an item into the batch.

CosmosItemOperation upsertItemOperation(T item)

Adds an operation to upsert an item into the batch.

CosmosItemOperation upsertItemOperation(T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to upsert an item into the batch.

static CosmosBatch createCosmosBatch(PartitionKey partitionKey)

Initializes a new instance of CosmosBatch that will contain operations to be performed across multiple items in the container with the provided partition key in a transactional manner

CosmosItemOperation deleteItemOperation(String id)

Adds an operation to delete an item into the batch.

CosmosItemOperation deleteItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to delete an item into the batch.

List<CosmosItemOperation> getOperations()

Return the list of operation in an unmodifiable instance so no one can change it in the down path.

PartitionKey getPartitionKeyValue()

Return the partition key for this batch.

CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations)

Adds a patch operations for an item into the batch.

CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations, CosmosBatchPatchItemRequestOptions requestOptions)

Adds a patch operations for an item into the batch.

CosmosItemOperation readItemOperation(String id)

Adds an operation to read an item into the batch.

CosmosItemOperation readItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to read an item into the batch.

Methods inherited from java.lang.Object

Method Details

createItemOperation

public CosmosItemOperation createItemOperation(T item)

Adds an operation to create an item into the batch.

Parameters:

item - A JSON serializable object that must contain an id property.

Returns:

The Cosmos batch instance with the operation added.

createItemOperation

public CosmosItemOperation createItemOperation(T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to create an item into the batch.

Parameters:

item - A JSON serializable object that must contain an id property.
requestOptions - The options for the item request.

Returns:

The Cosmos batch instance with the operation added.

replaceItemOperation

public CosmosItemOperation replaceItemOperation(String id, T item)

Adds an operation to replace an item into the batch.

Parameters:

id - The unique id of the item.
item - A JSON serializable object that must contain an id property.

Returns:

The Cosmos batch instance with the operation added.

replaceItemOperation

public CosmosItemOperation replaceItemOperation(String id, T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to replace an item into the batch.

Parameters:

id - The unique id of the item.
item - A JSON serializable object that must contain an id property.
requestOptions - The options for the item request.

Returns:

The Cosmos batch instance with the operation added.

upsertItemOperation

public CosmosItemOperation upsertItemOperation(T item)

Adds an operation to upsert an item into the batch.

Parameters:

item - A JSON serializable object that must contain an id property.

Returns:

The Cosmos batch instance with the operation added.

upsertItemOperation

public CosmosItemOperation upsertItemOperation(T item, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to upsert an item into the batch.

Parameters:

item - A JSON serializable object that must contain an id property.
requestOptions - The options for the item request.

Returns:

The Cosmos batch instance with the operation added.

createCosmosBatch

public static CosmosBatch createCosmosBatch(PartitionKey partitionKey)

Initializes a new instance of CosmosBatch that will contain operations to be performed across multiple items in the container with the provided partition key in a transactional manner

Parameters:

partitionKey - the partition key for all items in the batch.

Returns:

A new instance of CosmosBatch.

deleteItemOperation

public CosmosItemOperation deleteItemOperation(String id)

Adds an operation to delete an item into the batch.

Parameters:

id - The unique id of the item.

Returns:

The Cosmos batch instance with the operation added.

deleteItemOperation

public CosmosItemOperation deleteItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to delete an item into the batch.

Parameters:

id - The unique id of the item.
requestOptions - The options for the item request.

Returns:

The Cosmos batch instance with the operation added.

getOperations

public List getOperations()

Return the list of operation in an unmodifiable instance so no one can change it in the down path.

Returns:

The list of operations which are to be executed.

getPartitionKeyValue

public PartitionKey getPartitionKeyValue()

Return the partition key for this batch.

Returns:

The partition key for this batch.

patchItemOperation

public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations)

Adds a patch operations for an item into the batch.

Parameters:

id - the item id.
cosmosPatchOperations - Represents a container having list of operations to be sequentially applied to the referred Cosmos item.

Returns:

The added operation.

patchItemOperation

public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations, CosmosBatchPatchItemRequestOptions requestOptions)

Adds a patch operations for an item into the batch.

Parameters:

id - the item id.
cosmosPatchOperations - Represents a container having list of operations to be sequentially applied to the referred Cosmos item.
requestOptions - The options for the item request.

Returns:

The added operation.

readItemOperation

public CosmosItemOperation readItemOperation(String id)

Adds an operation to read an item into the batch.

Parameters:

id - The unique id of the item.

Returns:

The Cosmos batch instance with the operation added.

readItemOperation

public CosmosItemOperation readItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)

Adds an operation to read an item into the batch.

Parameters:

id - The unique id of the item.
requestOptions - The options for the item request.

Returns:

The Cosmos batch instance with the operation added.

Applies to