使用 Amazon SDK for .NET 对象持久化模型的批处理操作 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 对象持久化模型的批处理操作

批处理写入:放置和删除多个项目

要在单个请求中放置或删除表中的多个对象,请执行以下操作:

  • 运行 DynamoDBContextcreateBatchWrite 方法,然后创建 BatchWrite 类实例。

  • 指定要放置或删除的项目。

    • 要放置一个或多个项目,请使用 AddPutItemAddPutItems 方法。

    • 要删除一个或多个项目,可以指定项目的主键或映射到要删除的项目的客户端对象。使用AddDeleteItemAddDeleteItems,以及AddDeleteKey方法来指定要删除的项目列表。

  • 调用BatchWrite.Execute方法从表中放置和删除所有指定项目。

注意

使用对象持久化模型时,您可以指定批处理中任意数量的操作。但是,注意 Amazon DynamoDB 限制了批处理操作的数量以及批处理操作中批处理的总大小。有关具体限制的更多信息,请参阅 BatchWriteItem。如果 API 检测到您的批处理写入请求超出容许的写入请求数量,或者最大允许的 HTTP 负载大小,它就会将批处理划分为多个较小的批处理。此外,如果写入的响应返回了未处理的项目,API 将会就这些未处理的项目自动发送另一个批请求。

假设您定义了一个 C# 类 Book 类,该类映射到 DynamoDB 的 ProductCatalog 表。下面的 C# 代码示例使用 BatchWrite 对象上传两个项目并删除 ProductCatalog 表的一个项目。

DynamoDBContext context = new DynamoDBContext(client); var bookBatch = context.CreateBatchWrite<Book>(); // 1. Specify two books to add. Book book1 = new Book { Id = 902, ISBN = "902-11-11-1111", ProductCategory = "Book", Title = "My book3 in batch write" }; Book book2 = new Book { Id = 903, ISBN = "903-11-11-1111", ProductCategory = "Book", Title = "My book4 in batch write" }; bookBatch.AddPutItems(new List<Book> { book1, book2 }); // 2. Specify one book to delete. bookBatch.AddDeleteKey(111); bookBatch.Execute();

要从多个表中放置或删除对象,请执行以下操作:

  • 为每种类型创建一个 BatchWrite 类实例,并指定要放置或删除或删除的项目,如上一部分所述。

  • 使用以下方法之一创建 MultiTableBatchWrite 实例:

    • 对运行之前步骤中创建的 BatchWrite 对象运行 Combine 方法。

    • 提供 BatchWrite 对象列表,创建 MultiTableBatchWrite 类型实例。

    • 运行 DynamoDBContextCreateMultiTableBatchWrite 方法,传入 BatchWrite 对象列表。

  • 调用 MultiTableBatchWriteExecute 方法,对各种表执行指定的放置和删除操作。

假设您定义了 ForumThread C# 类,映射到 DynamoDB 的 ForumThread 表。另外,假设 Thread 类已启用版本控制。由于在使用批处理操作时不支持版本控制,因此您必须显式禁用版本控制,如以下 C# 代码示例所示。该示例使用 MultiTableBatchWrite对象执行多个表更新。

DynamoDBContext context = new DynamoDBContext(client); // Create BatchWrite objects for each of the Forum and Thread classes. var forumBatch = context.CreateBatchWrite<Forum>(); DynamoDBOperationConfig config = new DynamoDBOperationConfig(); config.SkipVersionCheck = true; var threadBatch = context.CreateBatchWrite<Thread>(config); // 1. New Forum item. Forum newForum = new Forum { Name = "Test BatchWrite Forum", Threads = 0 }; forumBatch.AddPutItem(newForum); // 2. Specify a forum to delete by specifying its primary key. forumBatch.AddDeleteKey("Some forum"); // 3. New Thread item. Thread newThread = new Thread { ForumName = "Amazon S3 forum", Subject = "My sample question", KeywordTags = new List<string> { "Amazon S3", "Bucket" }, Message = "Message text" }; threadBatch.AddPutItem(newThread); // Now run multi-table batch write. var superBatch = new MultiTableBatchWrite(forumBatch, threadBatch); superBatch.Execute();

要了解可工作的示例,请参阅 示例:使用 Amazon SDK for .NET 对象持久化模型的批处理写入操作

注意

DynamoDB 批处理 API 限制批处理写入的数量,还限制了批处理的大小。有关更多信息,请参阅BatchWriteItem。使用 .NET 对象持久化模型 API 时,您可以指定任意数量的操作。但是,如果批处理中的操作数或大小超过限制,.NET API 将批处理写入请求分成较小的批处理,并将多个批处理写入请求发送到 DynamoDB。

批处理获取:获取多个项目

要在单个请求中从表中检索多个项目,请执行以下操作:

  • 创建 CreateBatchGet 类的实例。

  • 指定要检索的主键列表。

  • 调用 Execute 方法。该响应返回 Results 属性的项目。

以下 C# 代码示例从 ProductCatalog 表检索三个项目。结果中的项目不一定与指定主键的顺序相同。

DynamoDBContext context = new DynamoDBContext(client); var bookBatch = context.CreateBatchGet<ProductCatalog>(); bookBatch.AddKey(101); bookBatch.AddKey(102); bookBatch.AddKey(103); bookBatch.Execute(); // Process result. Console.WriteLine(bookBatch.Results.Count); Book book1 = bookBatch.Results[0]; Book book2 = bookBatch.Results[1]; Book book3 = bookBatch.Results[2];

要从多个表中检索对象,请执行以下操作:

  • 对于每种类型,创建 CreateBatchGet 类型,并提供要从每个表中检索的主键值。

  • 使用以下方法之一创建 MultiTableBatchGet 类实例:

    • 对之前步骤中创建的 BatchGet 对象运行 Combine 方法。

    • 提供 BatchGet 对象列表,创建 MultiBatchGet 类型实例。

    • 运行 DynamoDBContextCreateMultiTableBatchGet 方法,传入 BatchGet 对象列表。

  • 调用 MultiTableBatchGetExecute 方法,返回单个 BatchGet 对象类型结果。

以下 C# 代码示例使用 CreateBatchGet 方法检索 OrderOrderDetail 表中的多个项目。

var orderBatch = context.CreateBatchGet<Order>(); orderBatch.AddKey(101); orderBatch.AddKey(102); var orderDetailBatch = context.CreateBatchGet<OrderDetail>(); orderDetailBatch.AddKey(101, "P1"); orderDetailBatch.AddKey(101, "P2"); orderDetailBatch.AddKey(102, "P3"); orderDetailBatch.AddKey(102, "P1"); var orderAndDetailSuperBatch = orderBatch.Combine(orderDetailBatch); orderAndDetailSuperBatch.Execute(); Console.WriteLine(orderBatch.Results.Count); Console.WriteLine(orderDetailBatch.Results.Count); Order order1 = orderBatch.Results[0]; Order order2 = orderBatch.Results[1]; OrderDetail orderDetail1 = orderDetailBatch.Results[0];