问题描述
是否可以过滤使用非关键属性在AWS版本2 DynamoDB查询?亚马逊表示,他们可以做到这一点: http://amzn.to/1FVgQ9B 。但是,他们也给了API?我发现AWSDynamoDBQueryEx pression,但我认为这只是让过滤的范围键(没有足够的文档)。我正在寻找在iOS和AWS版本正确的API 2.感谢!
Is it possible to filter DynamoDB queries using non-key attributes in AWS version 2 ? Amazon says that they can do it: http://amzn.to/1FVgQ9B. But do they also give the API? I found AWSDynamoDBQueryExpression, but I think it only lets filtering on the range key (not enough documentation). I'm looking for the proper API in iOS and AWS version 2. Thanks!
推荐答案
我回答我自己的问题。这是我张贴在AWS支持论坛以及:
I'm answering my own question. This is what I posted on AWS support forum as well:
您无法与高层次的API做到这一点 - AWSDynamoDBObjectMapper。当使用AWSDynamoDBObjectMapper,您需要提供一个AWSDynamoDBQueryEx pression对象的查询方法来指定查询条件。 AWSDynamoDBQueryEx pression不给你在非关键属性设置过滤器(条件)的选项。我不知道这是为什么不支持!然而,AWSDynamoDBScanEx pression让你指定的非关键属性的条件,当您使用的扫描方法。但你不希望扫描当你真正的意思是查询。
You can't do this with the high level API -- AWSDynamoDBObjectMapper. When using AWSDynamoDBObjectMapper, you need to provide an AWSDynamoDBQueryExpression object to the query method to specify the query conditions. AWSDynamoDBQueryExpression doesn't give you the option to set filters(conditions) on non-key attributes. I wonder why this isn't supported! However, AWSDynamoDBScanExpression lets you specify conditions on non-key attributes when you use the scan method. But you don't want to scan when you actually mean a query.
幸运的是,你可以使用低级别的API直接调用查询AWSDynamoDB提供AWSDynamoDBQueryInput它可以让你指定了很多低级别的参数做到这一点。 AWSDynamoDBQueryInput让你指定使用任何queryFilter或filterEx pression非关键属性过滤条件。 queryFilter是德precated,建议使用filterEx pression。这里有两个文件,帮助我摸不着头脑:
Fortunately, you can do this using the low level API by directly calling query on AWSDynamoDB providing an AWSDynamoDBQueryInput which lets you specify a lot of low level parameters. AWSDynamoDBQueryInput lets you specify the filter conditions on non-key attributes using either queryFilter or filterExpression. queryFilter is deprecated, it's recommended to use filterExpression.Here are the two documents that helped me to figure this out:
http://docs.aws.amazon.com/amazondynamodb/最新/ APIReference / API_Query.html 的
下面是在一个迅速code例子。在这种code根据我过滤批准字段是一个非关键属性。 recId是主键:
Here's a code example in swift. In this code I'm filtering based on "approved" field that is a non-key attribute. recId is the primary key:
func getApprovedRecords(recId: Int) {
let dynamoDB = AWSDynamoDB.defaultDynamoDB()
var startKey = nil
var queryInput = AWSDynamoDBQueryInput()
queryInput.tableName = TABLE_NAME
queryInput.limit = QUERY_SIZE
queryInput.exclusiveStartKey = startKey
var recIdValue = AWSDynamoDBAttributeValue()
recIdValue.N = String(recId)
var recIdCondition = AWSDynamoDBCondition()
recIdCondition.comparisonOperator = AWSDynamoDBComparisonOperator.EQ
recIdCondition.attributeValueList = [recIdValue]
queryInput.keyConditions = [ "recId"\" : recIdCondition]
var oneValue = AWSDynamoDBAttributeValue()
oneValue.N = "1"
queryInput.expressionAttributeValues = [ ":one" : oneValue ]
queryInput.filterExpression = "approved = :one"
dynamoDB.query(queryInput).continueWithBlock { (task: BFTask!) -> AnyObject! in
if ((task.error) != nil) {
NSLog("The request failed. Error: \(task.error)")
}
if ((task.exception) != nil) {
NSLog("The request failed. Exception: \(task.exception)")
}
if ((task.result) != nil) {
NSLog("The request succeeded.")
let results = task.result as! AWSDynamoDBQueryOutput
for r in results.items {
// do whatever with the result
}
}
return nil
}
}
这篇关于对非关键属性查询DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!