Monday 24 October 2016

DynamoDB Low Level API-updateItem() (Java)

If you don't want to use high-level API DynamoDBMapper to update the DynamoDB table. You can use the low level APIs to update the table items.

AWS don't provide detail document for item update in Java, this document is going to introduce several item update methods systematically.

1. UpdateItemOutcome updateItem(PrimaryKey primaryKey, AttributeUpdate... attributeUpdates)

This is most simple one, you are using AttributeUpdate directly. By using AttributeUpdate, you can update the item by add objects by using addElements()or add number by using addNumeric().


1
2
3
4
5
6
7
final String phoneNumber = "9872-6530";
table.updateItem(HASH_KEY, ITEM_HASH_KEY, RANGE_KEY, ITEM_RANGE_KEY,
                new AttributeUpdate("phone").addElements(phoneNumber));
outcome = table.getItemOutcome(new GetItemSpec()
               .withPrimaryKey(HASH_KEY, ITEM_HASH_KEY, RANGE_KEY, ITEM_RANGE_KEY)
               .withConsistentRead(true));
item = outcome.getItem();


2. UpdateItemOutcome updateItem(UpdateItemSpec updateItemSpec)

This is most powerful one, UpdateItemSpec() has all the features you need to update a item. There is also a benefit of using UpdateItemSpec(), you can specify the return value and expected results. If the ReturnValue is UPDATED_NEW, then it only returns the attributes you updated.
If the ReturnValue is ALL_NEW, then it will returns the whole item with all new attributes.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
UpdateItemOutcome outcome = table.updateItem(new UpdateItemSpec()
            .withReturnValues(ReturnValue.ALL_NEW)
            .withPrimaryKey(HASH_KEY, ITEM_HASH_KEY, RANGE_KEY, ITEM_RANGE_KEY)
            .withAttributeUpdate(
                new AttributeUpdate("Player-Position").addNumeric(1),
                new AttributeUpdate("Status").put("SUSPENDED"))
            .withExpected(
                new Expected("Player1-Position").lt(20),
                new Expected("Player2-Position").lt(20),
                new Expected("Status").eq("IN_PROGRESS"))
        );


3. UpdateItemOutcome updateItem(PrimaryKey primaryKey,
            String updateExpression, String conditionExpression,
            Map nameMap, Map valueMap)

This is another method to update item. You can specify the update expression directly.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Map<String, String> expressionAttributeNames = new HashMap<String, String>();
expressionAttributeNames.put("#P", "Price");

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val1", 25);  // update Price to 25...
expressionAttributeValues.put(":val2", 20);  //...but only if existing Price is 20

UpdateItemOutcome outcome = table.updateItem(
    new PrimaryKey("Id",101),
    "set #P = :val1", // UpdateExpression
    "#P = :val2",     // ConditionExpression
    expressionAttributeNames,
    expressionAttributeValues);


You can choose the method you need to update a DynamoDB table item.

1 comment: