TransactionalBatch Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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 CreateTransactionalBatch(PartitionKey) to create an instance of TransactionalBatch.
public abstract class TransactionalBatch
type TransactionalBatch = class
Public MustInherit Class TransactionalBatch
- Inheritance
-
TransactionalBatch
Examples
This example atomically modifies a set of documents as a batch.
public class ToDoActivity
{
public string type { get; set; }
public string id { get; set; }
public string status { get; set; }
}
string activityType = "personal";
ToDoActivity test1 = new ToDoActivity()
{
type = activityType,
id = "learning",
status = "ToBeDone"
};
ToDoActivity test2 = new ToDoActivity()
{
type = activityType,
id = "shopping",
status = "Done"
};
ToDoActivity test3 = new ToDoActivity()
{
type = activityType,
id = "swimming",
status = "ToBeDone"
};
ToDoActivity test4 = new ToDoActivity()
{
type = activityType,
id = "running",
status = "ToBeDone"
};
List<PatchOperation> patchOperations = new List<PatchOperation>();
patchOperations.Add(PatchOperation.Replace("/status", "InProgress");
patchOperations.Add(PatchOperation.Add("/progress", 50);
using (TransactionalBatchResponse batchResponse = await container.CreateTransactionalBatch(new Cosmos.PartitionKey(activityType))
.CreateItem<ToDoActivity>(test1)
.ReplaceItem<ToDoActivity>(test2.id, test2)
.UpsertItem<ToDoActivity>(test3)
.PatchItem(test4.id, patchOperations)
.DeleteItem("reading")
.CreateItemStream(streamPayload1)
.ReplaceItemStream("eating", streamPayload2)
.UpsertItemStream(streamPayload3)
.ExecuteAsync())
{
if (!batchResponse.IsSuccessStatusCode)
{
// Handle and log exception
return;
}
// Look up interested results - eg. via typed access on operation results
TransactionalBatchOperationResult<ToDoActivity> replaceResult = batchResponse.GetOperationResultAtIndex<ToDoActivity>(0);
ToDoActivity readActivity = replaceResult.Resource;
}
This example atomically reads a set of documents as a batch.
string activityType = "personal";
using (TransactionalBatchResponse batchResponse = await container.CreateTransactionalBatch(new Cosmos.PartitionKey(activityType))
.ReadItem("playing")
.ReadItem("walking")
.ReadItem("jogging")
.ReadItem("running")
.ExecuteAsync())
{
// Look up interested results - eg. via direct access to operation result stream
List<string> resultItems = new List<string>();
foreach (TransactionalBatchOperationResult operationResult in batchResponse)
{
using (StreamReader streamReader = new StreamReader(operationResult.ResourceStream))
{
resultItems.Add(await streamReader.ReadToEndAsync());
}
}
}
Remarks
Limits on TransactionalBatch requests
Constructors
TransactionalBatch() |
Methods
Applies to
Azure SDK for .NET