Update DynamoDB item with DynamoDBMapper in Java

How to update a DynamoDB item using DynamoDBMapper?

I have several processes using the DynamoDB table, so getting + save will create inconsistency. I cannot find a method for updating an item using DynamoDBMapper.

+6
source share
2 answers

The save() method will execute putItem or updateItem based on the value set in SaveBehavior . Please refer to the description below. For this reason, the DynamoDBMapper class does not have an update method. However, there is a separate removal method.

Saves an item in DynamoDB. The service method used is determined by the value DynamoDBMapperConfig.getSaveBehavior () to use either AmazonDynamoDB.putItem (PutItemRequest) or AmazonDynamoDB.updateItem (UpdateItemRequest):

UPDATE (default): UPDATE will not affect the unmodeled attributes of the save operation, and a null value for the modeled attribute will remove it from this element in DynamoDB. Due to the restriction of the updateItem request, the UPDATE implementation will send a putItem request when saving the object for the key only and send another updateItem request if the given key already exists in the table.

UPDATE_SKIP_NULL_ATTRIBUTES: similar to UPDATE except that it ignores any attributes with a null value and does NOT remove them from this element in DynamoDB. It also ensures that only one updateItem request is sent, regardless of whether the object is just a key or not.

CLOBBER: CLOBBER will clear and replace all attributes, including unmodeled ones (deletion and re- creation) when saved. Limitations of versioned fields will also be ignored. Any parameters specified in the saveExpression parameter will be imposed on any restrictions due to versioned attributes.

Usage example: -

 DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE); 

UPDATE The DynamoDBMapperConfig constructor (aws sdk 1.11.473) seems deprecated, and the constructor should be used instead:

 DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder() .withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT) .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE) .build(); dynamoDBMapper.save(yourObject, dynamoDBMapperConfig); 
+17
source

To ensure consistency of dynamo-db recording operations, you need to choose one of the options between optimistic locking and conditional recording.

Here are links to AWS documentation that may help you;

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.OptimisticLocking.html

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html

0
source

Source: https://habr.com/ru/post/1258529/


All Articles