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.
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.
"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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With