0

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).

Poala Astrid
  • 1,028
  • 2
  • 10
Winston
  • 1,308
  • 5
  • 16
  • 34

1 Answers1

0

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

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
  • Thanks for the answer. I am also reading some DynamoDB related materials. I would like to know if it would be a good idea to use time as the sort key? If time is the sort key, would it be possible that I can perform uniquness checking? – Winston Jul 04 '22 at 02:41
  • Just checked Local Secondary Indexes, they said **In a DynamoDB table, the combined partition key value and sort key value for each item must be unique** – Winston Jul 04 '22 at 03:17
  • You wouldn’t need an LSI. You can use the PK and SK in combination as the unique primary key. – hunterhacker Jul 05 '22 at 03:47
  • Thanks. I currently treat DynamoDB as a cache and I need to write 1000 entries per 10 seconds. After using the pricing calculator it seems the price is a bit high, based on the usage pattern. Therefore I switched to Elasticache eventually (faster write and cheaper in my case). But still I would like to thank for your answer. – Winston Jul 05 '22 at 05:35
  • During the process I use id as the partition key and time as the sort key. Then I can only have one item per partition key and sort key. – Winston Jul 05 '22 at 05:41