DynamoDB:写入事务中全局二级索引的条件检查
我有一个 DynamoDB 表,它跟踪两个 ID:
- 第一个 ID (A) 用作表中记录的分区和排序键,并在保存记录时随机生成。
- 第二个 ID (B) 不是随机生成的,它正在跟踪另一个系统中的实体。表中应该只有一条记录具有唯一的 B(实际上,如果这是 SQL,第二个 ID 字段将具有
UNIQUE
约束)。
该表有一个全局二级索引,分区键设置为B,排序键设置为A;充当主表中记录的反向查找。
我想将一条记录放入此主表中,但只有在没有已存在 B 的记录时才这样做。具体来说,我想:
- 检查 GSI 中是否不存在 B
- 调用另一个系统执行某些操作 放置
- 一条包含 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:
- Check that B doesn't exist in the GSI
- Call out to another system to do something
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能进行包含 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/