使用过滤器DynamoDB查询
我喜欢编写一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DynamoDB具有两个不同但类似的操作 -
查询
和扫描
:扫描
用于读取整个表格,包括所有分区键。查询
用于读取特定的分区键 - 以及其中的所有排序键(或排序键的连续范围 - 因此,该密钥的昵称“ range键”)。如果您的数据模型没有范围密钥,则
查询
与您无关 - 您应该使用扫描
。但是,这意味着每次调用此查询时,整个表都会被读取。除非您的桌子很小,否则这没有经济意义,您应该重新考虑数据模型。例如,如果您经常通过“合作伙伴”属性查找结果,则可以考虑以“合作伙伴”作为分区密钥创建GSI(全球次级索引),从而使您可以快速而便宜具有给定“合作伙伴”值的项目列表,而无需扫描整个表格。
DynamoDB has two distinct, but similar, operations -
Query
andScan
: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 useScan
.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.