Batch Operations Using AWS SDK for .NET Object Persistence Model

Batch Write: Putting and Deleting Multiple Items

To put or delete multiple objects from a table in a single request, do the
following:

Execute CreateBatchWrite method of the
DynamoDBContext and create an instance of the
BatchWrite class.

Specify the items you want to put or delete.

To put one or more items, use either the
AddPutItem or the AddPutItems
method.

To delete one or more items, you can either specify the
primary key of the item or a client-side object that maps to the
item you want to delete. Use the AddDeleteItem,
AddDeleteItems, and the
AddDeleteKey methods to specify the list of
items to delete.

Call the BatchWrite.Execute method to put and delete all
the specified items from the table.

Note

When using object persistence model, you can specify any number of
operations in a batch. However, note that DynamoDB limits the number
of operations in a batch and the total size of the batch in a batch
operation. For more information about the specific limits, see BatchWriteItem. If
the API detects your batch write request exceeded the allowed number of
write requests or exceeded the maximum allowed HTTP payload size, it breaks
the batch in to several smaller batches. Additionally, if a response to a
batch write returns unprocessed items, the API will automatically send
another batch request with those unprocessed items.

Suppose that you have defined a C# class Book class that maps to the
ProductCatalog table in DynamoDB. The following C# code snippet uses the
BatchWrite object to upload two items and delete one item from the
ProductCatalog table.

Create one instance of the BatchWrite class for each type
and specify the items you want to put or delete as described in the
preceding section.

Create an instance of MultiTableBatchWrite using one of
the following methods:

Execute the Combine method on one of the
BatchWrite objects that you created in the
preceding step.

Create an instance of the MultiTableBatchWrite
type by providing a list of BatchWrite
objects.

Execute the CreateMultiTableBatchWrite method of
DynamoDBContext and pass in your list of
BatchWrite objects.

Call the Execute method of
MultiTableBatchWrite, which performs the specified put
and delete operations on various tables.

Suppose that you have defined Forum and Thread C# classes that map to the Forum
and Thread tables in DynamoDB. Also, suppose that the Thread class has
versioning enabled. Because versioning is not supported when using batch operations,
you must explicitly disable versioning as shown in the following C# code snippet.
The code snippet uses the MultiTableBatchWrite object to perform a
multi-table update.

DynamoDB batch API limits the number of writes in batch and also
limits the size of the batch. For more information, see BatchWriteItem.
When using the .NET object persistence model API, you can specify any number
of operations. However, if either the number of operations in a batch or
size exceed the limit, the .NET API breaks the batch write request into
smaller batches and sends multiple batch write requests to DynamoDB.

Batch Get: Getting Multiple Items

To retrieve multiple items from a table in a single request, do the
following:

Create an instance of the CreateBatchGet class.

Specify a list of primary keys to retrieve.

Call the Execute method. The response returns the items
in the Results property.

The following C# code sample retrieves three items from the ProductCatalog table.
The items in the result are not necessarily in the same order in which you specified
the primary keys.