我需要通过用户ID从DynamoDB提取数据,因为它要使用IN
条件,但找不到使用方法的说明,您能帮我吗?我有一些代码,如何替换userId = :id1 OR userId = :id2
为userId 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] 删除。
我来说两句