Update Expression in DynamoDB Using Java (Guide w/ Code Examples)
![Rafal Wilinski](https://pbs.twimg.com/profile_images/1390706809548197895/SVCKpLNE_400x400.png)
Provided by Rafal Wilinski
This updates the item in the MyTable
table with an id
attribute of 123 and sets the "rating" attribute to 5. The UpdateItemRequest
class is used to specify the update expression, which is set rating = :val
. The expressionAttributeValues
map is used to specify the value to be used in the update expression. In this case, :val
is mapped to the value 5.
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest; import com.amazonaws.services.dynamodbv2.model.UpdateItemResult; public class UpdateExpressionExample { public static void main(String[] args) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withN("123")); Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":val", new AttributeValue().withN("5")); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName("MyTable") .withKey(key) .withUpdateExpression("set rating = :val") .withExpressionAttributeValues(expressionAttributeValues); UpdateItemResult result = client.updateItem(updateItemRequest); System.out.println(result); } }
Similar Code Examples
- Batch Write Item in DynamoDB Using Java
- Delete All Using DynamoDB Mapper
- Delete All Items in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Batch Query in DynamoDB Using Java
- Update Attribute in DynamoDB Using Java
- Query Local Secondary Index in DynamoDB Using Java
- Get in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Batch Write Using DynamoDB Mapper
- Batch Load Using DynamoDB Mapper
- Conditional Update in DynamoDB Using Java
- Get All Items from DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
- Batch Get in DynamoDB Using Java
Better DynamoDB experience.
First 7 days are on us. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase