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

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

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

下面的 C# 代码示例声明 BookForumThreadReply 类,并使用对象持久化模型属性将它们映射到 Amazon DynamoDB 表。

该示例然后使用 DynamoDBContext 说明以下批量写入操作:

  • BatchWrite 对象放置和删除 ProductCatalog 表的图书项目。

  • MultiTableBatchWrite 对象放置和删除 ForumThread 表的项目。

有关此示例中使用的表的更多信息,请参阅为 DynamoDB 中的代码示例创建表和加载数据。有关测试以下示例的 step-by-step 说明,请参阅.NET 代码示例

注意

下面的示例不适用于 .NET 核心,因为它不支持同步方法。有关更多信息,请参阅适用于 .NET 的 Amazon 异步 API

using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DataModel; using Amazon.Runtime; using Amazon.SecurityToken; namespace com.amazonaws.codesamples { class HighLevelBatchWriteItem { private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { DynamoDBContext context = new DynamoDBContext(client); SingleTableBatchWrite(context); MultiTableBatchWrite(context); } 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(DynamoDBContext context) { Book book1 = new Book { Id = 902, InPublication = true, ISBN = "902-11-11-1111", PageCount = "100", Price = 10, ProductCategory = "Book", Title = "My book3 in batch write" }; Book book2 = new Book { Id = 903, InPublication = true, ISBN = "903-11-11-1111", PageCount = "200", Price = 10, ProductCategory = "Book", Title = "My book4 in batch write" }; var bookBatch = context.CreateBatchWrite<Book>(); bookBatch.AddPutItems(new List<Book> { book1, book2 }); Console.WriteLine("Performing batch write in SingleTableBatchWrite()."); bookBatch.Execute(); } private static void MultiTableBatchWrite(DynamoDBContext context) { // 1. New Forum item. Forum newForum = new Forum { Name = "Test BatchWrite Forum", Threads = 0 }; var forumBatch = context.CreateBatchWrite<Forum>(); forumBatch.AddPutItem(newForum); // 2. New Thread item. Thread newThread = new Thread { ForumName = "S3 forum", Subject = "My sample question", KeywordTags = new List<string> { "S3", "Bucket" }, Message = "Message text" }; DynamoDBOperationConfig config = new DynamoDBOperationConfig(); config.SkipVersionCheck = true; var threadBatch = context.CreateBatchWrite<Thread>(config); threadBatch.AddPutItem(newThread); threadBatch.AddDeleteKey("some partition key value", "some sort key value"); var superBatch = new MultiTableBatchWrite(forumBatch, threadBatch); Console.WriteLine("Performing batch write in MultiTableBatchWrite()."); superBatch.Execute(); } } [DynamoDBTable("Reply")] public class Reply { [DynamoDBHashKey] //Partition key public string Id { get; set; } [DynamoDBRangeKey] //Sort key public DateTime ReplyDateTime { get; set; } // Properties included implicitly. public string Message { get; set; } // Explicit property mapping with object persistence model attributes. [DynamoDBProperty("LastPostedBy")] public string PostedBy { get; set; } // Property to store version number for optimistic locking. [DynamoDBVersion] public int? Version { get; set; } } [DynamoDBTable("Thread")] public class Thread { // PK mapping. [DynamoDBHashKey] //Partition key public string ForumName { get; set; } [DynamoDBRangeKey] //Sort key public String Subject { get; set; } // Implicit mapping. public string Message { get; set; } public string LastPostedBy { get; set; } public int Views { get; set; } public int Replies { get; set; } public bool Answered { get; set; } public DateTime LastPostedDateTime { get; set; } // Explicit mapping (property and table attribute names are different. [DynamoDBProperty("Tags")] public List<string> KeywordTags { get; set; } // Property to store version number for optimistic locking. [DynamoDBVersion] public int? Version { get; set; } } [DynamoDBTable("Forum")] public class Forum { [DynamoDBHashKey] //Partition key public string Name { get; set; } // All the following properties are explicitly mapped, // only to show how to provide mapping. [DynamoDBProperty] public int Threads { get; set; } [DynamoDBProperty] public int Views { get; set; } [DynamoDBProperty] public string LastPostBy { get; set; } [DynamoDBProperty] public DateTime LastPostDateTime { get; set; } [DynamoDBProperty] public int Messages { get; set; } } [DynamoDBTable("ProductCatalog")] public class Book { [DynamoDBHashKey] //Partition key public int Id { get; set; } public string Title { get; set; } public string ISBN { get; set; } public int Price { get; set; } public string PageCount { get; set; } public string ProductCategory { get; set; } public bool InPublication { get; set; } } }