Update Attribute 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 to perform update an item attribute in DynamoDB. When updating, ensure that you provide the Table Name, Key and the Update Expression to execute during the update.
The following code updates the attributeName
attribute of the item with an id
attribute of 123 in the MyTable
table. The UpdateItemRequest
class is used to specify the update expression, which is set attributeName = :val
.
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 UpdateAttributeExample { 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().withS("Jane")); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName("MyTable") .withKey(key) .withUpdateExpression("set firstName = :val") .withExpressionAttributeValues(expressionAttributeValues); UpdateItemResult result = client.updateItem(updateItemRequest); System.out.println(result); } }
Similar Code Examples
- Batch Write Item in DynamoDB Using Java
- Batch Save Using DynamoDB Mapper
- Update Multiple Items in DynamoDB Using Java
- Batch Read in DynamoDB Using Java
- Delete All Using DynamoDB Mapper
- Get in DynamoDB Using Java
- Update Item in DynamoDB Using Java
- Remove Item in DynamoDB Using Java
- Batch Query in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Query Global Secondary Index in DynamoDB Using Java
- Delete Table in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
- Update Multiple Attributes in DynamoDB Using Java
- Delete by Hashkey Using DynamoDB Mapper
Login to the AWS Console less. Use Dynobase.
Try 7-day free trial. No strings attached.
Product Features
DynamoDB Tools
DynamoDB Info
© 2024 Dynobase