dynamodb:如何归档所有没有特定属性的项目?

Ali*_*iza 5 java amazon-dynamodb

我有一个用户表,主用哈希键为userId.每个用户可能/可能没有称为"环境"的字符串属性.我想让所有拥有"environment"="xyz"或没有"environment"属性的用户.

以下代码将使用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);
Run Code Online (Sandbox Code Playgroud)

现在我刚刚删除了扫描过滤器,我在客户端进行过滤.有没有办法在服务器方面做到这一点?

谢谢,Aliza

Vad*_*dim 6

希望我不会太晚.我找到了你可以在查询中使用的有用函数.我没有检查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)
Run Code Online (Sandbox Code Playgroud)

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


rea*_*not 0

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

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

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

这对你有用吗?