我找不到用swift编写dynamodb查询以获取具有特定分区键的结果的方法。
例如,假设我的分区键是“bigCompany”,排序键是“email”。现在我想把他们的“大公司”名为“xyz”的所有电子邮件都取出来。
作为参考,我的代码结构与下面的加载函数非常相似。但是这个函数使用.load
来获取一个值,而不是查询。基本上,我需要找到一种方法来调用dynamoDBOBjectMapper .query()
,考虑到上面提到的约束。任何帮助都将不胜感激!
func getTableRow() {
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
//tableRow?.UserId --> (tableRow?.UserId)!
dynamoDBObjectMapper .load(DDBTableRow.self, hashKey: (tableRow?.UserId)!, rangeKey: tableRow?.GameTitle) .continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task:AWSTask!) -> AnyObject! in
if (task.error == nil) {
if (task.result != nil) {
let tableRow = task.result as! DDBTableRow
self.hashKeyTextField.text = tableRow.UserId
self.rangeKeyTextField.text = tableRow.GameTitle
self.attribute1TextField.text = tableRow.TopScore?.stringValue
self.attribute2TextField.text = tableRow.Wins?.stringValue
self.attribute3TextField.text = tableRow.Losses?.stringValue
}
} else {
print("Error: \(task.error)")
let alertController = UIAlertController(title: "Failed to get item from table.", message: task.error!.description, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: { (action:UIAlertAction) -> Void in
})
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
return nil
})
}
最佳答案
明白了。他们的键正确地使用了keyConditionExpression。下面是我的答案。
let queryExpression = AWSDynamoDBQueryExpression()
queryExpression.keyConditionExpression = "#bigCompany = :bigCompany"
queryExpression.expressionAttributeNames = ["#bigCompany": "bigCompany",]
queryExpression.expressionAttributeValues = [":bigCompany" : self.bigCompany,]
dynamoDBObjectMapper.query(Requests.self, expression: queryExpression) .continue(with: AWSExecutor.immediate(), with: { (task:AWSTask!) -> AnyObject! in
//If added successfully
if((task.result) != nil) {
let results = task.result! as AWSDynamoDBPaginatedOutput
print(results.items)
}
else {
//do error checking here
}
})
关于ios - 如何快速编写dynamodb查询以使用特定分区键获取所有值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41396001/