Java dynamodb:如何过滤所有没有特定属性的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23995387/
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-08-14 09:36:56  来源:igfitidea点击:

dynamodb: how to filer all items which do not have a certain attribute?

javaamazon-dynamodb

提问by Aliza

I have a table of users with a primary hash key of userId. each user may/may not have a string attribute called "environment". I would like to get all the users which have "environment"="xyz" or which do not have the "environment" attribute.

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

The following code will filter those users with environment=xyz, but how do I filter those items with no environment at all? Dynamo API will not allow to filter on an empty String.

以下代码将使用 environment=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);

For now I just dropped the scan filter, and I do the filtering client-side. Bit is there any way to do it server side?

现在我只是放弃了扫描过滤器,我在客户端进行过滤。位有什么办法可以做到服务器端?

Thanks, Aliza

谢谢,艾丽莎

回答by readyornot

You need to use the NULLComparisonOperator.

您需要使用NULL比较运算符。

Check out this link: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html

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

  • NOT_NULL : The attribute exists.
  • NULL : The attribute does not exist.
  • NOT_NULL :属性存在。
  • NULL :该属性不存在。

Does this work for you?

这对你有用吗?

回答by Vadim

Hope I'm not too late. I've found useful function which you could use in the query. I did not check with ScanRequest but with QueryRequest works as charm.

希望我还不算太晚。我找到了可以在查询中使用的有用函数。我没有检查 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) works with ":aws-sdk:1.11.11" also you could use attribute_exists(yourAttributeName)

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

回答by Sahil

I my case also, attribute_not_exists(attribute) worked. You can refer to this question:- How to check whether an attribute is not present in dynamoDB filter expression

我的情况也是如此,attribute_not_exists(attribute) 起作用了。您可以参考这个问题:-如何检查 dynamoDB 过滤器表达式中是否不存在属性

for more details.

更多细节。