DynamoDB remove an existing attribute and add new attribute

929 views Asked by At

We have an existing DynamoDB table having a non key attribute "latest". We wish to remove this attribute for the newer items from now with another named "latestV2". Will just replacing the attribute from Model class with the newer one work or do we need to keep both of them and not setting the old one anymore. Current Model Class :

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "Table1")
public class Table1DAO {
    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBAttribute(attributeName = "latest")
    private Boolean latest;
}

Will this work :

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "Table1")
public class Table1DAO {
    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBAttribute(attributeName = "latestV2")
    private Boolean latest;
}

The underlying setters/getters setlatest()/getlatest() should work for the new attributeName as well. Question is that while loading any old item with the old "latest" flag as true, will it break the code since that attribute is no longer modeled ?

Or should we have both the attributes in the Model class and just change the name of variable for the old attribute so that it is never set/get again like this :

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "Table1")
public class Table1DAO {
    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBAttribute(attributeName = "latest")
    private Boolean latestold;

    @DynamoDBAttribute(attributeName = "latestV2")
    private Boolean latest;
}
1

There are 1 answers

2
CruncherBigData On BEST ANSWER

Here is a good example of the table mapper. The @DynamoDBAttribute maps the field name from DynamoDB to the java class and therefore will ignore the "latest" attribute and will use "latestV2". So to answer your question you do not need to keep both attributes and modifying the old attribute does not impact your code as long as it is not mapped to any java class attribute.