Compartir a través de


Container.CreateTransactionalBatch(PartitionKey) Método

Definición

Inicializa una nueva instancia de TransactionalBatch que se puede usar para realizar operaciones en varios elementos del contenedor con la clave de partición proporcionada de forma transaccional.

public abstract Microsoft.Azure.Cosmos.TransactionalBatch CreateTransactionalBatch (Microsoft.Azure.Cosmos.PartitionKey partitionKey);
abstract member CreateTransactionalBatch : Microsoft.Azure.Cosmos.PartitionKey -> Microsoft.Azure.Cosmos.TransactionalBatch
Public MustOverride Function CreateTransactionalBatch (partitionKey As PartitionKey) As TransactionalBatch

Parámetros

partitionKey
PartitionKey

Clave de partición para todos los elementos del lote.

Devoluciones

Nueva instancia de TransactionalBatch.

Ejemplos

En este ejemplo se modifica de forma atómica un conjunto de documentos como un lote.

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"
};

using (TransactionalBatchResponse batchResponse = await container.CreateTransactionalBatch(new Cosmos.PartitionKey(activityType))
    .CreateItem<ToDoActivity>(test1)
    .ReplaceItem<ToDoActivity>(test2.id, test2)
    .UpsertItem<ToDoActivity>(test3)
    .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;
}

En este ejemplo se lee de forma atómica un conjunto de documentos como un lote.

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

Comentarios

Límites de solicitudes TransactionalBatch

Se aplica a