使用 SDK 為 DynamoDB 使用高階物件持續性模型 AWS - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK 為 DynamoDB 使用高階物件持續性模型 AWS

下列程式碼範例示範如何使用 DynamoDB 和 SDK 的物件持續性模型來執行建立、讀取、更新和刪除 (CRUD) 和批次作業。 AWS

如需詳細資訊,請參閱物件持久性模型

.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 的詳細資訊。