VOOZH about

URL: https://www.javacodegeeks.com/2016/07/query-dynamodb-items-java-part-2.html

⇱ Query DynamoDB Items with Java Part 2 - Java Code Geeks


On a previous post we had the chance to issue some basic DynamoDB query actions.

However apart from the basic actions the DynamoDB api provides us with some extra functionality.

Projections is a feature that has a select-like functionality.
You choose which attributes from a DynamoDB Item shall be fetched. Keep in mind that using projection will not have any impact on your query billing.

public Map<String,AttributeValue> getRegisterDate(String email) {

 Map<String,String> expressionAttributesNames = new HashMap<>();
 expressionAttributesNames.put("#email","email");

 Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
 expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));

 QueryRequest queryRequest = new QueryRequest()
 .withTableName(TABLE_NAME)
 .withKeyConditionExpression("#email = :emailValue")
 .withExpressionAttributeNames(expressionAttributesNames)
 .withExpressionAttributeValues(expressionAttributeValues)
 .withProjectionExpression("registerDate");

 QueryResult queryResult = amazonDynamoDB.query(queryRequest);

 List<Map<String,AttributeValue>> attributeValues = queryResult.getItems();

 if(attributeValues.size()>0) {
 return attributeValues.get(0);
 } else {
 return null;
 }
 }

Apart from selecting the attributes we can also specify the order according to our range key. We shall query the logins Table in a Descending order using scanIndexForward.

public List<Map<String,AttributeValue>> fetchLoginsDesc(String email) {

 List<Map<String,AttributeValue>> items = new ArrayList<>();

 Map<String,String> expressionAttributesNames = new HashMap<>();
 expressionAttributesNames.put("#email","email");

 Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
 expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));

 QueryRequest queryRequest = new QueryRequest()
 .withTableName(TABLE_NAME)
 .withKeyConditionExpression("#email = :emailValue")
 .withExpressionAttributeNames(expressionAttributesNames)
 .withExpressionAttributeValues(expressionAttributeValues)
 .withScanIndexForward(false);

 Map<String,AttributeValue> lastKey = null;

 do {

 QueryResult queryResult = amazonDynamoDB.query(queryRequest);
 List<Map<String,AttributeValue>> results = queryResult.getItems();
 items.addAll(results);
 lastKey = queryResult.getLastEvaluatedKey();
 } while (lastKey!=null);

 return items;
 }

A common functionality of databases is counting the items persisted in a collection. In our case we want to count the login occurrences of a specific user. However pay extra attention since the count functionality does nothing more than counting the total items fetched, therefore it will cost you as if you fetched the items.

public Integer countLogins(String email) {
 List<Map<String,AttributeValue>> items = new ArrayList<>();

 Map<String,String> expressionAttributesNames = new HashMap<>();
 expressionAttributesNames.put("#email","email");

 Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
 expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));

 QueryRequest queryRequest = new QueryRequest()
 .withTableName(TABLE_NAME)
 .withKeyConditionExpression("#email = :emailValue")
 .withExpressionAttributeNames(expressionAttributesNames)
 .withExpressionAttributeValues(expressionAttributeValues)
 .withSelect(Select.COUNT);

 Map<String,AttributeValue> lastKey = null;
 QueryResult queryResult = amazonDynamoDB.query(queryRequest);
 List<Map<String,AttributeValue>> results = queryResult.getItems();
 return queryResult.getCount();
 }

Another feature of DynamoDB is getting items in batches even if they belong on different tables. This is really helpful in cases where data that belong on a specific context are spread through different tables. Every get item is handled and charged as a DynamoDB read action. In case of batch get item all table keys should be specified since every query’s purpose on BatchGetItem is to fetch a single Item.

public Map<String,List<Map<String,AttributeValue>>> getMultipleInformation(String email,String name) {

 Map<String,KeysAndAttributes> requestItems = new HashMap<>();

 List<Map<String,AttributeValue>> userKeys = new ArrayList<>();
 Map<String,AttributeValue> userAttributes = new HashMap<>();
 userAttributes.put("email",new AttributeValue().withS(email));
 userKeys.add(userAttributes);
 requestItems.put(UserRepository.TABLE_NAME,new KeysAndAttributes().withKeys(userKeys));

 List<Map<String,AttributeValue>> supervisorKeys = new ArrayList<>();
 Map<String,AttributeValue> supervisorAttributes = new HashMap<>();
 supervisorAttributes.put("name",new AttributeValue().withS(name));
 supervisorKeys.add(supervisorAttributes);
 requestItems.put(SupervisorRepository.TABLE_NAME,new KeysAndAttributes().withKeys(supervisorKeys));

 BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest();
 batchGetItemRequest.setRequestItems(requestItems);

 BatchGetItemResult batchGetItemResult = amazonDynamoDB.batchGetItem(batchGetItemRequest);

 Map<String,List<Map<String,AttributeValue>>> responses = batchGetItemResult.getResponses();

 return responses;
 }

You can find the sourcecode on github

Reference: Query DynamoDB Items with Java Part 2 from our JCG partner Emmanouil Gkatziouras at the gkatzioura blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Emmanouil Gkatziouras
Emmanouil Gkatziouras
July 20th, 2016Last Updated: July 19th, 2016
0 783 2 minutes read

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz