Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 383
JavaBuddy Avatar asked Feb 07 '17 19:02

JavaBuddy


People also ask

Does DynamoDB support timestamp?

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.

Is DynamoDB good for time series data?

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.

Is timestamp a number or string?

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.


2 Answers

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

like image 135
notionquest Avatar answered Sep 19 '22 14:09

notionquest


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.

like image 37
gkatzioura Avatar answered Sep 20 '22 14:09

gkatzioura