boto3:用多种键值查询DynamoDB

发布于 2025-02-06 23:55:29 字数 595 浏览 2 评论 0 原文

在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 语法,但无法识别是否可能。

Is there any way of supplying multiple values for a DynamoDB table's Sort Key whilst doing a query in Boto3?

For a single SK value to search on, I'm doing this:

    table.query(
        IndexName="my_gsi",
        KeyConditionExpression=Key('my_gsi_pk').eq({pk value}) & Key('my_gsi_sk').eq({sk value}),
        FilterExpression={filter expression}
    )

... which works.

However, my scenario involves searching on one of a couple of potential SK values, so I'd like to, in SQL terms, do something like this:

    WHERE my_gsi_pk = {pk value}
    AND   my_gsi_sk IN ({sk value 1}, {sk value 2})

I've looked in the Boto3 documentation in the .query() section and concentrated upon the KeyConditionExpression syntax but can't identify whether this is possible or not.

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

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

发布评论

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

评论(2

执手闯天涯 2025-02-13 23:55:31

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”> 运算符在查询操作中的分区和排序键:

sk = ["Foo", "Bar"]

res = client.execute_statement(
  Statement=f'SELECT * FROM "my_table"."my_gsi" WHERE my_gsi_pk = ? AND my_gsi_sk IN [{",".join(["?" for k in sk])}]',
  Parameters= [{"S": "1"}] + [{"S": k} for k in sk]
)

这将创建一个partiql语句,例如 从“ my_table”中选择 *。“ my_gsi”。 [?,?] 和诸如 [{“ s”:“ 1”},{“ s”:“ foo”},{“ foo”},{“ s”:“ bar”}的替换参数和替换参数,

The query API does not support the IN operator in the KeyConditionExpression.

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:

sk = ["Foo", "Bar"]

res = client.execute_statement(
  Statement=f'SELECT * FROM "my_table"."my_gsi" WHERE my_gsi_pk = ? AND my_gsi_sk IN [{",".join(["?" for k in sk])}]',
  Parameters= [{"S": "1"}] + [{"S": k} for k in sk]
)

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"}].

染年凉城似染瑾 2025-02-13 23:55:31

请注意,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

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