DynamoDB - Item Update

Updating an item in DynamoDB basically consists of specifying the fully qualified primary key and table name for the item. A new value is required for each attribute you change. The operation uses UpdateItem , which modifies existing items or creates them when a missing item is found.
Updates may need to track changes by displaying the original and new values ​​before and after operations. UpdateItem uses the ReturnValues ​​parameter to achieve this.
Note . The operation does not report capacity consumption, but you can use the ReturnConsumedCapacity parameter .
Use the GUI console, Java, or any other tool to complete this task.
How to update elements using GUI tools?
Go to console. In the navigation bar on the left, select Tables . Select the desired table and then click the Elements tab .
Select the item you want to update and select Actions | Edit
Change any attributes or values ​​needed in the Edit Element window .
Updating elements with Java
Using Java in item update operations requires creating an instance of the Table class and calling its updateItem method . You then specify the element's primary key and provide detailed modifications to the UpdateExpression attributes .
Following is an example of the same −
DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient ( new ProfileCredentialsProvider ())); Table table = dynamoDB . getTable ( "ProductList" ); Map < String , String > expressionAttributeNames = new HashMap < String , String >(); expressionAttributeNames . put ( "#M" , "Make" ); expressionAttributeNames . put ( "#P" , "Price expressionAttributeNames.put(" #N", "ID"); Map < String , Object > expressionAttributeValues ​​= new HashMap < String , Object >(); expressionAttributeValues ​​. put ( ":val1" , new HashSet < String >( Arrays . asList ( "Make1" , "Make2" ))); expressionAttributeValues ​​. put ( ":val2" , 1 ); //price UpdateItemOutcome outcome = table . updateItem ( "internalID" , // key attribute name 111 , // key attribute value "add #M :val1 set #P = #P - :val2 remove #N" , // UpdateExpression expressionAttributeNames , expressionAttributeValues ​​);
The updateItem method also allows you to specify conditions, which can be seen in the following example:
Table table = dynamoDB . getTable ( "ProductList" ); Map < String , String > expressionAttributeNames = new HashMap < String , String >(); expressionAttributeNames . put ( "#P" , "Price" ); Map < String , Object > expressionAttributeValues ​​= new HashMap < String , Object >(); expressionAttributeValues ​​. put ( ":val1" , 44 ); // change Price to 44 expressionAttributeValues ​​. put ( ":val2" , 15 ); // only if currently 15 UpdateItemOutcome outcome = table . updateItem ( new PrimaryKey ( "internalID" , 111 ), "set #P = :val1" , // Update "#P = :val2" , // Condition expressionAttributeNames , expressionAttributeValues ​​);
Updating Items with Counters
DynamoDB allows atomic counters, which means using UpdateItem to increment/decrement attribute values ​​without affecting other queries; Moreover, the counters are always updated.
Below is an example that explains how this can be done.
Note. The following example can use a previously created data source. Before attempting to execute, acquire the supporting libraries and create the necessary data sources (tables with the required characteristics or other referenced sources).
This example also uses the Eclipse IDE, an AWS credential file, and the AWS Toolkit in an Eclipse AWS Java project.