用于对象的DynamoDB映射器注释,其中具有另一个对象的列表

时间:2019-03-04 22:07:10

标签: amazon-web-services annotations amazon-dynamodb aws-sdk dynamodb-queries

我正在尝试为以下情况创建dynamoDBMapper注释。

我有EmployeeLevelTrail,它是Employee级记录的一个类

@DynamoDBTable(tableName = TABLE_NAME)
public class EmployeeData {
    public final static String TABLE_NAME = “EmployeeDataRecord”;

    @DynamoDBAttribute(attributeName = “employeeID”)
    public String EmployeeID;

    @DynamoDBAttribute(attributeName = “EmployeeLevelDataRecords”)
    @DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.M)
    public EmployeeLevelTrail employeeLevelTrail

}


public class EmployeeLevelTrail {

    public final static String DDB_ATTR_EMPLOYEE_LEVEL_TRAIL = “employeeLevelTrail”;

    @DynamoDBAttribute(attributeName = DDB_ATTR_EMPLOYEE_LEVEL_TRAIL)
    private List<EmployeeLevelRecord> thisEmployeeLevelRecords;

    public void appendEmployeeLevelRecord(@NonNull EmployeeLevelRecord employeeLevelRecord) {

        thisEmployeeLevelRecords.add(employeeLevelRecord);

    }
}


public class EmployeeLevelRecord {

    private String Level;

    private String Manager;

    private Instant timeOfEvent;

}

这是我的注释,但不正确,因为我无法保存DynamoDB数据

3 个答案:

答案 0 :(得分:1)

要将另一个类标记为DynamoDBMapper数据模型的一部分,您可以使用@DynamoDBDocument对其进行注释,它告诉DynamoDBMapper一个类可以序列化为DynamoDB文档。

对于您不编写的类(例如,从Java库或外部库),或者如果您需要更多有关如何序列化自己的类的控制,则可以使用@DynamoDBTypeConverted,它允许通过提供自己的map arbitrary data实现将您从DynamoDBTypeConverter转换为从任何Java对象到任何受支持的DynamoDB类型。

使用示例代码,我添加了适当的@DynamoDBDocument@DynamoDBTypeConverted批注,以及将DynamoDBTypeConverter转换为Instant的示例String的示例实现。 ISO-8601 employeeId。如果@DynamoDBHashKey是表的哈希键,请确保还将employeeId批注添加到@DynamoDBTable(tableName = TABLE_NAME) public class EmployeeData { public final static String TABLE_NAME = “EmployeeDataRecord”; @DynamoDBAttribute(attributeName = “employeeID”) public String EmployeeID; @DynamoDBAttribute(attributeName = “EmployeeLevelDataRecords”) public EmployeeLevelTrail employeeLevelTrail } @DynamoDBDocument public class EmployeeLevelTrail { public final static String DDB_ATTR_EMPLOYEE_LEVEL_TRAIL = “employeeLevelTrail”; @DynamoDBAttribute(attributeName = DDB_ATTR_EMPLOYEE_LEVEL_TRAIL) private List<EmployeeLevelRecord> thisEmployeeLevelRecords; public void appendEmployeeLevelRecord(@NonNull EmployeeLevelRecord employeeLevelRecord) { thisEmployeeLevelRecords.add(employeeLevelRecord); } } @DynamoDBDocument public class EmployeeLevelRecord { private String Level; private String Manager; @DynamoDBTypeConverted(converter = InstantToStringTypeConverter.class) private Instant timeOfEvent; } public class InstantToStringTypeConverter implements DynamoDBTypeConverter<String, Instant> { @Override public String convert(final Instant instant) { return instant.toString(); } @Override public Instant unconvert(final String string) { return Instant.parse(string); } }

{{1}}

答案 1 :(得分:0)

您需要使用DynamoDBTypeConverted批注。您还需要创建一个转换器类,该转换器类定义如何从Java对象转换为DynamoDB原语(可能是字符串)。

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.ArbitraryDataMapping.html

您还可以从以下答案中查看示例:

DynamoDB JsonMarshaller cannot Deserialize List of Object

答案 2 :(得分:0)

我使用以下代码到达了解决方案。仅使用@DynamoDBTypeConvertedJSON,我就可以在表中创建一个条目,但由于遇到映射Dynamo DB映射异常而无法更新它。

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBTable(tableName = TABLE_NAME)
public class EmployeeData {

    public final static String TABLE_NAME = "EmployeeData";

    public final static String DDB_ATTR_ID = "Id";

    public final static String DDB_ATTR_EMPLOYEE_LEVEL_RECORD_TRAIL = "EmployeeLevelRecordTrail";

    @DynamoDBHashKey(attributeName = DDB_ATTR_ID)
    @DynamoDBAttribute(attributeName = DDB_ATTR_ID)
    private String id;

    @DynamoDBAttribute(attributeName = DDB_ATTR_EMPLOYEE_LEVEL_RECORD_TRAIL)
    @DynamoDBTypeConverted(converter = EmployeeLevelRecordTrailConverter.class)
    private  EmployeeLevelRecordTrail  employeeLevelRecordTrail;

    @DynamoDBAttribute(attributeName = DDB_ATTR_CREATED_TIME)
    @DynamoDBTypeConverted(converter = InstantConverter.class)
    private Instant joiningTime;
}



@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeLevelRecordTrail {

    private List<EmployeeLevelRecord> thisEmployeeLevelRecords;

    public void appendEmployeeLevelRecord(@NonNull EmployeeLevelRecord employeeLevelRecord) {

        thisEmployeeLevelRecords.add(employeeLevelRecord);

    }
}


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBDocument
public class EmployeeLevelRecord {

    private String Level;

    private String Manager;

    @DynamoDBTypeConverted(converter = InstantConverter.class)
    private Instant timeOfEvent;

}

public class EmployeeLevelRecordTrailConverter implements
        DynamoDBTypeConverter<List<EmployeeLevelRecord>, EmployeeLevelRecordTrail> {

    @Override
    public List<EmployeeLevelRecord> convert(EmployeeLevelRecordTrail employeeLevelRecordTrail) {
        return employeeLevelRecordTrail.getEmployeeLevelRecord();
    }

    @Override
    public EmployeeLevelRecordTrail unconvert(List<EmployeeLevelRecord> thisEmployeeLevelRecords) {
        return new EmployeeLevelRecordTrail(thisEmployeeLevelRecords);
    }
}

public class InstantConverter implements DynamoDBTypeConverter<String, Instant> {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_INSTANT;

    @Override
    public String convert(Instant instant) {
        return instant == null ? null : DATE_TIME_FORMATTER.format(instant);
    }

    @Override
    public Instant unconvert(String str) {
        return str == null ? null : Instant.from(DATE_TIME_FORMATTER.parse(str));
    }
}

阅读有关如何进行映射的文档很有帮助。