简体   繁体   中英

How to put the constrain of duplicated entries in DynamoDB?

I am using DynamoDB under Python environment and I would like to use the following DynamoDB as an example:

id time value
1 1 1
2 2 3
1 2 4
1 3 4
1 4 6
  1. I would like to have a table that has unique id and time (not id or time ).
  2. I would like to put entries from time to time. I would like the put operation would fail if there is an entry with the same id and time already exist in the table.

For the second criteria, I would like to know if I need to perform a get operation first or I could perform a write operation and, by table design, could raise an exception and then I ignore the exception (because the entry already exist in DynamoDB).

You'll want to have your code use a partition key that is the compound value of id and time separated by something like a hash. Like 1#1 .

You can insert data with a PutItem having a conditional expression to fail if the PK already exists.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM