具有另一个对象列表的Object的DynamoDB Mapper注释

问题描述 投票:1回答:3

我正在尝试为以下情况创建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数据

amazon-web-services annotations amazon-dynamodb aws-sdk dynamodb-queries
3个回答
1
投票

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

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

使用您的示例代码,我添加了适当的@DynamoDBTypeConvertedDynamoDBTypeConverter注释,以及将Instant转换为ISO-8601 StringemployeeId的示例实现。如果@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); } }

DynamoDBTypeConverted

0
投票

您需要使用https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.ArbitraryDataMapping.html注释。您还需要创建一个转换器类,它定义如何从Java对象到DynamoDB原语(可能是一个字符串)。

DynamoDB JsonMarshaller cannot Deserialize List of Object

您还可以查看此答案中的示例:

@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)); } }


0
投票

我使用下面的代码找到了解决方案。只有@DynamoDBTypeConvertedJSON,我能够在表中创建一个条目,但不会更新它,因为我正在运行映射Dynamo DB映射异常。

qazxswpoi

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

© www.soinside.com 2019 - 2024. All rights reserved.