Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

示例:使用适用于 .NET 的 AWS 开发工具包低级 API 的批量操作

本节介绍 DynamoDB 支持的批量操作、批量写入和批量获取的示例。

示例:使用适用于 .NET 的 AWS 开发工具包低级 API 的批量写入操作

以下 C# 代码示例使用 BatchWriteItem 方法执行以下放置和删除操作:

  • 在 Forum 表中放置一个项目

  • 在 Thread 表中放置一个项目并删除一个项目。

在创建批量写入请求时,您可以就一个或多个表指定任意数量的放置和删除请求。但是,DynamoDB BatchWriteItem 对批量写入请求的大小,以及单个批量写入操作中的放置和删除操作数量有限制。有关更多信息,请参阅 BatchWriteItem。如果您的请求超出这些限制,请求会遭到拒绝。如果您的表的预置吞吐量不足,无法处理此请求,那么响应将返回未处理的请求项目。

以下示例查看响应,了解响应是否包含任何未处理的请求项目。如果存在,则循环返回,并重新发送包含请求中的未处理项目的 BatchWriteItem 请求。如果您执行创建表并加载示例数据中的步骤,则您应已创建 Forum 和 Thread 表。您还能够以编程方式创建这些示例表和上传示例数据。有关更多信息,请参阅 使用适用于 .NET 的 AWS 开发工具包创建示例表和上传数据

有关测试以下示例的分步说明,请参阅.NET 代码示例

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

示例:使用适用于 .NET 的 AWS 开发工具包低级 API 的批量获取操作

以下 C# 代码示例使用 BatchGetItem 方法检索 Forum 和 Thread 表中的多个项目。BatchGetItemRequest 指定表名称和每个表的主键列表。示例介绍通过打印检索到的项目来处理响应。

如果您执行创建表并加载示例数据中的步骤,则您应已利用示例数据创建这些表。您还能够以编程方式创建这些示例表和上传示例数据。有关更多信息,请参阅 使用适用于 .NET 的 AWS 开发工具包创建示例表和上传数据

有关测试以下示例的分步说明,请参阅.NET 代码示例

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