After looking at Amazon DynamoDB I have been trying to figure out how to store my complex objects that I create in my code and want to persist. I understand how I would store these objects in a Relational Database like MySql but how would I store them in DynamoDB?
I can think of one way....turn them into Json and store the Json representation of the Object in DynamoDB. To get them out of DynamoDB again I would marshall them back from Json to my Object representation in my code.
Here is an example:
For this type of Object: A Car has an Engine Which has many Parts. Each Part has a Serial Number, a Life Span and a Replacement Value. Now I can turn the Car into json which would look something like this:
{
"engine": {
"parts": [
{
"serialNumber": "1234",
"lifeSpan": 10,
"replacementValue": 100
},
{
"serialNumber": "5678",
"lifeSpan": 1,
"replacementValue": 200
}
]
}
}
Should I just store the above Json in Dynamo as Key: CarName 'Jaguar', Value [above json] ? Or is there a better way to store the Car Object?
DynamoDB data types are a superset of JSON data types. This means that all JSON data can be represented as DynamoDB data, while the opposite isn't true.
You can store them as an object in Amazon S3 and then store the object identifier in your DynamoDB item. You can also use the object metadata support in Amazon S3 to provide a link back to the parent item in DynamoDB. Store the primary key value of the item as Amazon S3 metadata of the object in Amazon S3.
When not to use DynamoDB: When multi-item or cross table transactions are required. When complex queries and joins are required. When real-time analytics on historic data is required.
To get an AWS AppSync schema to handle nested JSON data in DynamoDB, do the following: Add a nested JSON data item to the DynamoDB table. Create an AWS AppSync API and attach the data source. Configure the nested JSON schema in the AWS AppSync API.
I understand how I would store these objects in a Relational Database like MySql but how would I store them in DynamoDB?
The mainstream solution for DynamoDB is pretty similar to the MySql one: you just, change a table row (mysql) with a table item (dynamodb), they both are flat structures, but a dynamodb item could have different attributes from other items in the table, and have a 64KB item size limit (column/attribute names included).
Should I just store the above Json in Dynamo as Key: CarName 'Jaguar', Value [above json] ? Or is there a better way to store the Car Object?
No, once your parts array grows you will face the 64KB item size limit. You can store a compressed version of the json to mitigate, but then you will not be able to use any attribute feature (eg. LSI or GSI, updateItem) in dynamodb, nor integrate seamlessly with other AWS products.
You can take a look at the way dyngodb stores those objects, shortly: one dynamodb item, for every object. One hash for arrays, with N ranges for the array elements.
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