CosmosBatch
Represents a batch of operations against items with the same PartitionKey
in a container that will be performed
in a transactional manner at the Azure Cosmos DB service.
Use TransactionalBatch.createTransactionalBatch(PartitionKey)
to create an instance of TransactionalBatch.
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");
TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new Cosmos.PartitionKey(activityType));
batch.createItemOperation<ToDoActivity>(test1);
batch.replaceItemOperation<ToDoActivity>(test2.id, test2);
batch.upsertItemOperation<ToDoActivity>(test3);
batch.deleteItemOperation("reading");
TransactionalBatchResponse response = container.executeTransactionalBatch(batch);
if (!response.isSuccessStatusCode()) {
// Handle and log exception
return;
}
// Look up interested results - e.g., via typed access on operation results
TransactionalBatchOperationResult 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";
TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new Cosmos.PartitionKey(activityType));
batch.readItemOperation("playing");
batch.readItemOperation("walking");
batch.readItemOperation("jogging");
batch.readItemOperation("running");
TransactionalBatchResponse response = container.executeTransactionalBatch(batch);
List<ToDoActivity> resultItems = new ArrayList<ToDoActivity>();
for (int i = 0; i < response.size(); i++) {
TransactionalBatchOperationResult result = response.get(0);
resultItems.add(result.getItem(ToDoActivity.class));
}
@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public final class TransactionalBatch extends Object
@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public static TransactionalBatch createTransactionalBatch(PartitionKey partitionKey)
TransactionalBatch
that will contain operations to be performed across multiple items in the container with the provided partition
key in a transactional mannerpartitionKey
- the partition key for all items in the batch.TransactionalBatch
.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation createItemOperation(T item)
T
- The type of item to be created.item
- A JSON serializable object that must contain an id property.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation createItemOperation(T item, TransactionalBatchItemRequestOptions requestOptions)
T
- The type of item to be created.item
- A JSON serializable object that must contain an id property.requestOptions
- The options for the item request.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation deleteItemOperation(String id)
id
- The unique id of the item.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation deleteItemOperation(String id, TransactionalBatchItemRequestOptions requestOptions)
id
- The unique id of the item.requestOptions
- The options for the item request.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation readItemOperation(String id)
id
- The unique id of the item.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation readItemOperation(String id, TransactionalBatchItemRequestOptions requestOptions)
id
- The unique id of the item.requestOptions
- The options for the item request.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation replaceItemOperation(String id, T item)
T
- The type of item to be replaced.id
- The unique id of the item.item
- A JSON serializable object that must contain an id property.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation replaceItemOperation(String id, T item, TransactionalBatchItemRequestOptions requestOptions)
T
- The type of item to be replaced.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.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation upsertItemOperation(T item)
T
- The type of item to be upserted.item
- A JSON serializable object that must contain an id property.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public <T> CosmosItemOperation upsertItemOperation(T item, TransactionalBatchItemRequestOptions requestOptions)
T
- The type of item to be upserted.item
- A JSON serializable object that must contain an id property.requestOptions
- The options for the item request.@Beta(value=V4_11_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations)
id
- the item id.cosmosPatchOperations
- Represents a container having list of operations to be sequentially applied to the referred Cosmos item.@Beta(value=V4_11_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations, TransactionalBatchPatchItemRequestOptions requestOptions)
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.@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public List<CosmosItemOperation> getOperations()
@Beta(value=V4_7_0, warningText="Preview API - subject to change in non-backwards compatible way") @Deprecated public PartitionKey getPartitionKeyValue()
Copyright © 2021 Microsoft Corporation. All rights reserved.