我如何在DynamoDB中使用IN条件

地面

我需要通过用户ID从DynamoDB提取数据,因为它要使用IN条件,但找不到使用方法的说明,您能帮我吗?我有一些代码,如何替换userId = :id1 OR userId = :id2userId IN :ids如何设置userIds:ids

public List<Data> getByUserIds(List<Long> userIds) {
    Table table = dynamoDB.getTable(getTableName());
    ScanSpec scanSpec = new ScanSpec()
            .withFilterExpression("userId = :id1 OR userId = :id2")
            .withValueMap(new ValueMap()
                    .withNumber(":id1", userIds.get(0))
                    .withNumber(":id2", userIds.get(1))
            );
    ItemCollection<ScanOutcome> scanOutcome = table.scan(scanSpec);
    return convertItemToData(scanOutcome.iterator());
}

谢谢!

地面

我已经实现了下面的解决方案

public List<HistoricalData> getByUserIdAndLocation(List<Long> userIds) {
    Table table = dynamoDB.getTable(getTableName());
    ScanSpec scanSpec = new ScanSpec()
            .withFilterExpression("userId IN (" + buildInFilterExpression(userIds) + ")")
            .withValueMap(
                    buildValueMap(userIds)
            );
    ItemCollection<ScanOutcome> scanOutcome = table.scan(scanSpec);
    return convertItemToHistoricalData(scanOutcome.iterator());
}

private String buildInFilterExpression(List<Long> userIds) {
    StringBuilder builder = new StringBuilder();
    for (Long id : userIds) {
        builder.append(":user" + id + ",");
    }
    return builder.toString().substring(0, builder.length() - 1);
}

private ValueMap buildValueMap(List<Long> userIds) {
    ValueMap valueMap = new ValueMap();
    for (Long id : userIds) {
        valueMap.withNumber(":user" + id, id);
    }
    return valueMap;
}

我认为需要使用valueMap.withList代替,valueMap.withNumber但这对我不起作用...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章