DynamoDB - Querying for the greatest value of a range key

依然范特西╮ 提交于 2019-12-11 12:31:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!