How to execute a query in DynamoDB based on HashKey and Key?

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

Say my table is TestTable , and this circuit looks 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 in this table based on hashKey , which is Id here, we do a query as:

Let's say my query is to get all the elements 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 the elements with Id ="123" and Date ="1234" .

How can I request this thing in DynamoDb

I use java as my programming language.

+7
java amazon-dynamodb
source share
3 answers

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

In your case, it should work as follows (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); 

See the Java Object Saving Model section in DynamoDB documents for more information.

+7
source share

You can use dynamoDbMapper.load () as follows:

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

If you are not using an index, you must have exactly one element, if any, Id ="123" and Date ="1234" .

For the exact hash key and range key (in the case of the = operator) you do not need a query operation. Please be robbery in the GetItem API, which is different. See the documentation page .

And if you need a statement without an equalizer for a range key (e.g. >= ), you can use the key condition expression in Query : Id = :id and Date >= :date .

+2
source share

All Articles