AWS Developer Tools Blog
Storing JSON documents in Amazon DynamoDB tables
DynamoDBMapper is a high-level abstraction layer in the AWS SDK for Java that allows you to transform java objects into items in Amazon DynamoDB tables and vice versa. All you need to do is annotate your java class in a few places, and the mapper takes care of getting the objects in and out of the database.
DynamoDBMapper has a new feature that allows you to save an object as a JSON document in a DynamoDB attribute. To do this, simply annotate the class with @DynamoDBDocument,
and the mapper does the heavy work of converting the object into a JSON document and storing it in DynamoDB. DynamoDBMapper also takes care of loading the java object from the JSON document when requested by the user.
Let’s say your application maintains the inventory of a car dealership in Amazon DynamoDB and uses DynamoDBMapper to save and retrieve data. One of the tables is Car
, which holds information about a car and has name
as its primary key. Here is how the java class looks for the table:
As you can see, the class Spec
is modeled with a @DynamoDBDocument
annotation. DynamoDBMapper converts an instance of Spec
into a JSON document before storing it in DynamoDB. When stored in DynamoDB, an instance of the class Car
will look like this:
{ "name" : "IS 350", "year" : "2015", "make" : "Lexus", "colors" : ["black","white","grey"], "spec" : { "engine" : "V6", "wheelbase" : "110.2 in", "length" : "183.7 in", "width" : "71.3 in", "height" : "56.3 in" } }
You can also apply other DyanmoDBMapper annotations like @DyanmoDBIgnore and
@DynamoDBAttribute
to the JSON document. For instance, model the height
attribute of the Spec
class with @DynamoDBIgnore
.
The updated item in DynamoDB will look like this:
{ "name" : "IS 350", "year" : "2015", "make" : "Lexus", "colors" : ["black","white","grey"], "spec" : { "engine" : "V6", "wheelbase" : "110.2 in", "length" : "183.7 in", "width" : "71.3 in" } }
To learn more, check out our other blog posts and the developer guide
- Specifying Conditional Constraints with Amazon DynamoDB Mapper.
- Client-side Encryption for Amazon DynamoDB.
- Using the SaveBehavior Configuration for the DynamoDBMapper.
- DynamoDBMapper developer guide.
Do you want to see new features in DynamoDBMapper? Let us know what you think!