DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • The Future of Java and AI: Coding in 2025
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • How Can Developers Drive Innovation by Combining IoT and AI?
  1. DZone
  2. Coding
  3. Java
  4. Update DynamoDB Items With Java

Update DynamoDB Items With Java

Amazon's DynamoDB is a powerful cloud database service that includes a simple Java API.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Aug. 11, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
34.1K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous post, we showed 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 to more advanced statements using conditional updates. Conditional updates can help us in many cases, such as handling concurrent updates.

We can achieve this 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 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, which once called will update the field specified but will also increase the counter attribute. Thus, the counter attribute will represent how many times an 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);
}


And that's that. As usual, you can find the source code on GitHub.

Java (programming language)

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: