14

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.

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey.. I have read your article already... It is indeed a nice one.Actually I don't want to make such condition for range key operation. Isn't there any other way ?
The other possibibility that I see (but have not tried, yet) is to use the low-level Java query api: docs.aws.amazon.com/amazondynamodb/latest/developerguide/… QuerySpec().withKeyConditionExpression("Id = :v_id and Date = :v_date").withValueMap(new ValueMap().withString(":v_id", "123") .withString(":v_date", "1234")
If you're the author of the article, please can you fix the broken link?
8

You could use dynamoDbMapper.load() as following:

TestTable testTable = new TestTable();
testTable.setId("123");
testTable.setDate("1234");
TestTable result = dynamoDBMapper.load(testTable);

1 Comment

What is returned when the value is not found on the table? Anyone knows?
3

If you're not using index, you should have exact one item, if any, whose Id ="123" and Date ="1234".

For the exact hash key and range key (in case of = operator), you don't need a query operation. Please have a loot at GetItem API, which is nifty. See documentation page.

And if you need non-EQ operator for range key (e.g. >=), you can use key condition expression in Query operation: Id = :id and Date >= :date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.