我正在使用 springboot 和 DynamoDB 作为数据库创建一个 REST 服务。我有一个 StudentDTO 类,其中包含以下字段和 Subject 类,其主题信息为 (key,value pair) :
@DynamoDBTable(tableName="Students")
Class Students{
@DynamoDBHashKey(attributeName="id")
int id;
String name;
int year;
@DynamoDBAttribute(attributeName="subjects")
List<Subjects> subjects; //it has serializedName("subjects")
//getter and setters
}
@DynamoDBDocument
Class Subjects{
String key;
String value;
}
我有一些需要获取数据的标准。这些标准可能会有所不同。下面是一些例子:
- 可以获取关于名称、id 的数据 ("id":1,"name":"joe")
- 可以获取关于姓名、主题信息的数据 ("name":"joe",subjects:"[{"key":"S1","value":"Geography"},{"key":"S2","value “:“历史”}]”)
- 可能需要姓名、年份的数据
没有可以构建查询的固定标准组合。我尝试使用 DynamoDBScanExpression,但无法确定如何设置 filterExpression。
创建了 (key,value) 的映射,例如 (,)
我可以创建属性名和属性值(都在 DynamoDBDAO.java 中):
Map<String, String> attributeNames = new HashMap<String, String>();
Map<String, AttributeValue> attributeValues = new HashMap<String, AttributeValue>();
StringJoiner filterExpression = new StringJoiner(" AND ");
if (null != json.getS_no()) {
StringBuilder expression = new StringBuilder();
attributeNames.put("#s_no", "s_no");
attributeValues.put(":sno", new AttributeValue().withN(json.getS_no()));
expression.append("#s_no").append(" ").append("<>").append(" ").append(":s_no");
filterExpression.add(expression);
}
if (null != json.getName()) {
StringBuilder expression = new StringBuilder();
attributeNames.put("#name", "name");
attributeValues.put(":name", new AttributeValue().withN(json.getName()));
expression.append("#name").append(" ").append("<>").append(" ").append(":name");
filterExpression.add(expression);
}
if(null != json.getSubjects()){
StringBuilder expression = new StringBuilder();
attributeNames.put("#subjects", "subjects");
attributeValues.put(":subjects", new AttributeValue().withS(map.keySet().toString()));
expression.append("#subjects").append(" ").append("IN").append(" ").append(":subjects");
}
...
and so on
扫描查询
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
.withFilterExpression(filterExpression.toString()) // HERE CONDITION CAN CHANGE ACCORDING TO THE ATTRIBUTES IN THE INPUT.
.withExpressionAttributeNames(attributeNames)
.withExpressionAttributeValues(attributeValues);
List<DBMapper> records = Dynamomapper.scan(DBMapper.class, scanExpression);
但是在 SCAN() 函数中它给出了异常:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:Students[subjects]; could not unconvert subjects
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:271)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:456)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.marshallIntoObjects(DynamoDBMapper.java:484)
at com.amazonaws.services.dynamodbv2.datamodeling.PaginatedScanList.<init>(PaginatedScanList.java:64)
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.scan(DynamoDBMapper.java:1458)
at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.scan(AbstractDynamoDBMapper.java:216)
at com.example.dynamodb.dao.DynamoDBDAO.findByAttributes(DynamoDBDAO.java:379)
请建议。