使用DynamoDB表进行IN查询不起作用

2020年9月21日 47点热度 0条评论

列表字段上带有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”:“安德鲁。绿色” }

解决方案如下:

使用了CONTAINS和OR的组合,并且有效。

ScanRequest scanRequest = new ScanRequest()
.withTableName("user")
.withFilterExpression("contains(subscribedTopics, :topic1) or contains(subscribedTopics, :topic2)")
.withProjectionExpression("userId")
.withExpressionAttributeValues(userAttributeValues);enter code here