Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what will happen if we insert into dynamo DB with a duplicate hash key?

I am trying to insert into dynamo DB. When I call the putItem function what will happen if the hash key is already present in the DB? Does the PutItemResult object contain something which can tell us if a duplicate hash entry was attempted? I want to avoid running another query to check if there is an entry with the hash key I am using.

like image 368
coder Avatar asked Sep 17 '12 10:09

coder


People also ask

Can DynamoDB have two hash keys?

If you want to both attributes together to be used as the key as a pair (called "compound" hash key in other databases), you can't - this isn't supported in DynamoDB. It can be approximated by creating one attribute containing the concatenation (for example) of the two keys.

Is DynamoDB hash key unique?

"Hash and Range Primary Key" means that a single row in DynamoDB has a unique primary key made up of both the hash and the range key.

Can DynamoDB have same partition key?

DynamoDB uses the partition key's value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. In a table that has only a partition key, no two items can have the same partition key value.

Does DynamoDB primary key need to be unique?

When creating a DynamoDB table, you must specify a primary key. Each item that you write into your table must include the primary key, and the primary key must uniquely identify each item. This table is storing Orders in an e-commerce application.


2 Answers

If you insert an item on an existing primary key, it will be overwritten unless you use the "expected values". Here is the introduction of the official documentation:

http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_PutItem.html

Creates a new item, or replaces an old item with a new item (including all the attributes). If an item already exists in the specified table with the same primary key, the new item completely replaces the existing item. You can perform a conditional put (insert a new item if one with the specified primary key doesn't exist), or replace an existing item if it has certain attribute values.

Note

To ensure that a new item does not replace an existing item, use a conditional put operation with Exists set to false for the primary key attribute, or attributes.

Otherwise, you can also use UpdateItem to update fields of a pre-existing item: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_UpdateItem.html

like image 178
yadutaf Avatar answered Sep 28 '22 19:09

yadutaf


You can use 'withReturnValues(ReturnValue.ALL_OLD)' that will return a Map from PutItemResult.getAttributes of the values that were there before the insertion.

If PutItemResult.getAttributes returns null, it was a new entry.

like image 32
Lee Winder Avatar answered Sep 28 '22 18:09

Lee Winder