DynamoDB:写入事务中全局二级索引的条件检查

发布于 2025-01-10 22:11:29 字数 581 浏览 0 评论 0原文

我有一个 DynamoDB 表,它跟踪两个 ID:

  • 第一个 ID (A) 用作表中记录的分区和排序键,并在保存记录时随机生成。
  • 第二个 ID (B) 不是随机生成的,它正在跟踪另一个系统中的实体。表中应该只有一条记录具有唯一的 B(实际上,如果这是 SQL,第二个 ID 字段将具有 UNIQUE 约束)。

该表有一个全局二级索引,分区键设置为B,排序键设置为A;充当主表中记录的反向查找。

我想将一条记录放入此主表中,但只有在没有已存在 B 的记录时才这样做。具体来说,我想:

  1. 检查 GSI 中是否不存在 B
  2. 调用另一个系统执行某些操作 放置
  3. 一条包含 B 的记录和随机生成的 < em>A

使用 DynamoDB 事务写入可以实现这一点吗?从文档来看,有一个 ConditionCheck 操作在这里可能有用;但据我所知,似乎不可能对 GSI 运行条件检查,或者我在这里遗漏了一些东西?

I have a DynamoDB table which is tracking two IDs:

  • The first ID (A) is used as the partition and sort key of the records in the table, and is generated randomly when the record is saved.
  • The second ID (B) is not randomly generated, and is tracking an entity in another system. There should only ever be one record with a unique B in the table (effectively the second ID field would have a UNIQUE constraint if this was SQL).

This table has a Global Secondary Index, with the partition key set to B and the sort key set to A; acting as a reverse lookup for the records that are in the main table.

I would like to put a record into this main table, but only do so if there's no record with B already present. Specifically, I'd like to:

  1. Check that B doesn't exist in the GSI
  2. Call out to another system to do something
  3. Put a record with B and a randomly generated A

Is this possible using DynamoDB transactional writes? From looking at the documents, there is a ConditionCheck operation that could be useful here; but as far as I can tell, it doesn't seem possible to run a condition check on a GSI, or am I missing something here?

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

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

发布评论

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

评论(1

夏尔 2025-01-17 22:11:29

您不能进行包含 GSI 的交易。我建议您遵循这篇博客文章的设计,在基表中放置一个条目,并以 B 作为主键来启用唯一性检查(也许然后使用它而不是 GSI):

https://aws.amazon.com/blogs/database/simulated-amazon-dynamodb-unique-constraints-using-transactions/

You cannot do transactions that include GSIs. I would recommend you follow the design of this blog post, putting an entry in the base table with B as the primary key to enable the uniqueness check (maybe then using it instead of a GSI):

https://aws.amazon.com/blogs/database/simulating-amazon-dynamodb-unique-constraints-using-transactions/

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