如何将重复条目的约束放在DynamoDB中?

发布于 2025-02-12 12:18:41 字数 723 浏览 1 评论 0 原文

我在Python环境下使用DynamoDB,我想以以下DynamoDB为例:

ID 时间
1 1 1 1
1 2 2 3
1 2 4
1 3 4
1 4 6
  1. 我想拥有一个具有唯一 id 和时间(不是 id time )。
  2. 我想不时放入条目。我希望,如果有相同的 id time 的条目,则PUT操作将失败。

对于第二个标准,我想知道我是否需要先进行 get 操作,否则我可以执行写操作,并且可以通过表设计提出异常,然后我忽略了例外(因为该条目已经存在于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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2025-02-19 12:18:41

您需要让您的代码使用一个分区密钥,该键是ID的复合值,并由诸如哈希(Hash)之类的时间分隔。喜欢 1#1

您可以使用具有条件表达式的PutItem插入数据,如果PK已经存在,则会失败。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文