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

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

示例:使用 Amazon SDK for .NET 文档模型 API 的批量操作

示例:使用 Amazon SDK for .NET 文档模型的批量写入操作

以下 C# 代码示例说明一个表和多个表的批量写入操作。该示例执行以下任务:

  • 演示单个表的批量写入。它向 ProductCatalog 表中添加两个项目。

  • 演示多个表的批量写入。它向 ForumThread 表添加一个项目,并从 Thread 表中删除一个项目。

如果您执行了为 DynamoDB 中的代码示例创建表和加载数据中的步骤,则您应已创建 ProductCatalogForumThread 表。您还能够以编程方式创建这些样本表。有关更多信息,请参见 创建示例表并使用 Amazon SDK for .NET 上传数据。有关测试以下示例的 step-by-step 说明,请参阅.NET 代码示例

using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DocumentModel; using Amazon.Runtime; namespace com.amazonaws.codesamples { class MidLevelBatchWriteItem { private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { SingleTableBatchWrite(); MultiTableBatchWrite(); } catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); } 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 SingleTableBatchWrite() { Table productCatalog = Table.LoadTable(client, "ProductCatalog"); var batchWrite = productCatalog.CreateBatchWrite(); var book1 = new Document(); book1["Id"] = 902; book1["Title"] = "My book1 in batch write using .NET helper classes"; book1["ISBN"] = "902-11-11-1111"; book1["Price"] = 10; book1["ProductCategory"] = "Book"; book1["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" }; book1["Dimensions"] = "8.5x11x.5"; book1["InStock"] = new DynamoDBBool(true); book1["QuantityOnHand"] = new DynamoDBNull(); //Quantity is unknown at this time batchWrite.AddDocumentToPut(book1); // Specify delete item using overload that takes PK. batchWrite.AddKeyToDelete(12345); Console.WriteLine("Performing batch write in SingleTableBatchWrite()"); batchWrite.Execute(); } private static void MultiTableBatchWrite() { // 1. Specify item to add in the Forum table. Table forum = Table.LoadTable(client, "Forum"); var forumBatchWrite = forum.CreateBatchWrite(); var forum1 = new Document(); forum1["Name"] = "Test BatchWrite Forum"; forum1["Threads"] = 0; forumBatchWrite.AddDocumentToPut(forum1); // 2a. Specify item to add in the Thread table. Table thread = Table.LoadTable(client, "Thread"); var threadBatchWrite = thread.CreateBatchWrite(); var thread1 = new Document(); thread1["ForumName"] = "S3 forum"; thread1["Subject"] = "My sample question"; thread1["Message"] = "Message text"; thread1["KeywordTags"] = new List<string> { "S3", "Bucket" }; threadBatchWrite.AddDocumentToPut(thread1); // 2b. Specify item to delete from the Thread table. threadBatchWrite.AddKeyToDelete("someForumName", "someSubject"); // 3. Create multi-table batch. var superBatch = new MultiTableDocumentBatchWrite(); superBatch.AddBatch(forumBatchWrite); superBatch.AddBatch(threadBatchWrite); Console.WriteLine("Performing batch write in MultiTableBatchWrite()"); superBatch.Execute(); } } }