問(wèn)題描述
是否可以在 AWS 版本 2 中使用非關(guān)鍵屬性過(guò)濾 DynamoDB 查詢?亞馬遜表示他們可以做到:http://amzn.to/1FVgQ9B.但是他們也提供 API 嗎?我找到了 AWSDynamoDBQueryExpression,但我認(rèn)為它只允許過(guò)濾范圍鍵(沒(méi)有足夠的文檔).我正在 iOS 和 AWS 版本 2 中尋找合適的 API.謝謝!
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!
推薦答案
我正在回答我自己的問(wèn)題.這也是我在 AWS 支持論壇上發(fā)布的內(nèi)容:
I'm answering my own question. This is what I posted on AWS support forum as well:
您無(wú)法使用高級(jí) API -- AWSDynamoDBObjectMapper 來(lái)執(zhí)行此操作.使用 AWSDynamoDBObjectMapper 時(shí),需要向查詢方法提供一個(gè) AWSDynamoDBQueryExpression 對(duì)象來(lái)指定查詢條件.AWSDynamoDBQueryExpression 沒(méi)有為您提供在非關(guān)鍵屬性上設(shè)置過(guò)濾器(條件)的選項(xiàng).我想知道為什么不支持這個(gè)!但是,AWSDynamoDBScanExpression 允許您在使用掃描方法時(shí)指定非關(guān)鍵屬性的條件.但是,當(dāng)您真正的意思是查詢時(shí),您不想掃描.
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.
幸運(yùn)的是,您可以使用低級(jí)別 API 執(zhí)行此操作,方法是直接在 AWSDynamoDB 上調(diào)用查詢,提供 AWSDynamoDBQueryInput,讓您可以指定許多低級(jí)別參數(shù).AWSDynamoDBQueryInput 允許您使用 queryFilter 或 filterExpression 指定非鍵屬性的過(guò)濾條件.不推薦使用 queryFilter,建議使用 filterExpression.以下是幫助我解決這個(gè)問(wèn)題的兩個(gè)文件:
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.htmlhttp://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSDynamoDBQueryInput.html
這是 swift 中的代碼示例.在此代碼中,我基于作為非關(guān)鍵屬性的已批準(zhǔn)"字段進(jìn)行過(guò)濾.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
}
}
這篇關(guān)于在非關(guān)鍵屬性上查詢 DynamoDB的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!