Update Multiple Attributes in DynamoDB Using Java (Guide w/ Code Examples)
![Rafal Wilinski](https://pbs.twimg.com/profile_images/1390706809548197895/SVCKpLNE_400x400.png)
Provided by Rafal Wilinski
You can use the UpdateItemRequest class the DynamoDB SDK offers to update an item in DynamoDB. To update multiple attributes, you can define multiple attributes in the attributeUpdate()
.
Here, the values for the attributes attribute1
and attribute2
are updated for an item in a DynamoDB table. The primary key for the item is specified as "id
" with a value of "12345
". The AttributeValueUpdate
objects contain the new values for the attributes, and the AttributeAction.PUT
action tells DynamoDB to set the attribute to the specified value. Alternatively, you can use other attribute actions such as ADD
or DELETE
to perform different types of updates.
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; import java.util.HashMap; import java.util.Map; public class UpdateMultipleAttributes { public static void main(String[] args) { DynamoDbClient ddb = DynamoDbClient.create(); String tableName = "my-table"; String keyAttributeName = "id"; String keyAttributeValue = "12345"; Map<String, AttributeValue> key = new HashMap<>(); key.put(keyAttributeName, AttributeValue.builder().s(keyAttributeValue).build()); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>(); attributeUpdates.put("attribute1", AttributeValueUpdate.builder() .value(AttributeValue.builder().s("new value").build()) .action(AttributeAction.PUT).build()); attributeUpdates.put("attribute2", AttributeValueUpdate.builder() .value(AttributeValue.builder().s("new value").build()) .action(AttributeAction.PUT).build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName(tableName) .key(key) .attributeUpdates(attributeUpdates) .build(); ddb.updateItem(request); } }
Similar Code Examples
- Remove with DynamoDB Mapper
- Update Item in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Delete in DynamoDB Using Java
- Batch Put Item in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Update Expression in DynamoDB Using Java
- Delete All Using DynamoDB Mapper
- Batch Delete Using DynamoDB Mapper
Tired of switching accounts and regions? Use Dynobase.
First 7 days are on us. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase