问题
I have a DynamoDB table which has the following structure
HK | RK | A1 | A2 | A3
(Hash Key) | (Range Key)
I have a local secondary index whose range key is A3.
I want to find out for a specific Hash key HK, what is the greatest value of the attribute A3. So I query the secondary index like this:
Map<String, AttributeValue> eav = new HashMap<>();
eav.put(":v1", new AttributeValue().withS("hash value"));
queryExpression = new DynamoDBQueryExpression<Table>()
.withIndexName("index-name")
.withKeyConditionExpression("HK = :v1")
.withExpressionAttributeValues(eav)
.withScanIndexForward(false); //This will sort the result in descending order (w.r to range key)
queryExpression.setLimit(1);
myCollection = dynamoDBMapper.query(Table.class, queryExpression);
The problem is that it returns all the records with the specified hash key, reverse sorted by range key(A3). I want to get the first record alone. (The record with the largest value for A3 for a given HK).
I tried with setLimit, but it is not working.
How can I achieve this..
回答1:
I solved it by using queryPage
rather than query
QueryResultPage<Lookup> res = dynamoDBMapper.queryPage(Table.class, queryExpression);
More info here
来源:https://stackoverflow.com/questions/35983291/dynamodb-querying-for-the-greatest-value-of-a-range-key