AWS DynamoDB多个" NE"字符串过滤器? [英] AWS DynamoDB multiple "NE" string filters?
问题描述
我试图扫描/查询一个AWS DynamoDB表的项目列表,其中ID(一个字符串)不等于任何字符串A,B,C,D等。
I'm trying to scan/query an AWS DynamoDB table for a list of items where id (a single string) is not equal to any of strings A, B, C, D, etc.
我已经试过这样的事情:
I've tried something like this:
for (String itemString : items) {
scanExpression.addFilterCondition("id",
new Condition().withComparisonOperator(ComparisonOperator.NE)
.withAttributeValueList(new AttributeValue().withS(itemString)));
}
PaginatedScanList<Items> result = mapper.scan(Item.class, scanExpression);
什么似乎发生的是,我每次添加过滤器时,它会覆盖previous值,使扫描仅针对一种itemString检查,没有几个。我失去了一些东西,或者这是DynamoDB的限制?
What appears to happen is that each time I add filter, it overwrites the previous values, so that the scan only checks against one itemString, not several. Am I missing something, or is this a limitation of DynamoDB?
请注意:如果是重要的,在我的例子,我列出了四个值(A,B,C,D),但在生产中,这可能是数百个值的列表。
Note: if it matters, in my example, I listed four values (A, B, C, D) but in production, this may be a list of hundreds of values.
推荐答案
您是说,你正在做的方式将覆盖previous值是正确的。下面是从23年1月9日SDK相关code对 DynamoDBScanEx pression
:
You are correct in saying that the way you are doing it "overwrites the previous values". Here is the relevant code from the 1.9.23 SDK on DynamoDBScanExpression
:
public void addFilterCondition(String attributeName, Condition condition) {
if ( scanFilter == null )
scanFilter = new HashMap<String, Condition>();
scanFilter.put(attributeName, condition);
}
由于您的的attributeName
将 ID
为看跌
,最后一个值将在这种情况下,获胜。
Since your attributeName
will be id
for both puts
, the last value will win in this case.
在我看来,这是从 DynamoDBScanEx pression
不良行为,我甚至会更倾向于说这是应该报告的错误。该文档没有说明何时重复的attributeName
,并将该方法名称使它看起来这是意外的行为不谈。
In my opinion, this is poor behavior from DynamoDBScanExpression
, and I would even lean more towards saying it is a bug that should be reported. The documentation does not state anything about when a duplicate attributeName
is added and the method name makes it seem like this is unexpected behavior.
我看不到解决这个以外建造出整个过滤器前pression的好方法。
I don't see a good way to work around this other than building out the entire filter expression.
另注:在文档,我不看不到一个过滤器前pression能维持多久是一个长度约束,以及有多少防爆pressionAttributeNames
和防爆pressionAttributeValues
允许一个请求。这可能会考虑,如果你正在试图筛选出一吨的属性值,但我还没有发现什么限制可能还是你应该期望什么样的行为的任何文档。
Another Note: On the documentation, I don't see a length constraint for how long a filter expression can be as well as how many ExpressionAttributeNames
and ExpressionAttributeValues
are allowed on a request. That may come into account if you are trying to filter out a ton of attribute values, but I haven't found any documentation of what that limit might be or what behavior you should expect.
StringJoiner filterExpression = new StringJoiner(" AND ");
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
int itemAttributeSuffix = 0;
for (String itemString : items) {
StringBuilder expression = new StringBuilder("#idname")
.append(" ")
.append("<>")
.append(" ")
.append(":idval")
.append(itemAttributeSuffix);
filterExpression.add(expression);
expressionAttributeValues.put(":idval" + itemAttributeSuffix++,
new AttributeValue().withS(itemString));
}
Map<String, String> expressionAttributeNames = Collections.singletonMap("#idname", "id");
scanExpression.setFilterExpression(filterExpression.toString());
scanExpression.setExpressionAttributeNames(expressionAttributeNames);
scanExpression.setExpressionAttributeValues(expressionAttributeValues);
PaginatedScanList<Items> result = mapper.scan(Item.class, scanExpression);
这篇关于AWS DynamoDB多个&QUOT; NE&QUOT;字符串过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!