Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to store an Object in Amazon Dynamo as Json?

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?

like image 339
tinytelly Avatar asked Jul 12 '12 00:07

tinytelly


People also ask

Is DynamoDB good for JSON?

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.

Can you store objects in DynamoDB?

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.

What is DynamoDB not good for?

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.

Can DynamoDB store nested JSON?

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.


1 Answers

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.

like image 58
aaaristo Avatar answered Oct 05 '22 18:10

aaaristo