简体   繁体   中英

Query max attribute value in DynamoDB with Java

Given my DynamoDB has a column of 'BlockNumber', how do I write the Java QuerySpec to find the MAX block number in the DB? (It is configured as a GSI.)

Typically, your GSI would have a partition key and a sort key, just like a regular DynamoDB table. You would issue a query against a known partition key and set ScanIndexForward=false and Limit=1, so it would return one item only, and it would be the item with a matching partition key and the maximum value of the sort key. When ScanIndexForward is false, DynamoDB reads the items in reverse order by sort key value.

If the data is immutable the best option for this is to have a separate record that holds aggregate values. Whenever you add an item that may change the max value you would update the aggregate record. The best approach to this is to use DynamoDB streams to perform the updates to the aggregate record. Using Global Secondary Indexes for Materialized Aggregation Queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM