'Auto populate timestamp in DynamoDB

I come from Relational Database background and we have a way to populate timestamp for row creation and update. I am having difficulty finding similar feature for DynamoDB.

I checked DynamoDB to check if they support autopopulate the date timestamp for every entry in dynamoDB. I see it is possible to create random ID but that is not what I need.

My usecase is to add a timestamp entry automatically when I add any entry to DynamoDB Table. Appreciate any pointers. Thanks



Solution 1:[1]

Java Solution using Annotation (Data modelling package)

You can use the DynamoDBAutoGeneratedTimestamp annotation.

@DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.CREATE)
public Date getCreatedDate() { return createdDate; }
public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; }

@DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.ALWAYS)
public Date getLastUpdatedDate() { return lastUpdatedDate; }
public void setLastUpdatedDate(Date lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; }

DynamoDBAutoGeneratedTimestamp

Solution 2:[2]

There is no such functionality like DEFAULT CURRENT TIMESTAMP or UPDATE CURRENT_TIMESTAMP. You will have to set the date by yourself.

However if you want to keep track of changes on updates then you can use something like atomic counters.

Thus on every update your will increment the counter value.

Solution 3:[3]

What worked for me, using aws-sdk-version 2.17.169:

First, you need to define the extension to support

AutoGeneratedTimestampRecordExtension.

    @Bean
    public DynamoDbEnhancedClient dynamoDbEnhancedClient(){
        return DynamoDbEnhancedClient.builder()
                .dynamoDbClient(dynamoDbClient())
                .extensions(AutoGeneratedTimestampRecordExtension.create())
                .build();
    }

Then, create a converter for java.time.Instant. As this is the only one supported at the time.

import java.time.Instant;
public class InstantToStringTypeConverter implements DynamoDBTypeConverter<String, Instant> {

    @Override
    public String convert(Instant instant) {
        return instant.toString();
    }

    @Override
    public Instant unconvert(String s) {
        return Instant.parse(s);
    }

Finally, on your model, add the annotations:

    @DynamoDbAutoGeneratedTimestampAttribute
    @DynamoDBTypeConverted(converter = InstantToStringTypeConverter.class)
    public Instant getCreated() {
        return created;
    }

    public void setCreated(Instant created) {
        this.created = created;
    }

    @DynamoDbAutoGeneratedTimestampAttribute
    @DynamoDBTypeConverted(converter = InstantToStringTypeConverter.class)
    public Instant getUpdated() {
        return updated;
    }

    public void setUpdated(Instant updated) {
        this.updated = updated;
    } 

See some reference here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 notionquest
Solution 2 gkatzioura
Solution 3