简体   繁体   English

AWS DynamoDB iOS查询

[英]AWS DynamoDB iOS Query

I followed the tutorial from the official AWS guide, but what is not explained is how to query for secondary indexes, ones I created myself. 我遵循了AWS官方指南中的教程,但没有解释如何查询二级索引(我自己创建的二级索引)。 So what method would I call on queryExpression and how would I use the method (what would I do exactly if I have say three secondary indexes and they each should have a given value for the query)? 那么,我将在queryExpression上调用什么方法,以及如何使用该方法(如果我说了三个二级索引,并且每个二级索引都应具有查询的给定值,我该怎么办)?

From the AWS DynamoDB IOS developers Guide: 从《 AWS DynamoDB IOS开发人员指南》中:

http://docs.aws.amazon.com/mobile/sdkforios/developerguide/dynamodb_om.html http://docs.aws.amazon.com/mobile/sdkforios/developerguide/dynamodb_om.html

The Query API enables you to query a table or a secondary index. Query API使您可以查询表或二级索引。 You must provide a hash key value in AWSDynamoDBQueryExpression. 您必须在AWSDynamoDBQueryExpression中提供哈希键值。 To query an index, you must also specify the indexName. 要查询索引,您还必须指定indexName。 You must specify the hashKeyAttribute if you query a global secondary with a different hashKey. 如果使用其他hashKey查询全局辅助数据库,则必须指定hashKeyAttribute。 If the table or index has a range key, you can optionally refine the results by providing a range key value and a condition. 如果表或索引具有范围键,则可以选择提供范围键值和条件来优化结果。 The query:expression: method takes two parameters—the class of the resulting object and an instance of AWSDynamoDBQueryExpression. query:expression:方法采用两个参数-结果对象的类和AWSDynamoDBQueryExpression的实例。 In the following example, we query the Books index table to find all books with author of “John Smith” and price less than 50: 在以下示例中,我们查询“图书”索引表以查找作者为“ John Smith”且价格小于50的所有图书:

And it would be something like this: 这将是这样的:

AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];

queryExpression.indexName = @"Author-Price-index";

queryExpression.hashKeyAttribute = @"Author";
queryExpression.hashKeyValues = @"John Smith";

queryExpression.rangeKeyConditionExpression = @"Price < :val";
queryExpression.expressionAttributeValues = @{@":val":@50};

[[dynamoDBObjectMapper query:[Book class]
          expression:queryExpression]
continueWithBlock:^id(AWSTask *task) {
     if (task.error) {
         NSLog(@"The request failed. Error: [%@]", task.error);
     }
     if (task.exception) {
         NSLog(@"The request failed. Exception: [%@]", task.exception);
     }
     if (task.result) {
         AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
         for (Book *book in paginatedOutput.items) {
             //Do something with book.
         }
     }
     return nil;
 }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM