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:SETassigns attributes (#upd0 = :updValue0setsGenre;#upd1 = :updValue1setsYear). Every attribute is aliased viaexpressionAttributeNames, so reserved words likeYearare always safe.ADDon a number does an atomic increment (ADD #upd2 :updValue2adds 1 toAwards) — safe under concurrency, no read-modify-write race.REMOVEdeletes an attribute;DELETEremoves elements from a set.
expressionAttributeValues— the:placeholder→ typed-AttributeValuemap.returnValues(ReturnValue.ALL_NEW)— returns the full item after the update inresponse.attributes()(alsoUPDATED_NEW,ALL_OLD,UPDATED_OLD, orNONE).- Upsert semantics —
UpdateItemcreates the item if the key doesn't exist. Add aconditionExpression(e.g.attribute_exists(Artist)) to update-only. - Prefer an update expression over the legacy
attributeUpdatesmap — 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.
Related examples
- DynamoDB UpdateItem in Go — the same update with AWS SDK for Go v2.
- DynamoDB PutItem in Java — replace the whole item instead.
- DynamoDB update expressions —
SET,ADD,REMOVE,DELETE, and idioms. - Understanding ReturnValues — what each
ReturnValuesoption gives you. - "Attribute name is a reserved keyword" — why the alias map here isn't optional.
- "Invalid UpdateExpression" syntax errors — the common SET/ADD syntax mistakes, decoded.
References
- UpdateItem — Amazon DynamoDB API Reference
- Use UpdateItem with an AWS SDK or CLI — Amazon DynamoDB Developer Guide
- DynamoDbClient — AWS SDK for Java 2.x API Reference
- UpdateItemRequest — AWS SDK for Java 2.x API Reference
- Update expressions — Amazon DynamoDB Developer Guide
Last verified 2026-07-13 against the official AWS documentation linked above.