DynamoDB - Aggregation

DynamoDB - Aggregation

DynamoDB does not provide aggregation functionality. You must use queries, scans, indexes, and various tools creatively to accomplish these tasks. In all of this, the throughput of requests/scans in these operations can be large.

You also have the option to use libraries and other tools for your preferred DynamoDB coding language. Make sure they are compatible with DynamoDB before using it.

Calculate the maximum or minimum

Use the ascending/descending order of the results, the Limit parameter, and any parameters that set the order to find the highest and lowest values.

For example -

Map < String , AttributeValue > eaval = new HashMap <>();  
eaval . put ( ":v1" , new AttributeValue (). withS ( "hashval" ));  
queryExpression = new DynamoDBQueryExpression < Table >() . withIndexName ( "yourindexname" ) . withKeyConditionExpression ( "HK = :v1" ) . withExpressionAttributeValues ​​( values        
    
    
   ) . withScanIndexForward ( false ); //descending order 
                   

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

Count the number

Use DescribeTable to get the number of elements in a table, however note that it provides stale data. Also use Java 's getScannedCount method .

Use LastEvaluatedKey to provide all results.

For example -

ScanRequest scanRequest = new ScanRequest (). withTableName ( yourtblName ); ScanResult yourresult = client . scan ( scanRequest ); System . out . println ( "#items:" + yourresult . getScannedCount ());   
 
 

Average and sum calculation

Use indexes and query/scan to extract and filter values ​​before processing. Then simply operate on those values ​​through the object.