Conditional Update in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
DynamoDB provides the ability to update an item conditionally based on certain conditions. You can use the updateItem method with a condition expression in Java. In the below example, the updateItem method is used to update an item in a table named "Product." The key of the item to update is specified using the key map, which maps attribute names to attribute values.
The updateExpression parameter is used to specify the update to be performed on the item. The expressionAttributeValues map provides the values for any placeholders in the update expression. The conditionExpression parameter is used to specify a condition that must be true for the update to be performed. If the condition is not true, the update will be skipped, and no error will be returned.
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.AttributeValue.M; import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest; import java.util.Map; public class ConditionalUpdateExample { public static void main(String[] args) { // Create a new client AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); // Define the key of the item to update Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("id", new AttributeValue().withS("123")); // 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")); // Define the condition expression String conditionExpression = "quantity < :oldQuantity"; // Define the values for the condition expression Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":oldQuantity", new AttributeValue().withN("50")); // Create the update request UpdateItemRequest request = new UpdateItemRequest() .withTableName("Product") .withKey(key) .withUpdateExpression(updateExpression) .withExpressionAttributeValues(expressionAttributeValues) .withConditionExpression(conditionExpression); // Send the request to update the item client.updateItem(request); } }
Similar Code Examples
- Update Item in DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Batch Read in DynamoDB Using Java
- Delete Expression in DynamoDB Using Java
- Batch Write Item in DynamoDB Using Java
- Batch Update Using DynamoDB Mapper
- Batch Get in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- GetItem in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Update Multiple Items in DynamoDB Using Java
- Query Date Range in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Batch Load Using DynamoDB Mapper
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today
Product Features
- New in Dynobase 3.0
- Semantic and Vector Search
- Schema Explorer
- Visual JSON Path Filtering
- Visual Item Diff
- MCP Server for AI Tools
- Use SQL with DynamoDB
- Import CSV To DynamoDB
- Import JSON To DynamoDB
- Export DynamoDB To JSON
- Export DynamoDB To S3
- Export DynamoDB To CSV
- DynamoDB Update Item
- DynamoDB Delete All Items
- DynamoDB Delete Item
- DynamoDB Create Table
- DynamoDB Delete Table
- DynamoDB Conditional Update