Update Multiple Items in DynamoDB Using Java (Guide w/ Code Examples)
![Rafal Wilinski](https://pbs.twimg.com/profile_images/1390706809548197895/SVCKpLNE_400x400.png)
Provided by Rafal Wilinski
DynamoDB does not provide a direct method to update multiple items at once, like a "batch update" operation. However, you can use the updateItem
method in a loop to update multiple items one by one.
In the snippet shown below, a list of items to update is defined, and the updateItem
method is called for each item in the list. The updateExpression
and expressionAttributeValues
are the same for all items, but the key of the item to update is taken from the current item in the loop. This enables a batch update.
However, you can also use a Transactional Update if you wish to update multiple updates as a single operation.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeValue.AttributeValue; import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest; import java.util.List; import java.util.Map; public class MultipleUpdateExample { public static void main(String[] args) { // Create a new client AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); // Define a list of items to update List<Map<String, AttributeValue>> itemsToUpdate = new ArrayList<Map<String, AttributeValue>>(); itemsToUpdate.add(new HashMap<String, AttributeValue>() {{ put("id", new AttributeValue().withS("123")); }}); itemsToUpdate.add(new HashMap<String, AttributeValue>() {{ put("id", new AttributeValue().withS("456")); }}); itemsToUpdate.add(new HashMap<String, AttributeValue>() {{ put("id", new AttributeValue().withS("789")); }}); // Define the update expression String updateExpression = "SET price = :newPrice, quantity = :newQuantity"; // Define the values for the update expression Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":newPrice", new AttributeValue().withN("999")); expressionAttributeValues.put(":newQuantity", new AttributeValue().withN("20")); for (Map<String, AttributeValue> item : itemsToUpdate) { // Create the update request UpdateItemRequest request = new UpdateItemRequest() .withTableName("Product") .withKey(item) .withUpdateExpression(updateExpression) .withExpressionAttributeValues(expressionAttributeValues); // Send the request to update the item client.updateItem(request); } } }
Similar Code Examples
- Query Local Secondary Index in DynamoDB Using Java
- Delete Record in DynamoDB Using Java
- Delete Multiple Items in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Delete in DynamoDB Using Java
- Delete All Items in DynamoDB Using Java
- Query Index in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Batch Put Item in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Delete Table in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Delete Expression in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
Login to the AWS Console less. Use Dynobase.
Try 7-day free trial. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase