I am using DynamoDB local to test the service. I need to find all medicalRecordIds of last 12 months from given date. I am trying to use query using DynamoDB mapper.
Following is the model class:
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;
}
I am reading test data in JSON format from a file and writing it to 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);
}
}
Following is how I am trying to query dynamoDB local:
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);
}
Above one is giving following exception.
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)
What is wrong in my code?
I was using the attribute name as date which is DynamoDB reserved keyword. After changing the attribute name I was able to get the response.