Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save datetime to DynamoDB?

I have the next code:

users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection) users_table.put_item(data={   "login": login,   "password": hashlib.sha256(password.encode("utf-8")).hexdigest(),   "profile": profile,   "registration_date": datetime.now() # PROBLEM IS HERE }) 

But when I run it, it fails with error:

TypeError: Unsupported type "< type 'datetime.datetime' >" for value "2015-01-12 05:02:57.053131"

I've tried a lot of ways, but it seems that it isn't possible to save datetime to DynamoDB. Btw it works fine in MongoDB.

Is there any solution?

like image 665
Alexander Perechnev Avatar asked Jan 12 '15 02:01

Alexander Perechnev


People also ask

Can DynamoDB store dates?

The Date values are stored as ISO-8601 formatted strings. SS (string set) type, NS (number set) type, or BS (binary set) type. The DynamoDBTypeConverter interface lets you map your own arbitrary data types to a data type that is natively supported by DynamoDB.

Is DynamoDB good for time series data?

This takes out the guesswork of provisioning read and write units and you simply pay for what you use. 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.

What is timestamp in DynamoDB?

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.

Can DynamoDB store relational data?

A DynamoDB table design corresponds to the relational order entry schema that is shown in Relational modeling. It follows the Adjacency list design pattern, which is a common way to represent relational data structures in DynamoDB.


2 Answers

Okay, I see that DynamoDB does not support any date types. So the only solution is to use unix-like time as integer, or save date as string.

enter image description here

like image 71
Alexander Perechnev Avatar answered Oct 18 '22 11:10

Alexander Perechnev


According to alejandro-franco response .isoformat() make the trick.

Just tested and this a working example:

CustomerPreferenceTable.put_item(     Item={         "id": str(uuid4()),         "validAfter": datetime.utcnow().isoformat(),         "validBefore": (datetime.utcnow() + timedelta(days=365)).isoformat(),         "tags": ["potato", "eggplant"]     } ) 
like image 33
user1855042 Avatar answered Oct 18 '22 12:10

user1855042