Allows to execute a batch of Put and/or Delete Requests for many tables in a single call. A total of 25 requests are allowed.
There are no transaction guarantees provided by this API. It does not allow conditional puts nor does it support return values.
C# |
BatchWriteItemResponse BatchWriteItem( BatchWriteItemRequest batchWriteItemRequest )
- batchWriteItemRequest (BatchWriteItemRequest)
- Container for the necessary parameters to execute the BatchWriteItem service method on AmazonDynamoDB.
The following examples show how to batch items into two tables.
This example will construct a batch-write collection for the first table in the request. The request will include two Put operations and one Delete operation.
// Create items to put into first table Dictionary<string, AttributeValue> item1 = new Dictionary<string,AttributeValue>(); item1["Author"] = new AttributeValue { S = "Mark Twain" }; item1["Title"] = new AttributeValue { S = "A Connecticut Yankee in King Arthur's Court" }; item1["Pages"] = new AttributeValue { N = "575" }; Dictionary<string, AttributeValue> item2 = new Dictionary<string, AttributeValue>(); item2["Author"] = new AttributeValue { S = "Booker Taliaferro Washington" }; item2["Title"] = new AttributeValue { S = "My Larger Education" }; item2["Pages"] = new AttributeValue { N = "313" }; item2["Year"] = new AttributeValue { N = "1911" }; // Create key for item to delete from first table // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "Tom Sawyer, Detective" Key keyToDelete1 = new Key { HashKeyElement = new AttributeValue { S = "Mark Twain" }, RangeKeyElement = new AttributeValue { S = "Tom Sawyer, Detective" } }; // Construct write-request for first table List<WriteRequest> sampleTableItems = new List<WriteRequest>(); sampleTableItems.Add(new WriteRequest { PutRequest = new PutRequest { Item = item1 } }); sampleTableItems.Add(new WriteRequest { PutRequest = new PutRequest { Item = item2 } }); sampleTableItems.Add(new WriteRequest { DeleteRequest = new DeleteRequest { Key = keyToDelete1 } });
This example will construct a batch-write collection for the second table in the request. The request will include one Delete operation.
// Create key for item to delete from second table // Hash-key of the target item is string value "Francis Scott Key Fitzgerald" Key keyToDelete2 = new Key { HashKeyElement = new AttributeValue { S = "Francis Scott Key Fitzgerald" }, }; // Construct write-request for first table List<WriteRequest> authorsTableItems = new List<WriteRequest>(); authorsTableItems.Add(new WriteRequest { DeleteRequest = new DeleteRequest { Key = keyToDelete2 } });
This example will construct the BatchWrite request from the two earlier-created collections, will issue the call and in case some items are not processed, will attempt to write the remaining items.
// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Construct table-keys mapping Dictionary<string, List<WriteRequest>> requestItems = new Dictionary<string, List<WriteRequest>>(); BatchWriteItemRequest request = new BatchWriteItemRequest(); requestItems["SampleTable"] = sampleTableItems; requestItems["AuthorsTable"] = authorsTableItems; BatchWriteItemResult result; do { // Issue request and retrieve items result = client.BatchWriteItem(request).BatchWriteItemResult; // Some items may not have been processed! // Set RequestItems to the result's UnprocessedItems and reissue request request.RequestItems = result.UnprocessedItems; } while (result.UnprocessedItems.Count > 0);