使用过滤器DynamoDB查询

发布于 2025-01-31 11:43:48 字数 854 浏览 2 评论 0原文

我喜欢编写一个DynamoDB查询,其中我为某个字段过滤,听起来很简单。 我发现的所有示例始终包括分区键值,这确实使我感到困惑,因为它是唯一的值,但我想要一个列表。 我将ID作为分区密钥,没有排序键或任何其他索引。我试图将合作伙伴添加为索引没有任何区别。

        AttributeValue attribute = AttributeValue.builder()
            .s(partner)
            .build();

    Map<String, AttributeValue> expressionValues = new HashMap<>();
    expressionValues.put(":value", attribute);
    Expression expression = Expression.builder()
            .expression("partner = :value")
            .expressionValues(expressionValues)
            .build();
    QueryConditional queryConditional = QueryConditional
            .keyEqualTo(Key.builder()
                    .partitionValue("id????")
                    .build());
    Iterator<Product> results = productTable.query(r -> r.queryConditional(queryConditional)

感谢任何帮助。我这一边有误解吗?

I like to write a dynamoDb query in which I filter for a certain field, sounds simple.
All the examples I find always include the partition key value, which really confuses me, since it is unique value, but I want a list.
I got id as the partition key and no sort key or any other index. I tried to add partner as an index did not make any difference.

        AttributeValue attribute = AttributeValue.builder()
            .s(partner)
            .build();

    Map<String, AttributeValue> expressionValues = new HashMap<>();
    expressionValues.put(":value", attribute);
    Expression expression = Expression.builder()
            .expression("partner = :value")
            .expressionValues(expressionValues)
            .build();
    QueryConditional queryConditional = QueryConditional
            .keyEqualTo(Key.builder()
                    .partitionValue("id????")
                    .build());
    Iterator<Product> results = productTable.query(r -> r.queryConditional(queryConditional)

Would appreciate any help. Is there a misunderstandig on my side?

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

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

发布评论

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

评论(1

狼亦尘 2025-02-07 11:43:48

DynamoDB具有两个不同但类似的操作 - 查询扫描

  • 扫描用于读取整个表格,包括所有分区键。
  • 查询用于读取特定的分区键 - 以及其中的所有排序键(或排序键的连续范围 - 因此,该密钥的昵称“ range键”)。

如果您的数据模型没有范围密钥,则查询与您无关 - 您应该使用扫描

但是,这意味着每次调用此查询时,整个表都会被读取。除非您的桌子很小,否则这没有经济意义,您应该重新考虑数据模型。例如,如果您经常通过“合作伙伴”属性查找结果,则可以考虑以“合作伙伴”作为分区密钥创建GSI(全球次级索引),从而使您可以快速而便宜具有给定“合作伙伴”值的项目列表,而无需扫描整个表格。

DynamoDB has two distinct, but similar, operations - Query and Scan:

  • Scan is for reading the entire table, including all partition keys.
  • Query is for reading a specific partition key - and all sort key in it (or a contiguous range of sort key - hence the nickname "range key" for that key).

If your data model does not have a range key, Query is not relevant for you - you should use Scan.

However this means that each time you call this query, the entire table will be read. Unless your table is tiny, this doesn't make economic sense, and you should reconsider your data model. For example, if you frequently look up results by the "partner" attribute, you can consider creating a GSI (global secondary index) with "partner" as its partition key, allowing you to quickly and cheapy fetch the list of items with a given "partner" value without scanning the entire table.

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