我有一个用户表,主用哈希键为userId.每个用户可能/可能没有称为"环境"的字符串属性.我想让所有拥有"environment"="xyz"或没有"environment"属性的用户.
以下代码将使用environment = xyz过滤这些用户,但如何过滤那些没有环境的项目?Dynamo API不允许过滤空字符串.
AmazonDynamoDBClient client = DbClientManager.getDynamoDbClient(); ArrayListavList = new ArrayList (); avList.add(new AttributeValue().withS("xyz")); Condition scanFilterCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(avList); Map conditions = new HashMap<>(); conditions.put("environment", scanFilterCondition); ScanRequest scanRequest = new ScanRequest() .withTableName("users") .withAttributesToGet( "userId", "environment"); .withScanFilter(conditions); ScanResult result = client.scan(scanRequest);
现在我刚刚删除了扫描过滤器,我在客户端进行过滤.有没有办法在服务器方面做到这一点?
谢谢,Aliza
希望我不会太晚.我找到了你可以在查询中使用的有用函数.我没有检查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)