【问题标题】:dynamodb: how to filer all items which do not have a certain attribute?dynamodb:如何过滤所有没有特定属性的项目?
【发布时间】:2020-04-15 01:34:10
【问题描述】:

我有一个用户表,其主哈希键为 userId。每个用户可能/可能没有称为“环境”的字符串属性。 我想获取所有具有“环境”=“xyz”或没有“环境”属性的用户。

下面的代码将过滤那些环境=xyz 的用户,但是我如何过滤那些根本没有环境的项目呢? Dynamo API 不允许过滤空字符串。

    AmazonDynamoDBClient client = DbClientManager.getDynamoDbClient();

    ArrayList<AttributeValue> avList = new ArrayList<AttributeValue>();
    avList.add(new AttributeValue().withS("xyz"));

    Condition scanFilterCondition = new Condition()
            .withComparisonOperator(ComparisonOperator.EQ.toString())
            .withAttributeValueList(avList);
    Map<String, Condition> conditions = new HashMap<>();
    conditions.put("environment", scanFilterCondition);

    ScanRequest scanRequest = new ScanRequest()
            .withTableName("users")
            .withAttributesToGet(
                    "userId", 
                    "environment");
            .withScanFilter(conditions);

    ScanResult result = client.scan(scanRequest);

现在我只是放弃了扫描过滤器,我在客户端进行过滤。位有没有办法在服务器端做到这一点?

谢谢, 艾丽莎

【问题讨论】:

    标签: java amazon-dynamodb


    【解决方案1】:

    希望我不会太晚。 我找到了可以在查询中使用的有用函数。我没有使用 ScanRequest 进行检查,但使用 QueryRequest 作为魅力。

    QueryRequest queryRequest = new QueryRequest()
                .withTableName("YouTableName")
        queryRequest.setFilterExpression(" attribute_not_exists(yourAttributeName) ")
        queryRequest.setExpressionAttributeValues(expressionAttributeValues)
        queryRequest.setExclusiveStartKey(ifYouHave)
        queryRequest.setSelect('ALL_ATTRIBUTES')
        queryRequest.setExpressionAttributeNames(youNames)
    

    attribute_not_exists(yourAttributeName) 适用于“:aws-sdk:1.11.11” 你也可以使用 attribute_exists(yourAttributeName)

    【讨论】:

      【解决方案2】:

      您需要使用NULLComparisonOperator。

      查看此链接:http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html

      • NOT_NULL : 属性存在。
      • NULL : 属性不存在。

      这对你有用吗?

      【讨论】:

      • 这行不通,因为我想在同一个属性上有 2 个条件(逻辑 OR)。当我将条件放入 Map 结构时,第二个条件会覆盖第一个条件(因为它们具有相同的键)。我可以过滤 environment=xyz 或 environment=NULL 但不能同时过滤。
      • 我明白了……我环顾四周,找不到办法。我能给出的唯一建议是将其发布到 Amazon DynamoDB 论坛:forums.aws.amazon.com/forum.jspa?forumID=131。我过去曾在那里发帖,亚马逊的某个人确实回复了我。您的问题比这个更复杂,例如forums.aws.amazon.com/thread.jspa?messageID=341426&#341426,因为您还需要处理缺少environment 属性。祝您搜索顺利!
      【解决方案3】:

      我的情况也是,attribute_not_exists(attribute) 有效。你可以参考这个问题:-How to check whether an attribute is not present in dynamoDB filter expression

      了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        • 2015-10-23
        • 2019-10-25
        • 2012-07-01
        • 2019-11-21
        相关资源
        最近更新 更多