2019
Dec
18




When I migrate RDS table to the Dynamodb, one of major important functionality is generating sequence id. Dynamodb do not support it.

Here is an example for using DynamoDB add expression to generate a sequential id. Also you don't need to create the item before update it. DynamoDB will automatically create a new item if it doesn't exist.

Dynamodb Table example

rows field: id field: value
1 sequenceId 1,2,3 : start from 1
2 any item you put any item you put

Java code

Example
  1. // initiate dynamodb client
  2. AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
  3. .withRegion(region)
  4. .withCredentials(
  5. ...
  6. )
  7. .build()
  8.  
  9. // primary key
  10. HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
  11. key.put("id", new AttributeValue().withS("sequenceId"));
  12.  
  13. // expression value
  14. Map<String, AttributeValue> u = new HashMap<String, AttributeValue>();
  15. u.put(":val", new AttributeValue().withN("1"));
  16.  
  17. // expression name
  18. Map<String, String> n = new HashMap<String, String>();
  19. n.put("#index", "index");
  20.  
  21. UpdateItemRequest req = new UpdateItemRequest()
  22. .withTableName("table_name")
  23. .withKey(key)
  24. .withUpdateExpression("ADD #index :val")
  25. .withExpressionAttributeValues(u)
  26. .withExpressionAttributeNames(n)
  27. .withReturnValues(ReturnValue.UPDATED_NEW);
  28. UpdateItemResult res = this.client.updateItem(req);
  29. Map<String, AttributeValue> attr = res.getAttributes();
  30. int result = Integer.parseInt(attr.get("index").getN());

DynamoDB has four kind of update expressions: SET, ADD, REMOVE, DELETE, in the case, I use add expression: ADD #index + 1

- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
- SET: Modifying or Adding Item Attributes
- REMOVE: Deleting Attributes from an Item
- ADD: Updating Numbers and Sets
- DELETE: Removing Elements from a Set

Update if the item doesn't exist

https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/API_UpdateItem_v20111205.html
If you use ADD to increment or decrement a number value for an item that doesn't exist before the update, DynamoDB uses 0 as the initial value. Also, if you update an item using ADD to increment or decrement a number value for an attribute that doesn't exist before the update (but the item does) DynamoDB uses 0 as the initial value. For example, you use ADD to add +3 to an attribute that did not exist before the update. DynamoDB uses 0 for the initial value, and the value after the update is 3.

目前回應 Comments(1 comments)

  • ajay 2020/03/31

    Could please specify what is the table structure here? (columns etc)

    confuse between id & sequenceId column and what is schema of table?

    Reply

    Admin

    I put a dynamodb item which has a field id = "sequenceId" and field value = "1" , then update value by "Dynamodb add expression", it will return the added value of sequence id 

     

    field id: "sequenceId"

    field value: "1" (1,2,3,4...)

回應 (Leave a comment)