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
- // initiate dynamodb client
- AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
- .withRegion(region)
- .withCredentials(
- ...
- )
- .build()
- // primary key
- HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
- key.put("id", new AttributeValue().withS("sequenceId"));
- // expression value
- Map<String, AttributeValue> u = new HashMap<String, AttributeValue>();
- u.put(":val", new AttributeValue().withN("1"));
- // expression name
- Map<String, String> n = new HashMap<String, String>();
- n.put("#index", "index");
- UpdateItemRequest req = new UpdateItemRequest()
- .withTableName("table_name")
- .withKey(key)
- .withUpdateExpression("ADD #index :val")
- .withExpressionAttributeValues(u)
- .withExpressionAttributeNames(n)
- .withReturnValues(ReturnValue.UPDATED_NEW);
- UpdateItemResult res = this.client.updateItem(req);
- Map<String, AttributeValue> attr = res.getAttributes();
- 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
- 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.htmlIf 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(2 comments)
Harsh 2022/02/28
What if two hosts read the data at the same time and then update the data? Will that not lead to same id for both?
ReplyPlease answer. Need to solve this problem of different hosts reading data at the same time.
Admin
ajay 2020/03/31
Could please specify what is the table structure here? (columns etc)
Replyconfuse between id & sequenceId column and what is schema of table?
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...)