【问题标题】:Using Amazon/AWS DynamodB scanfilter with both OR and AND conditions in java在 java 中使用带有 OR 和 AND 条件的 Amazon/AWS DynamodB 扫描过滤器
【发布时间】:2021-03-19 06:26:09
【问题描述】:

我正在使用 dynamoDB 和 java。我有一个用例,我必须使用过滤器表达式进行扫描。我有名为 Order 的表和许多字段。对于我的扫描表达式,我需要三个名为 "field1","field2","field3"。

基本上我的查询看起来像

select * from Order where (field1 is  null OR field2 is null) AND (field3 != "Test")

我只能使用 OR 来形成扫描表达式,但我被困在如何在这里添加 AND 上。看起来我们只能使用一个运算符。我们还有其他方法可以实现这一目标吗?

以下是仅使用 OR 条件的代码 sn-p。我想在下面的 sn-p 中添加 AND 条件。

        final HashMap<String, Condition> scanFilter = new HashMap<>();
        scanFilter.put("field1", new Condition().withComparisonOperator(ComparisonOperator.NULL));
        scanFilter.put("field2", new Condition().withComparisonOperator(ComparisonOperator.NULL));
        final DynamoDBScanExpression expression = new DynamoDBScanExpression()
                .withConsistentRead(false)
                .withConditionalOperator(ConditionalOperator.OR)
                .withScanFilter(scanFilter);
        final ScanResultPage<SnapshotByGrantee> scanResult = scanPage(Order.class, expression, exclusiveStartKey, limit);
       

【问题讨论】:

  • 您是否使用 awscli 或其他方法验证了您提议的过滤器表达式确实有效?然后研究如何使用 Java SDK 来表达它。

标签: amazon-web-services amazon-dynamodb dynamodb-queries


【解决方案1】:

这就是我最终为使它工作所做的事情!!。

        Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
        expressionAttributeValues.put("col1", new AttributeValue().withS("value1"));
        expressionAttributeValues.put(":col3", new AttributeValue().withS("value2"));
        expressionAttributeValues.put(":col3", new AttributeValue().withS("value3"));
        Map<String, String> expressionAttributeNames= new HashMap<>();
        expressionAttributeNames.put("#col4", "col4");
        final DynamoDBScanExpression expression = new DynamoDBScanExpression()
                .withConsistentRead(false)
                .withFilterExpression("(attribute_not_exists("+something1+")  or  attribute_not_exists("+something2+") ) AND (not  (#col4 in (:test1, :test2, :test3 ))) ")
                .withExpressionAttributeNames(expressionAttributeNames)
                .withExpressionAttributeValues(expressionAttributeValues);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2019-07-26
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多