Update DynamoDB item using DynamoDBMapper in Java

后端 未结 2 1919
春和景丽
春和景丽 2021-02-20 10:00

How can I update DynamoDB item using DynamoDBMapper?

I have multiple processes, using the DynamoDB table, thus, get + save will create inconsistency. I can not find the

相关标签:
2条回答
  • 2021-02-20 10:18

    To have consistency in your dynamo db write operations you will have to choose one option between Optimistic locking and Conditional writes.

    Here are the links to AWS documentation which 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 讨论(0)
  • 2021-02-20 10:21

    The save() method will perform the putItem or updateItem based on the value set in SaveBehavior. Please refer the below description. There is no update method in DynamoDBMapper class because of this reason. However, there is a separate delete method available.

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

    UPDATE (default) : UPDATE will not affect unmodeled attributes on a save operation and a null value for the modeled attribute will remove it from that item in DynamoDB. Because of the limitation of updateItem request, the implementation of UPDATE will send a putItem request when a key-only object is being saved, and it will send another updateItem request if the given key(s) already exists in the table.

    UPDATE_SKIP_NULL_ATTRIBUTES : Similar to UPDATE except that it ignores any null value attribute(s) and will NOT remove them from that item in DynamoDB. It also guarantees to send only one single updateItem request, no matter the object is key-only or not.

    CLOBBER : CLOBBER will clear and replace all attributes, included unmodeled ones, (delete and recreate) on save. Versioned field constraints will also be disregarded. Any options specified in the saveExpression parameter will be overlaid on any constraints due to versioned attributes.

    Example usage:-

    DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig(SaveBehavior.UPDATE);
    

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

    DynamoDBMapperConfig dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
      .withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
      .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE)
      .build();
    dynamoDBMapper.save(yourObject, dynamoDBMapperConfig);
    
    0 讨论(0)
提交回复
热议问题