I am using DynamoDB for storage. I need to store a Java object in one attribute within a table. I see two approaches:
What are the pros and cons of each (In terms of item size, flexibility in Dynamo DB queries)?
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.
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.
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.
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