CosmosBatch Class
- java.
lang. Object - com.
azure. cosmos. models. CosmosBatch
- com.
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));
}
Method Summary
Methods inherited from java.lang.Object
Method Details
createItemOperation
public CosmosItemOperation
Adds an operation to create an item into the batch.
Parameters:
Returns:
createItemOperation
public CosmosItemOperation
Adds an operation to create an item into the batch.
Parameters:
Returns:
replaceItemOperation
public CosmosItemOperation
Adds an operation to replace an item into the batch.
Parameters:
Returns:
replaceItemOperation
public CosmosItemOperation
Adds an operation to replace an item into the batch.
Parameters:
Returns:
upsertItemOperation
public CosmosItemOperation
Adds an operation to upsert an item into the batch.
Parameters:
Returns:
upsertItemOperation
public CosmosItemOperation
Adds an operation to upsert an item into the batch.
Parameters:
Returns:
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:
Returns:
deleteItemOperation
public CosmosItemOperation deleteItemOperation(String id)
Adds an operation to delete an item into the batch.
Parameters:
Returns:
deleteItemOperation
public CosmosItemOperation deleteItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)
Adds an operation to delete an item into the batch.
Parameters:
Returns:
getOperations
public List
Return the list of operation in an unmodifiable instance so no one can change it in the down path.
Returns:
getPartitionKeyValue
public PartitionKey getPartitionKeyValue()
Return the partition key for this batch.
Returns:
patchItemOperation
public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations)
Adds a patch operations for an item into the batch.
Parameters:
Returns:
patchItemOperation
public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations, CosmosBatchPatchItemRequestOptions requestOptions)
Adds a patch operations for an item into the batch.
Parameters:
Returns:
readItemOperation
public CosmosItemOperation readItemOperation(String id)
Adds an operation to read an item into the batch.
Parameters:
Returns:
readItemOperation
public CosmosItemOperation readItemOperation(String id, CosmosBatchItemRequestOptions requestOptions)
Adds an operation to read an item into the batch.
Parameters:
Returns:
Applies to
Azure SDK for Java