0

DynamoDB local を使用してサービスをテストしています。指定された日付から過去 12 か月のすべての medicalRecordIds を検索する必要があります。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