Amazon DynamoDB
开发人员指南 (API 版本 2012-08-10)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

步骤 5:从 Movies 表中读取并显示一个记录

Microsoft .NET 和 DynamoDB 教程的本步骤中,您将检索并显示您在步骤 4 中添加的新电影记录。DynamoDB_intro 中的 Main 函数通过等待在 05_ReadingItem.cs 文件中实施的 ReadingMovie_async 来执行此操作:

using System; using System.Threading.Tasks; using Amazon.DynamoDBv2.DocumentModel; namespace DynamoDB_intro { public static partial class Ddb_Intro { /*-------------------------------------------------------------------------- * ReadingMovie_async *--------------------------------------------------------------------------*/ public static async Task<bool> ReadingMovie_async( int year, string title, bool report ) { // Create Primitives for the HASH and RANGE portions of the primary key Primitive hash = new Primitive(year.ToString(), true); Primitive range = new Primitive(title, false); operationSucceeded = false; operationFailed = false; try { Task<Document> readMovie = moviesTable.GetItemAsync(hash, range, token); if( report ) Console.WriteLine( " -- Reading the {0} movie \"{1}\" from the Movies table...", year, title ); movie_record = await readMovie; if( movie_record == null ) { if( report ) Console.WriteLine( " -- Sorry, that movie isn't in the Movies table." ); return ( false ); } else { if( report ) Console.WriteLine( " -- Found it! The movie record looks like this:\n" + movie_record.ToJsonPretty( ) ); operationSucceeded = true; return ( true ); } } catch( Exception ex ) { Console.WriteLine( " FAILED to get the movie, because: {0}.", ex.Message ); operationFailed = true; } return ( false ); } } }

ReadingMovie_async 转而等待 DynamoDB Table.GetItemAsyn 方法将新电影记录作为 Document 检索。ReadingMovie_async 随后使用 Document.ToJsonPretty 方法将电影作为 JSON 文本显示。

了解更多信息

下一步

步骤 6:更新新电影记录

本页内容: