Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying for greatest value of Range key on AWS DynamoDb

What is the DynamoDB equivalent of

SELECT MAX(RANGE_KEY) FROM MYTABLE WHERE PRIMARYKEY = "value"

The best I can come up with is

from boto.dynamodb2.table import Table as awsTable

tb = awsTable("MYTABLE")
rs = list(tb.query_2(PRIMARYKEY__eq="value", reverse=True, limit=1))
MAXVALUE = rs[0][RANGE_KEY]

Is there a better way to do this?

like image 534
Vishal Avatar asked Aug 23 '14 02:08

Vishal


People also ask

Can you Query on sort key DynamoDB?

You can Query any table or secondary index, provided that it has a composite primary key (partition key and sort key). Query operations consume read capacity units, as follows. The table's provisioned read capacity. The index's provisioned read capacity.

What's the maximum number of fields that can make a primary key in DynamoDB?

As I'm sure you have figured out you cannot have more than two attributes form your primary key (hash+range). Thus, depending on the type of queries you will be performing and the size of your data you can structure your table in different ways.

Which is faster scan or Query in DynamoDB?

More complex queries on DynamoDB data are occasionally required. Instead of scanning for such queries, it is usually preferable to create a GSI (global secondary index). Out of interest, I ran an experiment to confirm that Scan operation is indeed slower than Query operation.


3 Answers

That's the correct way.

Because the records matched by the Hash Key are sorted by the Range Key, getting the first one by the descendant order will give you the record with the maximum range key.

Query results are always sorted by the range key. If the data type of the range key is Number, the results are returned in numeric order; otherwise, the results are returned in order of ASCII character code values. By default, the sort order is ascending. To reverse the order use the ScanIndexForward parameter set to false.

Query and Scan Operations - Amazon DynamoDB : http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html

NOTE: Setting the reverse parameter to true via boto API is equivalent to setting ScanIndexForward to false via the native AWS API.

like image 53
b-s-d Avatar answered Sep 25 '22 23:09

b-s-d


If someone looking how to do it with Java:

QuerySpec querySpec = new QuerySpec();
        querySpec.withKeyConditionExpression("PRIMARYKEY = :key")
                .withValueMap(new ValueMap()
                        .withString(":key", primaryKeyValue));
        querySpec.withScanIndexForward(true); 
        querySpec.withMaxResultSize(1);
like image 38
user3485142 Avatar answered Sep 24 '22 23:09

user3485142


In boto3 you can do it this way:

import boto3
from boto3.dynamodb.conditions import Key, Attr

kce = Key('table_id').eq(tableId) & Key('range').between(start, end)
output = table.query(KeyConditionExpression = kce, ScanIndexForward = False, Limit = 1) 

output contains the row associated with the Max value for the range between start and end. For the Min value change the ScanIndexForward to True

like image 22
abedfar Avatar answered Sep 25 '22 23:09

abedfar