Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Query in DynamoDB on the basis of HashKey and range Key?

I am new to DynamoDb stuff. I just want to know how can we query on a table in DynamoDB with the hashKey and rangeKey.

Let's say my Table is TestTable and it's schema is something like this:

1.Id (HK of type String)
2 Date (RK of type String )
3 Name (attribute of type String)

Now If I want to query on this table on the basis of hashKey which is Id here, we make a query as :

Let's say my query is to get all Items having Id ="123".

TestTable testTable = new TestTable();
testTable.setId("123");

DynamoDBQueryExpression<TestTable> queryExpression = new DynamoDBQueryExpression<TestTable>()
                                                                .withHashKeyValues(TestTable)
                                                                .withConsistentRead(false);

Now I want to get all Items having Id ="123" and Date ="1234".

How can I query this thing in DynamoDB

I am using java as my programming language.

like image 501
Geek_To_Learn Avatar asked Jun 15 '15 08:06

Geek_To_Learn


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.

Is there a way to query multiple hash keys in DynamoDB?

There is no way to query by multiple hash keys, but, as of April 2014, you can use QueryFilter so you can filter by non key fields in addition to hash key fields.

Can we query DynamoDB without primary key?

Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.

What is hash key and range key in DynamoDB?

Hash key is used to select the DynamoDB partition. Partitions are parts of the table data. Range keys are used to sort the items in the partition, if they exist. So both have a different purpose and together help to do complex query.


Video Answer


2 Answers

I wrote an article about DynamoDB queries and indexing using the AWS Java SDK some time ago: http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/

In your case, it should work like this (see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html):

AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
DynamoDBMapper mapper = new DynamoDBMapper(client);

String hashKey = "123";
long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
Date twoWeeksAgo = new Date();
twoWeeksAgo.setTime(twoWeeksAgoMilli);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);            
Condition rangeKeyCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.GT.toString())
        .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr.toString()));

Reply replyKey = new Reply();
replyKey.setId(hashKey);

DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
        .withHashKeyValues(replyKey)
        .withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);

List<Reply> latestReplies = mapper.query(Reply.class, queryExpression);

Check out the Java Object Persistence Model section of the DynamoDB docs for more info.

like image 163
Markus Klems Avatar answered Nov 15 '22 12:11

Markus Klems


You could use dynamoDbMapper.load() as following:

TestTable testTable = new TestTable();
testTable.setId("123");
testTable.setDate("1234");
TestTable result = dynamoDBMapper.load(testTable);
like image 24
Nikita Avatar answered Nov 15 '22 10:11

Nikita