Example: Batch operations using the Amazon SDK for .NET low-level API
Topics
This section provides examples of batch operations, batch write and batch get, that Amazon DynamoDB supports.
Example: Batch write operation using the Amazon SDK for .NET low-level API
The following C# code example uses the BatchWriteItem
method to perform the
following put and delete operations:
-
Put one item in the
Forum
table. -
Put one item and delete one item from the
Thread
table.
You can specify any number of put and delete requests against one or more tables when
creating your batch write request. However, DynamoDB BatchWriteItem
limits the
size of a batch write request and the number of put and delete operations in a single batch
write operation. For more information, see BatchWriteItem. If your request exceeds
these limits, your request is rejected. If your table does not have sufficient provisioned
throughput to serve this request, the unprocessed request items are returned in the
response.
The following example checks the response to see if it has any unprocessed request
items. If it does, it loops back and resends the BatchWriteItem
request with
unprocessed items in the request. You can also create these sample tables and upload
sample data programmatically. For more information, see Creating example
tables and uploading data using the Amazon SDK for .NET.
For step-by-step instructions for testing the following sample, see .NET code examples.
Example
using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.Runtime; namespace com.amazonaws.codesamples { class LowLevelBatchWrite { private static string table1Name = "Forum"; private static string table2Name = "Thread"; private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { TestBatchWrite(); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } private static void TestBatchWrite() { var request = new BatchWriteItemRequest { ReturnConsumedCapacity = "TOTAL", RequestItems = new Dictionary<string, List<WriteRequest>> { { table1Name, new List<WriteRequest> { new WriteRequest { PutRequest = new PutRequest { Item = new Dictionary<string, AttributeValue> { { "Name", new AttributeValue { S = "S3 forum" } }, { "Threads", new AttributeValue { N = "0" }} } } } } }, { table2Name, new List<WriteRequest> { new WriteRequest { PutRequest = new PutRequest { Item = new Dictionary<string, AttributeValue> { { "ForumName", new AttributeValue { S = "S3 forum" } }, { "Subject", new AttributeValue { S = "My sample question" } }, { "Message", new AttributeValue { S = "Message Text." } }, { "KeywordTags", new AttributeValue { SS = new List<string> { "S3", "Bucket" } } } } } }, new WriteRequest { // For the operation to delete an item, if you provide a primary key value // that does not exist in the table, there is no error, it is just a no-op. DeleteRequest = new DeleteRequest { Key = new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Some partition key value" } }, { "Subject", new AttributeValue { S = "Some sort key value" } } } } } } } } }; CallBatchWriteTillCompletion(request); } private static void CallBatchWriteTillCompletion(BatchWriteItemRequest request) { BatchWriteItemResponse response; int callCount = 0; do { Console.WriteLine("Making request"); response = client.BatchWriteItem(request); callCount++; // Check the response. var tableConsumedCapacities = response.ConsumedCapacity; var unprocessed = response.UnprocessedItems; Console.WriteLine("Per-table consumed capacity"); foreach (var tableConsumedCapacity in tableConsumedCapacities) { Console.WriteLine("{0} - {1}", tableConsumedCapacity.TableName, tableConsumedCapacity.CapacityUnits); } Console.WriteLine("Unprocessed"); foreach (var unp in unprocessed) { Console.WriteLine("{0} - {1}", unp.Key, unp.Value.Count); } Console.WriteLine(); // For the next iteration, the request will have unprocessed items. request.RequestItems = unprocessed; } while (response.UnprocessedItems.Count > 0); Console.WriteLine("Total # of batch write API calls made: {0}", callCount); } } }
Example: Batch get operation using the Amazon SDK for .NET low-level API
The following C# code example uses the BatchGetItem
method to retrieve
multiple items from the Forum
and the Thread
tables in Amazon DynamoDB.
The BatchGetItemRequest
specifies the table names and a list of primary keys
for each table. The example processes the response by printing the items retrieved.
For step-by-step instructions for testing the following sample, see .NET code examples.
Example
using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.Runtime; namespace com.amazonaws.codesamples { class LowLevelBatchGet { private static string table1Name = "Forum"; private static string table2Name = "Thread"; private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { RetrieveMultipleItemsBatchGet(); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } } private static void RetrieveMultipleItemsBatchGet() { var request = new BatchGetItemRequest { RequestItems = new Dictionary<string, KeysAndAttributes>() { { table1Name, new KeysAndAttributes { Keys = new List<Dictionary<string, AttributeValue> >() { new Dictionary<string, AttributeValue>() { { "Name", new AttributeValue { S = "Amazon DynamoDB" } } }, new Dictionary<string, AttributeValue>() { { "Name", new AttributeValue { S = "Amazon S3" } } } } }}, { table2Name, new KeysAndAttributes { Keys = new List<Dictionary<string, AttributeValue> >() { new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon DynamoDB" } }, { "Subject", new AttributeValue { S = "DynamoDB Thread 1" } } }, new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon DynamoDB" } }, { "Subject", new AttributeValue { S = "DynamoDB Thread 2" } } }, new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon S3" } }, { "Subject", new AttributeValue { S = "S3 Thread 1" } } } } } } } }; BatchGetItemResponse response; do { Console.WriteLine("Making request"); response = client.BatchGetItem(request); // Check the response. var responses = response.Responses; // Attribute list in the response. foreach (var tableResponse in responses) { var tableResults = tableResponse.Value; Console.WriteLine("Items retrieved from table {0}", tableResponse.Key); foreach (var item1 in tableResults) { PrintItem(item1); } } // Any unprocessed keys? could happen if you exceed ProvisionedThroughput or some other error. Dictionary<string, KeysAndAttributes> unprocessedKeys = response.UnprocessedKeys; foreach (var unprocessedTableKeys in unprocessedKeys) { // Print table name. Console.WriteLine(unprocessedTableKeys.Key); // Print unprocessed primary keys. foreach (var key in unprocessedTableKeys.Value.Keys) { PrintItem(key); } } request.RequestItems = unprocessedKeys; } while (response.UnprocessedKeys.Count > 0); } private static void PrintItem(Dictionary<string, AttributeValue> attributeList) { foreach (KeyValuePair<string, AttributeValue> kvp in attributeList) { string attributeName = kvp.Key; AttributeValue value = kvp.Value; Console.WriteLine( attributeName + " " + (value.S == null ? "" : "S=[" + value.S + "]") + (value.N == null ? "" : "N=[" + value.N + "]") + (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") + (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]") ); } Console.WriteLine("************************************************"); } } }