gpt4 book ai didi

ios - 如何快速编写 dynamodb 查询以获取具有特定分区键的所有值

转载 作者:行者123 更新时间:2023-11-28 12:33:50 24 4
gpt4 key购买 nike

我无法找到一种方法来快速编写 dynamodb 查询来获取具有特定分区键的结果。

例如,假设我的分区键是“bigCompany”,排序键是“email”。现在我想获取所有名称为“bigCompany”的电子邮件为“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com