AWS Developer Blog

Storing Java objects in Amazon DynamoDB tables

by zachmu | on | in Java | Permalink | Comments |  Share

The AWS SDK for Java makes it easy to store objects in Amazon DynamoDB and get them back out again, all without having to write the code to transform your objects into table items and vice versa. All you need to do is annotate your domain classes in a few places and the SDK will handle the work of getting objects into and out of the database. For example, here’s a minimal User class we want to store in DynamoDB:

@DynamoDBTable(tableName = "users")
public class User {
    private Integer id;
    private Set<String> friends;
    private String status;
    public Integer getId() { return id; }
    public void setId(Integer id) { = id; }
    public Set<String> getFriends() { return friends; }
    public void setFriends(Set<String> friends) { this.friends = friends; }
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }

The DynamoDBMapper utility class makes it simple to store instances of this class in DynamoDB.

AmazonDynamoDB dynamo = new AmazonDynamoDBClient(awsCredentials);
DynamoDBMapper mapper = new DynamoDBMapper(dynamo);
// save a new item;
// update the item
// delete the item

DynamoDBMapper can also handle query and scan operations with automatic result pagination, optimistic locking, automatic hash key generation, custom marshaling logic, and much more. For more detail, see this article.