AWS DynamoDB - 在 java 中的单个非键属性上组合多个查询过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27135725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:18:12  来源:igfitidea点击:

AWS DynamoDB - combining multiple query filters on a single non-key attribute in java

javaamazon-web-servicesamazon-dynamodbnosql

提问by ben.bourdin

Earlier this year Amazon announcedsupport of query filters on non-key attributes.

今年早些时候,亚马逊宣布支持对非关键属性的查询过滤器。

Can conditions be combined on a single attribute value? For example, in this scenario I would like to retrieve all items which do not match a certain list of values in a single 'non-key' column.

条件可以组合在单个属性值上吗?例如,在这种情况下,我想检索与单个“非键”列中的特定值列表不匹配的所有项目。

Their documentation states that each condition can only hold one attribute value for comparisons like NOT_EQUALSor BEGINS_WITH. The following therefore does not work:

他们的文档指出,每个条件只能保存一个属性值进行比较,例如NOT_EQUALSBEGINS_WITH。因此,以下方法不起作用:

HashMap<String, Condition> queryFilter = new HashMap<String, Condition>();

List<AttributeValue> AttributeValues = new ArrayList<AttributeValue>();
AttributeValues.add(new AttributeValue().withS("someValue1"));
AttributeValues.add(new AttributeValue().withS("someValue2"));

Condition attributeCondition = new Condition()
    .withComparisonOperator(ComparisonOperator.NE)
    .withAttributeValueList(AttributeValues);

queryFilter.put("COLUMN_1", attributeCondition);

DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>()
    .withHashKeyValues(itemKey)
    .withQueryFilter(queryFilter)
    .withLimit(pPageSize);

It looks like only the INcomparison operator can hold a list of attribute values. Ideally these conditions should be chainable? Since the query filter is a hash map we cannot put multiple conditions on the same column (I've tried):

看起来只有IN比较运算符才能保存属性值列表。理想情况下,这些条件应该是可链接的吗?由于查询过滤器是一个哈希映射,我们不能在同一列上放置多个条件(我已经尝试过):

Condition c1 = new Condition()
    .withAttributeValueList(new AttributeValue().withS("someValue1"))
    .withComparisonOperator(ComparisonOperator.NE);

Condition c2 = new Condition()
    .withAttributeValueList(new AttributeValue().withS("someValue2"))
    .withComparisonOperator(ComparisonOperator.NE);

DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>()
    .withHashKeyValues(itemKey)
    .withConditionalOperator(ConditionalOperator.AND)
    .withQueryFilterEntry("COLUMN_1", c1)
    .withQueryFilterEntry("COLUMN_1", c2)
    .withLimit(pPageSize);

Any help or clarification would be greatly appreciated!

任何帮助或澄清将不胜感激!

Thanks

谢谢

回答by ben.bourdin

So turns out this is made possible by adding a FilterExpression to the query (introduced recently in this blog post)

所以事实证明这是通过向查询添加 FilterExpression 来实现的(最近在这篇博文中介绍)

I had seen this in the DynamoDB documentationbut hadn't upgraded to the latest AWS Java SDK :(

我在DynamoDB 文档中看到过这个,但还没有升级到最新的 AWS Java SDK :(

Using my above example this would look like:

使用我上面的例子,这看起来像:

Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":val1", new AttributeValue().withS("someValue1"));
expressionAttributeValues.put(":val2", new AttributeValue().withS("someValue2"));

DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>()
    .withHashKeyValues(itemKey)
    .withFilterExpression("COLUMN_1 <> :val1 AND COLUMN_1 <> :val2")
    .withExpressionAttributeValues(expressionAttributeValues)
    .withLimit(pPageSize);

Thanks AWS Support!

感谢 AWS 支持!