T - The object type which this mapper operates.H - The hash key value type.R - The range key value type; use ? if no range key.public final class DynamoDBTableMapper<T,H,R> extends Object
DynamoDBMapper which operates only on a specified
class/table. All calls are forwarded to the underlying
DynamoDBMapper which was used to create this table mapper.
A minimal example using get annotations,
@DynamoDBTable(tableName="TestTable")
public class TestClass {
private Long key;
private String rangeKey;
private Double amount;
private Long version;
@DynamoDBHashKey
public Long getKey() { return key; }
public void setKey(Long key) { this.key = key; }
@DynamoDBRangeKey
public String getRangeKey() { return rangeKey; }
public void setRangeKey(String rangeKey) { this.rangeKey = rangeKey; }
@DynamoDBAttribute(attributeName="amount")
public Double getAmount() { return amount; }
public void setAmount(Double amount) { this.amount = amount; }
@DynamoDBVersionAttribute
public Long getVersion() { return version; }
public void setVersion(Long version) { this.version = version; }
}
Initialize the DynamoDB mapper,
AmazonDynamoDB dbClient = new AmazonDynamoDBClient(); DynamoDBMapper dbMapper = new DynamoDBMapper(dbClient);Then, create a new table mapper with hash and range key,
DynamoDBTableMapper<TestClass,Long,String> mapper = dbMapper.newTableMapper(TestClass.class);Or, if the table does not have a range key,
DynamoDBTableMapper<TestClass,Long,?> mapper = dbMapper.newTableMapper(TestClass.class);If you don't have your DynamoDB table set up yet, you can use,
mapper.createTableIfNotExists(new ProvisionedThroughput(25L, 25L));Save instances of annotated classes and retrieve them,
TestClass object = new TestClass();
object.setKey(1234L);
object.setRangeKey("ABCD");
object.setAmount(101D);
try {
mapper.saveIfNotExists(object);
} catch (ConditionalCheckFailedException e) {
// handle already existing
}
Execute a query operation,
int limit = 10;
List<TestClass> objects = new ArrayList<TestClass>(limit);
DynamoDBQueryExpression<TestClass> query = mapper.queryExpressionOf(1234L)
.withRangeKeyCondition(mapper..rangeKey().name(), mapper.rangeKey().ge("ABAA"))
.withQueryFilterEntry("amount", mapper.field("amount").gt(100D))
.withConsistentReads(true);
QueryResultPage<TestClass> results = new QueryResultPage<TestClass>();
do {
if (results.getLastEvaluatedKey() != null) {
query.setExclusiveStartKey(results.getLastEvaluatedKey());
}
query.setLimit(limit - objects.size());
results = mapper.query(query);
for (TestClass object : results.getResults()) {
objects.add(object);
}
} while (results.getLastEvaluatedKey() != null && objects.size() < limit)
DynamoDBMapper,
AmazonDynamoDB| Modifier and Type | Method and Description |
|---|---|
List<DynamoDBMapper.FailedBatch> |
batchDelete(Iterable<T> objectsToDelete)
Deletes the objects given using one or more calls to the batchWtiteItem API.
|
List<T> |
batchLoad(Iterable<T> itemsToGet)
Retrieves multiple items from the table using their primary keys.
|
List<DynamoDBMapper.FailedBatch> |
batchSave(Iterable<T> objectsToSave)
Saves the objects given using one or more calls to the batchWriteItem API.
|
List<DynamoDBMapper.FailedBatch> |
batchWrite(Iterable<T> objectsToWrite,
Iterable<T> objectsToDelete)
Saves and deletes the objects given using one or more calls to the
batchWriteItem API.
|
int |
count(DynamoDBQueryExpression<T> queryExpression)
Evaluates the specified query expression and returns the count of matching
items, without returning any of the actual item data
|
int |
count(DynamoDBScanExpression scanExpression)
Evaluates the specified scan expression and returns the count of matching
items, without returning any of the actual item data.
|
S3Link |
createS3Link(Region s3region,
String bucketName,
String key)
Creates an S3Link with the specified region, bucket name and key.
|
S3Link |
createS3Link(String bucketName,
String key)
Creates an S3Link with the specified bucket name and key using the
default S3 region.
|
boolean |
createTableIfNotExists(ProvisionedThroughput throughput)
Creates the table and ignores the
ResourceInUseException if it
ialready exists. |
void |
delete(T object)
Deletes the given object from its DynamoDB table.
|
void |
delete(T object,
DynamoDBDeleteExpression deleteExpression)
Deletes the given object from its DynamoDB table using the specified
deleteExpression.
|
void |
deleteIfExists(T object)
Deletes the given object from its DynamoDB table with the condition that
the hash and, if applicable, the range key, already exist.
|
boolean |
deleteTableIfExists()
Deletes the table and ignores the
ResourceNotFoundException if
it does not already exist. |
TableDescription |
describeTable()
Returns information about the table, including the current status of the
table, when it was created, the primary key schema, and any indexes on
the table.
|
<V> DynamoDBMapperFieldModel<T,V> |
field(String attributeName)
Gets the field model for a given attribute.
|
S3ClientCache |
getS3ClientCache()
Returns the underlying S3ClientCache for accessing S3.
|
<H> DynamoDBMapperFieldModel<T,H> |
hashKey()
Gets the hash key field model for the specified type.
|
T |
load(H hashKey)
Loads an object with the hash key given.
|
T |
load(H hashKey,
R rangeKey)
Loads an object with the hash and range key.
|
PaginatedParallelScanList<T> |
parallelScan(DynamoDBScanExpression scanExpression,
int totalSegments)
Scans through an Amazon DynamoDB table on logically partitioned segments
in parallel and returns the matching results in one unmodifiable list of
instantiated objects.
|
QueryResultPage<T> |
query(DynamoDBQueryExpression<T> queryExpression)
Queries an Amazon DynamoDB table and returns the matching results as an
unmodifiable list of instantiated objects.
|
QueryResultPage<T> |
queryPage(DynamoDBQueryExpression<T> queryExpression)
Queries an Amazon DynamoDB table and returns a single page of matching
results.
|
<R> DynamoDBMapperFieldModel<T,R> |
rangeKey()
Gets the range key field model for the specified type.
|
void |
save(T object)
Saves the object given into DynamoDB.
|
void |
save(T object,
DynamoDBSaveExpression saveExpression)
Saves the object given into DynamoDB using the specified saveExpression.
|
void |
saveIfExists(T object)
Saves the object given into DynamoDB with the condition that the hash
and, if applicable, the range key, already exist.
|
void |
saveIfNotExists(T object)
Saves the object given into DynamoDB with the condition that the hash
and if applicable, the range key, does not already exist.
|
PaginatedScanList<T> |
scan(DynamoDBScanExpression scanExpression)
Scans through an Amazon DynamoDB table and returns the matching results
as an unmodifiable list of instantiated objects.
|
ScanResultPage<T> |
scanPage(DynamoDBScanExpression scanExpression)
Scans through an Amazon DynamoDB table and returns a single page of
matching results.
|
public <V> DynamoDBMapperFieldModel<T,V> field(String attributeName)
V - The field model's value type.attributeName - The attribute name.public <H> DynamoDBMapperFieldModel<T,H> hashKey()
H - The hash key type.DynamoDBMappingException - If the hash key is not present.public <R> DynamoDBMapperFieldModel<T,R> rangeKey()
R - The range key type.DynamoDBMappingException - If the range key is not present.public final List<T> batchLoad(Iterable<T> itemsToGet)
itemsToGet - The items to get.DynamoDBMapper.batchLoad(java.lang.Iterable<? extends java.lang.Object>)public final List<DynamoDBMapper.FailedBatch> batchSave(Iterable<T> objectsToSave)
objectsToSave - The objects to save.DynamoDBMapper.batchSave(java.lang.Iterable<? extends java.lang.Object>)public final List<DynamoDBMapper.FailedBatch> batchDelete(Iterable<T> objectsToDelete)
objectsToDelete - The objects to delete.DynamoDBMapper.batchDelete(java.lang.Iterable<? extends java.lang.Object>)public final List<DynamoDBMapper.FailedBatch> batchWrite(Iterable<T> objectsToWrite, Iterable<T> objectsToDelete)
objectsToWrite - The objects to write.objectsToDelete - The objects to delete.DynamoDBMapper.batchWrite(java.lang.Iterable<? extends java.lang.Object>, java.lang.Iterable<? extends java.lang.Object>)public final T load(H hashKey)
hashKey - The hash key value.DynamoDBMapper.load(java.lang.Class<T>, java.lang.Object, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)public final T load(H hashKey, R rangeKey)
hashKey - The hash key value.rangeKey - The range key value.DynamoDBMapper.load(java.lang.Class<T>, java.lang.Object, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)public final void save(T object)
object - The object to save.DynamoDBMapper.save(T)public final void save(T object, DynamoDBSaveExpression saveExpression)
object - The object to save.saveExpression - The save expression.DynamoDBMapper.save(T)public final void saveIfNotExists(T object) throws ConditionalCheckFailedException
object - The object to create.ConditionalCheckFailedException - If the object exists.DynamoDBMapper.save(T),
DynamoDBSaveExpression,
ExpectedAttributeValuepublic final void saveIfExists(T object) throws ConditionalCheckFailedException
object - The object to update.ConditionalCheckFailedException - If the object does not exist.DynamoDBMapper.save(T),
DynamoDBSaveExpression,
ExpectedAttributeValuepublic final void delete(T object)
object - The object to delete.DynamoDBMapper.delete(java.lang.Object)public final void delete(T object, DynamoDBDeleteExpression deleteExpression)
object - The object to delete.deleteExpression - The delete expression.DynamoDBMapper.delete(java.lang.Object)public final void deleteIfExists(T object) throws ConditionalCheckFailedException
object - The object to delete.ConditionalCheckFailedException - If the object does not exist.DynamoDBMapper.delete(java.lang.Object),
DynamoDBDeleteExpression,
ExpectedAttributeValuepublic final int count(DynamoDBQueryExpression<T> queryExpression)
queryExpression - The query expression.DynamoDBMapper.count(java.lang.Class<?>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)public final QueryResultPage<T> query(DynamoDBQueryExpression<T> queryExpression)
queryExpression - The query expression.DynamoDBMapper.query(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression<T>)public final QueryResultPage<T> queryPage(DynamoDBQueryExpression<T> queryExpression)
queryExpression - The query expression.DynamoDBMapper.query(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression<T>)public final int count(DynamoDBScanExpression scanExpression)
scanExpression - The scan expression.DynamoDBMapper.count(java.lang.Class<?>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)public final PaginatedScanList<T> scan(DynamoDBScanExpression scanExpression)
scanExpression - The scan expression.DynamoDBMapper.scan(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)public final ScanResultPage<T> scanPage(DynamoDBScanExpression scanExpression)
scanExpression - The scan expression.DynamoDBMapper.scanPage(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)public final PaginatedParallelScanList<T> parallelScan(DynamoDBScanExpression scanExpression, int totalSegments)
scanExpression - The scan expression.totalSegments - The total segments.DynamoDBMapper.parallelScan(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression, int)public final S3Link createS3Link(Region s3region, String bucketName, String key)
s3region - The S3 region.bucketName - The bucket name.key - The key.DynamoDBMapper.createS3Link(java.lang.String, java.lang.String)public final S3Link createS3Link(String bucketName, String key)
bucketName - The bucket name.key - The key.DynamoDBMapper.createS3Link(java.lang.String, java.lang.String)public final S3ClientCache getS3ClientCache()
DynamoDBMapper.getS3ClientCache()public final TableDescription describeTable()
AmazonDynamoDB.describeTable(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)public final boolean createTableIfNotExists(ProvisionedThroughput throughput)
ResourceInUseException if it
ialready exists.throughput - The provisioned throughput.AmazonDynamoDB.createTable(com.amazonaws.services.dynamodbv2.model.CreateTableRequest),
CreateTableRequestpublic final boolean deleteTableIfExists()
ResourceNotFoundException if
it does not already exist.AmazonDynamoDB.deleteTable(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest),
DeleteTableRequestCopyright © 2013 Amazon Web Services, Inc. All Rights Reserved.