boto3:用多种键值查询DynamoDB
在boto3中进行查询时,有什么方法可以为DynamoDB表的排序键提供多个值?
要搜索单个SK值,我正在这样做:
table.query(
IndexName="my_gsi",
KeyConditionExpression=Key('my_gsi_pk').eq({pk value}) & Key('my_gsi_sk').eq({sk value}),
FilterExpression={filter expression}
)
...哪个有效。
但是,我的场景涉及在几个潜在的SK值之一上搜索,因此我想用SQL术语进行类似的操作:
WHERE my_gsi_pk = {pk value}
AND my_gsi_sk IN ({sk value 1}, {sk value 2})
我在.query()部分中的boto3文档中查看并集中精力 keyConditionexpression
语法,但无法识别是否可能。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QUERY
api 运算符中支持keyconditionexpression
。使用 API而不是API。这将执行a partiql =“ https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.select.html#ql-reference.select.select.parameters” rel =“ nofollow noreferrer”> 运算符在查询操作中的分区和排序键:
这将创建一个partiql语句,例如
从“ my_table”中选择 *。“ my_gsi”。 [?,?]
和诸如[{“ s”:“ 1”},{“ s”:“ foo”},{“ foo”},{“ s”:“ bar”}的替换参数和替换参数,
。The
query
API does not support theIN
operator in theKeyConditionExpression
.Use the execute_statement API instead. This executes a PartiQL statement, which does accept the
IN
operator in query operations for the Partition and Sort keys:This creates a PartiQL Statement like
SELECT * FROM "my_table"."my_gsi" WHERE my_gsi_pk = ? AND my_gsi_sk IN [?, ?]
and substitution Parameters like[{"S": "1"}, {"S": "Foo"}, {"S": "Bar"}]
.请注意,PartiQl将花费比查询更多的 rcu 。您可以通过请求returnConsumedCapacity = returnConsumedCapacity.total进行检查
Please note that the PartiQL will spend much more RCU than the Query. You can check this by requesting ReturnConsumedCapacity = ReturnConsumedCapacity.TOTAL