Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON as string in DynamoDB vs List/Map types

Tags:

I am using DynamoDB for storage. I need to store a Java object in one attribute within a table. I see two approaches:

  1. One to convert the object to JSON using Jackson on the client side, and then store the JSON string in the attribute.
  2. Another way is to use DynamoDB List/Map types to store my object.

What are the pros and cons of each (In terms of item size, flexibility in Dynamo DB queries)?

like image 623
enitihas Avatar asked Aug 01 '17 05:08

enitihas


People also ask

What is the data type for JSON in DynamoDB?

JSON has no sets, just arrays, so DynamoDB sets (SS, NS, and BS types) will be converted to JSON arrays. JSON has no binary representation, so DynamoDB binary scalars and sets (B and BS types) will be converted to base64-encoded JSON strings or lists of strings.

Can you store JSON in DynamoDB?

PutItem and JSON documentsYou can store a JSON document as an attribute in a DynamoDB table. To do this, use the withJSON method of Item . This method parses the JSON document and maps each element to a native DynamoDB data type.


1 Answers

First approach

The benefit here is that you can store arbitrary data and should not care if DynamoDB supports it. You don't even need to care if this is a valid JSON. If you are storing DynamoDB List/Maps all attributes should be of types that DynamoDB supports.

You can push this approach even further and use compression and your item will occupy less space and save you some RCUs/WCUs in the process.

The first drawback is that it is harder to work with code like this. Simply because you need to convert data back and forth and this will make your code and operations more complicated.

The second drawback, DynamoDB won't know anything about your data and won't be able to access it. For example you will not be able to use document paths feature.

Second approach

I think this is a preferred approach unless you have a good reason to stick to the first one (e.g. unusual data or size constraints).

The benefit here is that it is easier to work with, you can access all DynamoDB features and if you are using DynamoDBMapper it is really easy to implement.

like image 86
Ivan Mushketyk Avatar answered Dec 31 '22 05:12

Ivan Mushketyk