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?
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.
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.
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.
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.
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.
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"] } )
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