[英]Date range query in DynamoDB local
我在本地使用 DynamoDB 来测试服务。 我需要找到自给定日期起过去 12 个月的所有医疗记录 ID。 我正在尝试使用 DynamoDB 映射器使用查询。
以下是模型类:
public @Data class MedicalRecord {
@DynamoDBHashKey(attributeName = "medicalRecordId")
private Integer medicalRecordId;
@DynamoDBRangeKey(attributeName = "date")
private String date;
@DynamoDBAttribute
private int healthCareProviderId;
@DynamoDBAttribute
private int systolicBloodPressure;
@DynamoDBAttribute
private int diastolicBloodPressure;
@DynamoDBAttribute
private int heartRate;
@DynamoDBAttribute
private double temperature;
@DynamoDBAttribute
private int heightInCms;
@DynamoDBAttribute
private int weightInLbs;
}
我正在从文件中读取 JSON 格式的测试数据并将其写入 DynamoDB。
private void populateMedicalRecordTable() throws FileNotFoundException, ParseException {
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
FileReader fileReader = new FileReader("src/main/resources/medical_records");
MedicalRecord[] medicalRecords = gson.fromJson(fileReader, MedicalRecord[].class);
for(MedicalRecord medicalRecord : medicalRecords) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = dateFormatter.parse(medicalRecord.getDate());
Date date1 = new Date();
date1.setTime(date.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String newDateStr = dateFormat.format(date1);
medicalRecord.setDate(newDateStr);
System.out.println("Date is "+medicalRecord.getDate());
dynamoDBMapper.save(medicalRecord);
}
}
以下是我尝试查询 dynamoDB 本地的方式:
public List<MedicalRecord> queryMedicalRecords(Integer id, Date date) {
long oneYearAgoMillis = date.getTime() - (365L * 24L * 60L * 60L * 1000L);
Date oneYearAgo = new Date();
oneYearAgo.setTime(oneYearAgoMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String newDateStr = dateFormat.format(oneYearAgo);
System.out.println("Using ISO8601Utils String is "+newDateStr);
Map<String, AttributeValue> map = new HashMap<>();
map.put(":val1", new AttributeValue().withN(String.valueOf(id)));
map.put(":val2", new AttributeValue().withS(newDateStr));
DynamoDBQueryExpression<MedicalRecord> dynamoDBQueryExpression = new DynamoDBQueryExpression<MedicalRecord>()
.withKeyConditionExpression("medicalRecordId = :val1 and date >= :val2")
.withExpressionAttributeValues(map);
return dynamoDBMapper.query(MedicalRecord.class, dynamoDBQueryExpression);
}
上面的一个给出了以下例外。
2021-06-18 19:24:40.658 ERROR 50181 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.amazonaws.AmazonServiceException: Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 461a0144-cbd0-4d8b-9850-0690b0e13602; Proxy: null)] with root cause
com.amazonaws.AmazonServiceException: Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 461a0144-cbd0-4d8b-9850-0690b0e13602; Proxy: null)
我的代码有什么问题?
我使用属性名称作为日期,这是 DynamoDB 保留关键字。 更改属性名称后,我能够得到响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.