Class DynamoDBMapper

java.lang.Object
com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper
All Implemented Interfaces:
IDynamoDBMapper

public class DynamoDBMapper extends AbstractDynamoDBMapper
Object mapper for domain-object interaction with DynamoDB.

To use, define a domain class that represents an item in a DynamoDB table and annotate it with the annotations found in the com.amazonaws.services.dynamodbv2.datamodeling package. In order to allow the mapper to correctly persist the data, each modeled property in the domain class should be accessible via getter and setter methods, and each property annotation should be either applied to the getter method or the class field. A minimal example using getter annotations:

 @DynamoDBTable(tableName = "TestTable")
 public class TestClass {

     private Long key;
     private double rangeKey;
     private Long version;

     private Set<Integer> integerSetAttribute;

     @DynamoDBHashKey
     public Long getKey() {
         return key;
     }

     public void setKey(Long key) {
         this.key = key;
     }

     @DynamoDBRangeKey
     public double getRangeKey() {
         return rangeKey;
     }

     public void setRangeKey(double rangeKey) {
         this.rangeKey = rangeKey;
     }

     @DynamoDBAttribute(attributeName = "integerSetAttribute")
     public Set<Integer> getIntegerAttribute() {
         return integerSetAttribute;
     }

     public void setIntegerAttribute(Set<Integer> integerAttribute) {
         this.integerSetAttribute = integerAttribute;
     }

     @DynamoDBVersionAttribute
     public Long getVersion() {
         return version;
     }

     public void setVersion(Long version) {
         this.version = version;
     }
 }
 

Save instances of annotated classes to DynamoDB, retrieve them, and delete them using the DynamoDBMapper class, as in the following example.

 DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
 Long hashKey = 105L;
 double rangeKey = 1.0d;
 TestClass obj = mapper.load(TestClass.class, hashKey, rangeKey);
 obj.getIntegerAttribute().add(42);
 mapper.save(obj);
 mapper.delete(obj);
 

If you don't have your DynamoDB table set up yet, you can use generateCreateTableRequest(Class) to construct the CreateTableRequest for the table represented by your annotated class.

 AmazonDynamoDB dynamoDBClient = new AmazonDynamoDBClient();
 DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
 CreateTableRequest req = mapper.generateCreateTableRequest(TestClass.class);
 // Table provision throughput is still required since it cannot be specified in your POJO
 req.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
 // Fire off the CreateTableRequest using the low-level client
 dynamoDBClient.createTable(req);
 

When using the save, load, and delete methods, DynamoDBMapper will throw DynamoDBMappingExceptions to indicate that domain classes are incorrectly annotated or otherwise incompatible with this class. Service exceptions will always be propagated as AmazonClientException, and DynamoDB-specific subclasses such as ConditionalCheckFailedException will be used when possible.

This class is thread-safe and can be shared between threads. It's also very lightweight, so it doesn't need to be.

See Also: