Partager via


Container.CreateTransactionalBatch(PartitionKey) Méthode

Définition

Initialise une nouvelle instance de TransactionalBatch qui peut être utilisée pour effectuer des opérations sur plusieurs éléments du conteneur avec la clé de partition fournie de manière transactionnelle.

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

Paramètres

partitionKey
PartitionKey

Clé de partition pour tous les éléments du lot.

Retours

Nouvelle instance de TransactionalBatch.

Exemples

Cet exemple montre comment modifier atomiquement un ensemble de documents sous la forme d’un lot.

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

Cet exemple montre comment lire atomiquement un ensemble de documents sous la forme d’un lot.

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

Remarques

Limites sur les requêtes TransactionalBatch

S’applique à