0

我正在使用本地 DynamoDB 来测试服务。我需要找到从给定日期起过去 12 个月的所有 MedicalRecordId。我正在尝试使用 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)

我的代码有什么问题?

4

1 回答 1

1

我使用属性名称作为 DynamoDB 保留关键字的日期。更改属性名称后,我能够得到响应。

于 2021-06-19T04:11:40.250 回答