首先,DynamoDB 中没有 JSON 数据类型。如果您的意思是将数据存储为 DynamoDB 数据类型 MAP,那么以下解决方案应该适合您。
简而言之,过滤器表达式应该如下所示:-
FilterExpression : 'records.K1 = :recordsK1Value and records.K2 = :recordsK2Value'
如果您只需要输出“records.K1”和“records.K2”,则可以使用项目表达式。
ProjectionExpression : 'records.K1, records.K2'
完整代码:-
public List<String> queryMoviesAndFilterByMapAttribute() {
List<String> moviesJsonList = new ArrayList<>();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Table table = dynamoDB.getTable("Movies");
QuerySpec querySpec = new QuerySpec();
querySpec.withKeyConditionExpression("yearkey = :yearval and title = :titleval")
//.withProjectionExpression("records.K1, records.K2")
.withFilterExpression("records.K1 = :recordsK1Value and records.K2 = :recordsK2Value").withValueMap(
new ValueMap().withNumber(":yearval", 1991).withString(":titleval", "Movie with map attribute")
.withString(":recordsK1Value", "V1").withString(":recordsK2Value", "V2"));
IteratorSupport<Item, QueryOutcome> iterator = table.query(querySpec).iterator();
while (iterator.hasNext()) {
Item movieItem = iterator.next();
System.out.println("Movie data ====================>" + movieItem.toJSONPretty());
moviesJsonList.add(movieItem.toJSON());
}
return moviesJsonList;
}
包含所有字段的示例输出(即没有项目表达式):-
Movie data ====================>{
"yearkey" : 1991,
"records" : {
"K1" : "V1",
"K2" : "V2",
"K3" : "V3",
"K4" : "V4"
},
"title" : "Movie with map attribute"
}
取消注释项目表达式后的示例输出:-
请注意,输出中不存在其他字段,例如 yearkey、title、K3 和 K4。
Movie data ====================>{
"records" : {
"K1" : "V1",
"K2" : "V2"
}
}