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
There are multiple ways to represent a timestamp in DynamoDB. Probably the most common is to use a Number type to represent the timestamp with the value as a Unix timestamp (seconds or milliseconds). Additionally, you can store the timestamp as a String type with the value as an ISO 8601 formatted string.
All that to say, DynamoDb can work well for time series data, and can be much faster at querying the data, but you need to carefully consider how you need to access it.
A string representation of a timestamp is a character or a Unicode graphic string that starts with a digit and has a length of at least 14 characters.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With