DynamoDB UpdateItem in Java (AWS SDK v2)

UpdateItem changes specific attributes of one item (and creates the item if it doesn't exist), leaving everything else untouched. In AWS SDK for Java 2.x you build an UpdateItemRequest with an updateExpression.

Code

import java.util.HashMap;
import java.util.Map;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;

public class UpdateItemExample {
    public static void main(String[] args) {
        try (DynamoDbClient ddb = DynamoDbClient.builder()
                .region(Region.US_EAST_1)
                .build()) {

            Map<String, AttributeValue> key = new HashMap<>();
            key.put("Artist", AttributeValue.builder().s("Arturo Sandoval").build());
            key.put("SongTitle", AttributeValue.builder().s("Cubano Chant").build());

            Map<String, String> names = new HashMap<>();
            names.put("#upd0", "Genre");
            names.put("#upd1", "Year");
            names.put("#upd2", "Awards");

            Map<String, AttributeValue> values = new HashMap<>();
            values.put(":updValue0", AttributeValue.builder().s("Latin Jazz").build());
            values.put(":updValue1", AttributeValue.builder().n("1994").build());
            values.put(":updValue2", AttributeValue.builder().n("1").build());

            UpdateItemRequest request = UpdateItemRequest.builder()
                    .tableName("Music")
                    .key(key)
                    .updateExpression("SET #upd0 = :updValue0, #upd1 = :updValue1 ADD #upd2 :updValue2")
                    .expressionAttributeNames(names)
                    .expressionAttributeValues(values)
                    .returnValues(ReturnValue.ALL_NEW)
                    .build();

            UpdateItemResponse response = ddb.updateItem(request);
            System.out.println(response.attributes()); // the item after the update
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
        }
    }
}

Explanation

  • key — the full primary key of the item to update.
  • updateExpression — one or more clauses:
    • SET assigns attributes (#upd0 = :updValue0 sets Genre; #upd1 = :updValue1 sets Year). Every attribute is aliased via expressionAttributeNames, so reserved words like Year are always safe.
    • ADD on a number does an atomic increment (ADD #upd2 :updValue2 adds 1 to Awards) — safe under concurrency, no read-modify-write race. REMOVE deletes an attribute; DELETE removes elements from a set.
  • expressionAttributeValues — the :placeholder → typed-AttributeValue map.
  • returnValues(ReturnValue.ALL_NEW) — returns the full item after the update in response.attributes() (also UPDATED_NEW, ALL_OLD, UPDATED_OLD, or NONE).
  • Upsert semanticsUpdateItem creates the item if the key doesn't exist. Add a conditionExpression (e.g. attribute_exists(Artist)) to update-only.
  • Prefer an update expression over the legacy attributeUpdates map — expressions support multiple clauses, aliases, and conditions in one request.

Do it visually

The DynamoDB Expression Builder builds SET / ADD / REMOVE clauses with the name/value maps and copies runnable code.

To edit items in a GUI — change a field, review the generated UpdateExpression, copy the code — download DynoTable.

References

Last verified 2026-07-13 against the official AWS documentation linked above.

Lavora con DynamoDB senza la Console

DynoTable è un client desktop veloce per DynamoDB — sfoglia le tabelle, esegui query in stile SQL e modifica gli Item localmente.