DynamoDB-查询范围键的最大值 [英] DynamoDB - Querying for the greatest value of a range key

查看:344
本文介绍了DynamoDB-查询范围键的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DynamoDB表,其结构如下

  RK | A1 | A2 | A3 
(哈希键)| (范围键)

我有一个本地二级索引,其范围键 A3



我想找出特定的哈希键HK ,它的最大价值是多少?属性 A3 。因此,我这样查询二级索引:

  Map< String,AttributeValue> eav =新的HashMap<>(); 
eav.put(:v1,new AttributeValue()。withS( hash value));
queryExpression =新的DynamoDBQueryExpression< Table>()
.withIndexName( index-name)
.withKeyConditionExpression( H​​K =:v1)
.withExpressionAttributeValues(eav)
.withScanIndexForward(false); //这将按降序对结果进行排序(从w到范围键)

queryExpression.setLimit(1);
myCollection = dynamoDBMapper.query(Table.class,queryExpression);

问题是它会返回所有记录,并带有指定的哈希键,按范围键(A3)反向排序。我想独自获得第一张唱片。 (给定HK的A3值最大的记录。)



我尝试了 setLimit ,但是它不起作用。 p>

如何实现此目标。

解决方案

我通过使用 queryPage 而不是 query

  QueryResultPage< Lookup> res = dynamoDBMapper.queryPage(Table.class,queryExpression); 

更多信息此处


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..

解决方案

I solved it by using queryPage rather than query

QueryResultPage<Lookup> res = dynamoDBMapper.queryPage(Table.class, queryExpression);

More info here

这篇关于DynamoDB-查询范围键的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆