使用 DynamoDB table 的 IN 查询不工作
IN query with DynamoDB table is not working
列表字段中带有 IN 的 ScanRequest 与 DynamoDB 中的条目不匹配。
代码:
Map<String, AttributeValue> userAttributeValues = new HashMap<>();
userAttributeValues.put(":topic1", new AttributeValue().withS("5"));
userAttributeValues.put(":topic2", new AttributeValue().withS("11"));
ScanRequest scanRequest = new ScanRequest()
.withTableName("user")
.withFilterExpression("subscribedTopics IN (:topic1, :topic2)")
.withProjectionExpression("userId")
.withExpressionAttributeValues(userAttributeValues);
ScanResult result = dynamoDBClient.scan(scanRequest);
System.out.println("Number of results: " + result.getCount());
for (Map<String, AttributeValue> userItemsMap : result.getItems()) {
AttributeValue userIdAttributeValue = userItemsMap.get("userId");
String userId = userIdAttributeValue.getS();
}
具有有效条目的用户项目
{ "subscribedTopics": [ "2", "5", "8", "1", "3", "4", "6", "7", "11" ], "userId": "Andrew.Green" }
使用了 CONTAINS 和 OR 的组合并且有效。
ScanRequest scanRequest = new ScanRequest()
.withTableName("user")
.withFilterExpression("contains(subscribedTopics, :topic1) or contains(subscribedTopics, :topic2)")
.withProjectionExpression("userId")
.withExpressionAttributeValues(userAttributeValues);enter code here
列表字段中带有 IN 的 ScanRequest 与 DynamoDB 中的条目不匹配。 代码:
Map<String, AttributeValue> userAttributeValues = new HashMap<>();
userAttributeValues.put(":topic1", new AttributeValue().withS("5"));
userAttributeValues.put(":topic2", new AttributeValue().withS("11"));
ScanRequest scanRequest = new ScanRequest()
.withTableName("user")
.withFilterExpression("subscribedTopics IN (:topic1, :topic2)")
.withProjectionExpression("userId")
.withExpressionAttributeValues(userAttributeValues);
ScanResult result = dynamoDBClient.scan(scanRequest);
System.out.println("Number of results: " + result.getCount());
for (Map<String, AttributeValue> userItemsMap : result.getItems()) {
AttributeValue userIdAttributeValue = userItemsMap.get("userId");
String userId = userIdAttributeValue.getS();
}
具有有效条目的用户项目
{ "subscribedTopics": [ "2", "5", "8", "1", "3", "4", "6", "7", "11" ], "userId": "Andrew.Green" }
使用了 CONTAINS 和 OR 的组合并且有效。
ScanRequest scanRequest = new ScanRequest()
.withTableName("user")
.withFilterExpression("contains(subscribedTopics, :topic1) or contains(subscribedTopics, :topic2)")
.withProjectionExpression("userId")
.withExpressionAttributeValues(userAttributeValues);enter code here