VOOZH about

URL: https://www.javacodegeeks.com/2016/08/update-dynamodb-items-java.html

⇱ Update DynamoDB Items with Java - Java Code Geeks


On a previous post we proceeded into inserting items to DynamoDB using Java. DynamoDB also supports updating items.

We will use the Login table for the update examples.
When issuing an update you must specify the primary key of the item you want to update.

public void updateName(String email,String fullName) {

 Map<String,AttributeValue> attributeValues = new HashMap<>();
 attributeValues.put("email",new AttributeValue().withS(email));
 attributeValues.put("fullname",new AttributeValue().withS(fullName));

 UpdateItemRequest updateItemRequest = new UpdateItemRequest()
 .withTableName(TABLE_NAME)
 .addKeyEntry("email",new AttributeValue().withS(email))
 .addAttributeUpdatesEntry("fullname",
 new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)));

 UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);
 }

We can proceed on more advanced statements using conditional updates. Conditional updates can help us in many cases such as handling concurrent updates.

We can achieve so by using plain expressions.

public void updateConditionallyWithExpression(String email,String fullName,String prefix) {

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

 Map<String, AttributeValue> attributeValues = new HashMap<>();
 attributeValues.put(":prefix", new AttributeValue().withS(prefix));
 attributeValues.put(":fullname", new AttributeValue().withS(fullName));

 UpdateItemRequest updateItemRequest = new UpdateItemRequest()
 .withTableName(TABLE_NAME)
 .withKey(key)
 .withUpdateExpression("set fullname = :fullname")
 .withConditionExpression("begins_with(fullname,:prefix)")
 .withExpressionAttributeValues(attributeValues);
 UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);
 }

Or through by specifying attributes.

public void updateConditionallyWithAttributeEntries(String email, String fullName, String prefix){

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

 UpdateItemRequest updateItemRequest = new UpdateItemRequest()
 .withTableName(TABLE_NAME)
 .withKey(key)
 .addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullName)).withAction(AttributeAction.PUT))
 .addExpectedEntry("fullname",new ExpectedAttributeValue().withValue(new AttributeValue().withS(prefix)).withComparisonOperator(ComparisonOperator.BEGINS_WITH));

 UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);
 }

Another feature is atomic counters. We can issue updates to a DynamoDB item and increase the attribute values. We will add an extra field called count. Also we will add another update function. Once called the function will update the field specified but will also increase the counter attribute. Thus the counter attribute will represent how many times and update was performed on a specific item.

public void addUpdateCounter(String email) {

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

 UpdateItemRequest updateItemRequest = new UpdateItemRequest()
 .withTableName(TABLE_NAME)
 .withKey(key)
 .addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("0")).withAction(AttributeAction.PUT));

 UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);
 }

 public void updateAndIncreaseCounter(String email,String fullname) {

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

 UpdateItemRequest updateItemRequest = new UpdateItemRequest()
 .withTableName(TABLE_NAME)
 .withKey(key)
 .addAttributeUpdatesEntry("fullname",new AttributeValueUpdate().withValue(new AttributeValue().withS(fullname)).withAction(AttributeAction.PUT))
 .addAttributeUpdatesEntry("counter",new AttributeValueUpdate().withValue(new AttributeValue().withN("1")).withAction(AttributeAction.ADD));

 UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);
 }

You can find the sourcecode on github.

Reference: Update DynamoDB Items with Java 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
August 9th, 2016Last Updated: August 9th, 2016
0 126 1 minute 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