Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a new key-value pair into a map in DynamoDB? (java)

Tags:

I have a DynamoDB attribute whose value is a map from a Number to a String. I am trying to put in a new key-value pair. From what I've read, this seems to be possible, but I don't know how.

I assume the solution is similar to the one in the link below:

How to update a Map or a List on AWS DynamoDB document API?

But I do not believe the example is on putting in a new item to a map. Could someone show me how to put in an item to a map?

Thanks.

EDIT:

I do not want to get the item, locally make the changes, and put it back. I am working with multiple clients who might interact concurrently (and I assume the update by dynamo ensures there will be no race conditions).

like image 299
Denis Li Avatar asked Jun 30 '15 14:06

Denis Li


People also ask

How do I add a foreign key in DynamoDB?

DynamoDB does not have foreign keys. It is a NoSQL database that doesn't support that kind of relational data. There is no data integrity maintained between tables, so this sort of behavior is not built in. If you want to have this sort of behavior, you will have to model your data storage in DynamoDB a different way.

What is map in DynamoDB?

A map is similar to a JSON object. There are no restrictions on the data types that can be stored in a map element, and the elements in a map do not have to be of the same type. Maps are ideal for storing JSON documents in DynamoDB.

What is item in DynamoDB?

In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How it works. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality.


2 Answers

With the following arguments to UpdateItem, you can condition adding a map entry at #number when map.#number does not exist in the map already:

UpdateExpression = "SET map.#number = :string" ExpressionAttributeNames = { "#number" : "1" } ExpressionAttributeValues = { ":string" : "the string to store in the map at key value 1" } ConditionExpression = "attribute_not_exists(map.#number)" 
like image 175
Alexander Patrikalakis Avatar answered Oct 23 '22 22:10

Alexander Patrikalakis


Visit http://www.tryyourskill.com/aws/insert-or-append-key-values-map-in-dynamodb-using-java may help you with the code snippet to add or append values in map column

public boolean insertKeyValue(String tableName, String primaryKey, String      primaryKeyValue, String updateColumn, String newKey, String newValue) {      //Configuration to connect to DynamoDB     Table table = dynamoDB.getTable(tableName);     boolean insertAppendStatus = false;     try {         //Updates when map is already exist in the table         UpdateItemSpec updateItemSpec = new UpdateItemSpec()             .withPrimaryKey(primaryKey, primaryKeyValue)             .withReturnValues(ReturnValue.ALL_NEW)             .withUpdateExpression("set #columnName." + newKey + " = :columnValue")             .withNameMap(new NameMap().with("#columnName", updateColumn))             .withValueMap(new ValueMap().with(":columnValue", newValue))             .withConditionExpression("attribute_exists("+ updateColumn +")");          table.updateItem(updateItemSpec);         insertAppendStatus = true;     //Add map column when it's not exist in the table     } catch (ConditionalCheckFailedException e) {         HashMap<String, String> map =  new HashMap<>();         map.put(newKey, newValue);         UpdateItemSpec updateItemSpec = new UpdateItemSpec()             .withPrimaryKey(primaryKey,primaryKeyValue)             .withReturnValues(ReturnValue.ALL_NEW)             .withUpdateExpression("set #columnName = :m")             .withNameMap(new NameMap().with("#columnName", updateColumn))             .withValueMap(new ValueMap().withMap(":m", map));          table.updateItem(updateItemSpec);         insertAppendStatus = true;     } catch(Exception e) {         e.printStackTrace();     }     return insertAppendStatus; } 
like image 22
Surajit Kundu Avatar answered Oct 24 '22 00:10

Surajit Kundu