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

步骤 8:查询 Movies 表

Microsoft .NET 和 DynamoDB 教程的本步骤中,您将以三种不同的方式查询 Movies 表。

使用简单的文档模型搜索来查询 1985 年的电影

为了设置简单的文档模型查询,DynamoDB_intro 中的 Main 通过调用 Table.Query API(同时将 1985 作为分区键(也称为哈希键))创建了一个 Search 对象,并创建了一个空筛选条件 Expression

try { search = moviesTable.Query( 1985, new Expression( ) ); }

它随后等待 SearchListing_async,后者在 08_Querying.cs 中实施以检索和显示查询结果:

using System; using System.Text; using System.Threading.Tasks; using Amazon.DynamoDBv2.Model; using Amazon.DynamoDBv2.DocumentModel; using System.Collections.Generic; namespace DynamoDB_intro { public static partial class Ddb_Intro { /*-------------------------------------------------------------------------- * SearchListing_async *--------------------------------------------------------------------------*/ public static async Task<bool> SearchListing_async( Search search ) { int i = 0; List<Document> docList = new List<Document>( ); Console.WriteLine( " Here are the movies retrieved:\n" + " --------------------------------------------------------------------------" ); Task<List<Document>> getNextBatch; operationSucceeded = false; operationFailed = false; do { try { getNextBatch = search.GetNextSetAsync( ); docList = await getNextBatch; } catch( Exception ex ) { Console.WriteLine( " FAILED to get the next batch of movies from Search! Reason:\n " + ex.Message ); operationFailed = true; return ( false ); } foreach( Document doc in docList ) { i++; showMovieDocShort( doc ); } } while( !search.IsDone ); Console.WriteLine( " -- Retrieved {0} movies.", i ); operationSucceeded = true; return ( true ); } /*-------------------------------------------------------------------------- * ClientQuerying_async *--------------------------------------------------------------------------*/ public static async Task<bool> ClientQuerying_async( QueryRequest qRequest ) { operationSucceeded = false; operationFailed = false; QueryResponse qResponse; try { Task<QueryResponse> clientQueryTask = client.QueryAsync( qRequest ); qResponse = await clientQueryTask; } catch( Exception ex ) { Console.WriteLine( " The low-level query FAILED, because:\n {0}.", ex.Message ); operationFailed = true; return ( false ); } Console.WriteLine( " -- The low-level query succeeded, and returned {0} movies!", qResponse.Items.Count ); if( !pause( ) ) { operationFailed = true; return ( false ); } Console.WriteLine( " Here are the movies retrieved:" + " --------------------------------------------------------------------------" ); foreach( Dictionary<string, AttributeValue> item in qResponse.Items ) showMovieAttrsShort( item ); Console.WriteLine( " -- Retrieved {0} movies.", qResponse.Items.Count ); operationSucceeded = true; return ( true ); } } }

使用 QueryOperationConfig 创建更复杂的查询搜索

为了查询 1992 年的名称从“B...”到“Hzz...”的电影,Main 函数创建了一个包含 QueryFilter 和各种其他字段的 QueryOperationConfig 对象:

QueryOperationConfig config = new QueryOperationConfig( ); config.Filter = new QueryFilter( ); config.Filter.AddCondition( "year", QueryOperator.Equal, new DynamoDBEntry[ ] { 1992 } ); config.Filter.AddCondition( "title", QueryOperator.Between, new DynamoDBEntry[ ] { "B", "Hzz" } ); config.AttributesToGet = new List<string> { "year", "title", "info" }; config.Select = SelectValues.SpecificAttributes;

然后,它再次通过调用 Table.Query API 创建了一个 Search 对象,这一次将 QueryOperationConfig 对象作为唯一的参数。

然后,它将再次等待 SearchListing_async 检索和显示查询结果。

使用低级别查询查找 1992 年的名称在“M...”和“Tzz...”之间的电影

为了使用低级别查询检索 1992 年的名称从“M...”到“Tzz...”的电影,Main 函数创建了一个 QueryRequest 对象:

QueryRequest qRequest= new QueryRequest { TableName = "Movies", ExpressionAttributeNames = new Dictionary<string, string> { { "#yr", "year" } }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":qYr", new AttributeValue { N = "1992" } }, { ":tSt", new AttributeValue { S = "M" } }, { ":tEn", new AttributeValue { S = "Tzz" } } }, KeyConditionExpression = "#yr = :qYr and title between :tSt and :tEn", ProjectionExpression = "#yr, title, info.actors[0], info.genres, info.running_time_secs" };

该对象随后等待 ClientQuerying_async 函数在 08_Querying.cs 文件中实施。ClientQuerying_async 转而等待低级别 DynamoDB 方法 AmazonDynamoDBClient.QueryAsync 检索查询结果。

注意

由于“year”是 DynamoDB 中的保留字,您需要使用 ExpressionAttributeNames 为它创建一个别名(此处为 #yr)以在低级别表达式中使用它。

下一步

步骤 9:扫描 Movies 表