利用 AWS SDK 使用 DynamoDB 的高级对象持久化模型 - Amazon DynamoDB

利用 AWS SDK 使用 DynamoDB 的高级对象持久化模型

以下代码示例显示如何使用 DynamoDB 的对象持久化模型和 AWS SDK 来执行创建、读取、更新和删除 (CRUD) 及批处理操作。

有关更多信息,请参阅对象持久化模型

.NET
AWS SDK for .NET
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

使用高级对象持久化模型执行 CRUD 操作。

/// <summary> /// Shows how to perform high-level CRUD operations on an Amazon DynamoDB /// table. /// </summary> public class HighLevelItemCrud { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await PerformCRUDOperations(context); } public static async Task PerformCRUDOperations(IDynamoDBContext context) { int bookId = 1001; // Some unique value. Book myBook = new Book { Id = bookId, Title = "object persistence-AWS SDK for.NET SDK-Book 1001", Isbn = "111-1111111001", BookAuthors = new List<string> { "Author 1", "Author 2" }, }; // Save the book to the ProductCatalog table. await context.SaveAsync(myBook); // Retrieve the book from the ProductCatalog table. Book bookRetrieved = await context.LoadAsync<Book>(bookId); // Update some properties. bookRetrieved.Isbn = "222-2222221001"; // Update existing authors list with the following values. bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; await context.SaveAsync(bookRetrieved); // Retrieve the updated book. This time, add the optional // ConsistentRead parameter using DynamoDBContextConfig object. await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); // Delete the book. await context.DeleteAsync<Book>(bookId); // Try to retrieve deleted book. It should return null. Book deletedBook = await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); if (deletedBook == null) { Console.WriteLine("Book is deleted"); } } }

使用高级对象持久化模型执行批量写入操作。

/// <summary> /// Performs high-level batch write operations to an Amazon DynamoDB table. /// This example was written using the AWS SDK for .NET version 3.7 and .NET /// Core 5.0. /// </summary> public class HighLevelBatchWriteItem { public static async Task SingleTableBatchWrite(IDynamoDBContext 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("Adding two books to ProductCatalog table."); await bookBatch.ExecuteAsync(); } public static async Task MultiTableBatchWrite(IDynamoDBContext context) { // New Forum item. Forum newForum = new Forum { Name = "Test BatchWrite Forum", Threads = 0, }; var forumBatch = context.CreateBatchWrite<Forum>(); forumBatch.AddPutItem(newForum); // 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()."); await superBatch.ExecuteAsync(); } public static async Task Main() { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await SingleTableBatchWrite(context); await MultiTableBatchWrite(context); } }

使用高级对象持久化模型将任意数据映射为表。

/// <summary> /// Shows how to map arbitrary data to an Amazon DynamoDB table. /// </summary> public class HighLevelMappingArbitraryData { /// <summary> /// Creates a book, adds it to the DynamoDB ProductCatalog table, retrieves /// the new book from the table, updates the dimensions and writes the /// changed item back to the table. /// </summary> /// <param name="context">The DynamoDB context object used to write and /// read data from the table.</param> public static async Task AddRetrieveUpdateBook(IDynamoDBContext context) { // Create a book. DimensionType myBookDimensions = new DimensionType() { Length = 8M, Height = 11M, Thickness = 0.5M, }; Book myBook = new Book { Id = 501, Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data", Isbn = "999-9999999999", BookAuthors = new List<string> { "Author 1", "Author 2" }, Dimensions = myBookDimensions, }; // Add the book to the DynamoDB table ProductCatalog. await context.SaveAsync(myBook); // Retrieve the book. Book bookRetrieved = await context.LoadAsync<Book>(501); // Update the book dimensions property. bookRetrieved.Dimensions.Height += 1; bookRetrieved.Dimensions.Length += 1; bookRetrieved.Dimensions.Thickness += 0.2M; // Write the changed item to the table. await context.SaveAsync(bookRetrieved); } public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await AddRetrieveUpdateBook(context); } }

使用高级对象持久化模型查询和扫描表。

/// <summary> /// Shows how to perform high-level query and scan operations to Amazon /// DynamoDB tables. /// </summary> public class HighLevelQueryAndScan { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); // Get an item. await GetBook(context, 101); // Sample forum and thread to test queries. string forumName = "Amazon DynamoDB"; string threadSubject = "DynamoDB Thread 1"; // Sample queries. await FindRepliesInLast15Days(context, forumName, threadSubject); await FindRepliesPostedWithinTimePeriod(context, forumName, threadSubject); // Scan table. await FindProductsPricedLessThanZero(context); } public static async Task GetBook(IDynamoDBContext context, int productId) { Book bookItem = await context.LoadAsync<Book>(productId); Console.WriteLine("\nGetBook: Printing result....."); Console.WriteLine($"Title: {bookItem.Title} \n ISBN:{bookItem.Isbn} \n No. of pages: {bookItem.PageCount}"); } /// <summary> /// Queries a DynamoDB table to find replies posted within the last 15 days. /// </summary> /// <param name="context">The DynamoDB context used to perform the query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadSubject">The thread object containing the query parameters.</param> public static async Task FindRepliesInLast15Days( IDynamoDBContext context, string forumName, string threadSubject) { string replyId = $"{forumName} #{threadSubject}"; DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15); List<object> times = new List<object>(); times.Add(twoWeeksAgoDate); List<ScanCondition> scs = new List<ScanCondition>(); var sc = new ScanCondition("PostedBy", ScanOperator.GreaterThan, times.ToArray()); scs.Add(sc); var cfg = new DynamoDBOperationConfig { QueryFilter = scs, }; AsyncSearch<Reply> response = context.QueryAsync<Reply>(replyId, cfg); IEnumerable<Reply> latestReplies = await response.GetRemainingAsync(); Console.WriteLine("\nReplies in last 15 days:"); foreach (Reply r in latestReplies) { Console.WriteLine($"{r.Id}\t{r.PostedBy}\t{r.Message}\t{r.ReplyDateTime}"); } } /// <summary> /// Queries for replies posted within a specific time period. /// </summary> /// <param name="context">The DynamoDB context used to perform the query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadSubject">Information about the subject that we're /// interested in.</param> public static async Task FindRepliesPostedWithinTimePeriod( IDynamoDBContext context, string forumName, string threadSubject) { string forumId = forumName + "#" + threadSubject; Console.WriteLine("\nReplies posted within time period:"); DateTime startDate = DateTime.UtcNow - TimeSpan.FromDays(30); DateTime endDate = DateTime.UtcNow - TimeSpan.FromDays(1); List<object> times = new List<object>(); times.Add(startDate); times.Add(endDate); List<ScanCondition> scs = new List<ScanCondition>(); var sc = new ScanCondition("LastPostedBy", ScanOperator.Between, times.ToArray()); scs.Add(sc); var cfg = new DynamoDBOperationConfig { QueryFilter = scs, }; AsyncSearch<Reply> response = context.QueryAsync<Reply>(forumId, cfg); IEnumerable<Reply> repliesInAPeriod = await response.GetRemainingAsync(); foreach (Reply r in repliesInAPeriod) { Console.WriteLine("{r.Id}\t{r.PostedBy}\t{r.Message}\t{r.ReplyDateTime}"); } } /// <summary> /// Queries the DynamoDB ProductCatalog table for products costing less /// than zero. /// </summary> /// <param name="context">The DynamoDB context object used to perform the /// query.</param> public static async Task FindProductsPricedLessThanZero(IDynamoDBContext context) { int price = 0; List<ScanCondition> scs = new List<ScanCondition>(); var sc1 = new ScanCondition("Price", ScanOperator.LessThan, price); var sc2 = new ScanCondition("ProductCategory", ScanOperator.Equal, "Book"); scs.Add(sc1); scs.Add(sc2); AsyncSearch<Book> response = context.ScanAsync<Book>(scs); IEnumerable<Book> itemsWithWrongPrice = await response.GetRemainingAsync(); Console.WriteLine("\nFindProductsPricedLessThanZero: Printing result....."); foreach (Book r in itemsWithWrongPrice) { Console.WriteLine($"{r.Id}\t{r.Title}\t{r.Price}\t{r.Isbn}"); } } }

有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 结合使用 DynamoDB 与 AWS SDK。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。