阅读背景:

仅使用swift中的范围键查询AWS DynamoDB服务器

来源:互联网 

I am trying to use a query function on an aws dynamodb server and return the value of an index using only its range key. Basically I want a user to be able to find other users using only this range key. My program generates a random code from one user, and the other user is supposed to be able to find that user without knowing their user id and just knowing their random code. At the moment, when I query, it only returns users matching the same user id as the device that is querying, so I am trying to query for just the random code (which is the range key). Here is my code:

我试图在aws dynamodb服务器上使用查询函数,并仅使用其范围键返回索引的值。基本上我希望用户能够仅使用此范围键找到其他用户。我的程序从一个用户生成随机代码,而另一个用户应该能够在不知道用户ID的情况下找到该用户并且只知道他们的随机代码。目前,当我查询时,它只返回匹配与查询设备相同的用户ID的用户,因此我试图仅查询随机代码(即范围键)。这是我的代码:

let queryExpression = AWSDynamoDBQueryExpression()
        queryExpression.keyConditionExpression = "#randomCode = :randomCode AND #userId = :userId"

        queryExpression.expressionAttributeNames = [
            "#userId": "userId",
            "#randomCode": "randomCode"
        ]
        queryExpression.expressionAttributeValues = [
            ":randomCode": passedCode,
            ":userId": AWSIdentityManager.default().identityId
        ]


        // 2) Make the query
        dynamoDbObjectMapper.query(Timestamps.self, expression: queryExpression) { (output: AWSDynamoDBPaginatedOutput?, error: Error?) in
            if error != nil {
                print("The request failed. Error: \(String(describing: error))")
            }
            if output == nil {
                print("Nill output")
            }
            if output != nil {
                if output!.items.count == 0 {
                    print("code not found")
                    DispatchQueue.main.async {
                        let animation = CABasicAnimation(keyPath: "position")
                        animation.duration = 0.07
                        animation.repeatCount = 4
                        animation.autoreverses = true
                        animation.fromValue = NSValue(cgPoint: CGPoint(x: self.codeTextField.center.x - 10, y: self.codeTextField.center.y))
                        animation.toValue = NSValue(cgPoint: CGPoint(x: self.codeTextField.center.x + 10, y: self.codeTextField.center.y))
                        self.codeTextField.layer.add(animation, forKey: "position")
                    }
                }
                else {
                    for timestamps in output!.items {
                        let timestampItem = timestamps as? Timestamps
                        //print(timestampItem?._songURI as! String)
                        let currentSong = timestampItem?._songURI as! String
                        let currentTimestamp = timestampItem?._timestamp
                        let currentTimestampDouble = currentTimestamp as! Double
                        //print(currentTimestampDouble)
                        self.playSync(currentSong: currentSong, currentTimestamp: currentTimestampDouble)
                    }

                }

            }
        }

If I pass the hash key as nil or don't pass it at all, aws returns an error that the query did not contain a hash key. How do I query for a hash key I do not know, or how do I just query for the range key?

如果我将散列键作为nil传递或者根本不传递它,则aws会返回一个错误,即查询不包含散列键。如何查询我不知道的哈希键,或者如何查询范围键?

Thank you for reading!

谢谢你的阅读!

1 个解决方案

#1


0  

You can't perform that query using the index you currently have. You need to create a Global Secondary Index on the table with randomCode as the primary key of that index. Then you can query by only randomCode by specifying that the query use the Global Secondary Index.

您无法使用当前拥有的索引执行该查询。您需要在表上创建一个Global Secondary Index,并将randomCode作为该索引的主键。然后,您只能通过指定查询使用全局二级索引来查询randomCode。


分享到: