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,?> table = dbMapper.newTableMapper(TestClass.class);If you don't have your DynamoDB table set up yet, you can use,
table.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 { table.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 = new DynamoDBQueryExpression() .withRangeKeyCondition(table.rangeKey().name(), table.rangeKey().ge("ABAA")) .withQueryFilterEntry("amount", table.field("amount").gt(100D)) .withHashKeyValues(1234L) .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.
|
TableDescription |
createTable(ProvisionedThroughput throughput)
Creates the table with the specified throughput; also populates the same
throughput for all global secondary indexes.
|
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.
|
TableDescription |
deleteTable()
Deletes the table.
|
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.
|
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.
|
PaginatedQueryList<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.
|
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 DynamoDBMapperFieldModel<T,H> hashKey()
H
- The hash key type.DynamoDBMappingException
- If the hash key is not present.public DynamoDBMapperFieldModel<T,R> rangeKey()
R
- The range key type.DynamoDBMappingException
- If the range key is not present.public List<T> batchLoad(Iterable<T> itemsToGet)
itemsToGet
- The items to get.DynamoDBMapper.batchLoad(java.lang.Iterable<? extends java.lang.Object>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public List<DynamoDBMapper.FailedBatch> batchSave(Iterable<T> objectsToSave)
objectsToSave
- The objects to save.AbstractDynamoDBMapper.batchSave(java.lang.Iterable<? extends java.lang.Object>)
public List<DynamoDBMapper.FailedBatch> batchDelete(Iterable<T> objectsToDelete)
objectsToDelete
- The objects to delete.AbstractDynamoDBMapper.batchDelete(java.lang.Iterable<? extends java.lang.Object>)
public 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>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public T load(H hashKey)
hashKey
- The hash key value.DynamoDBMapper.load(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public T load(H hashKey, R rangeKey)
hashKey
- The hash key value.rangeKey
- The range key value.DynamoDBMapper.load(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public void save(T object)
object
- The object to save.DynamoDBMapper.save(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public void save(T object, DynamoDBSaveExpression saveExpression)
object
- The object to save.saveExpression
- The save expression.DynamoDBMapper.save(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public void saveIfNotExists(T object) throws ConditionalCheckFailedException
object
- The object to create.ConditionalCheckFailedException
- If the object exists.DynamoDBMapper.save(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
,
DynamoDBSaveExpression
,
ExpectedAttributeValue
public void saveIfExists(T object) throws ConditionalCheckFailedException
object
- The object to update.ConditionalCheckFailedException
- If the object does not exist.DynamoDBMapper.save(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
,
DynamoDBSaveExpression
,
ExpectedAttributeValue
public final void delete(T object)
object
- The object to delete.DynamoDBMapper.delete(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public final void delete(T object, DynamoDBDeleteExpression deleteExpression)
object
- The object to delete.deleteExpression
- The delete expression.DynamoDBMapper.delete(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public void deleteIfExists(T object) throws ConditionalCheckFailedException
object
- The object to delete.ConditionalCheckFailedException
- If the object does not exist.DynamoDBMapper.delete(T, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
,
DynamoDBDeleteExpression
,
ExpectedAttributeValue
public int count(DynamoDBQueryExpression<T> queryExpression)
queryExpression
- The query expression.DynamoDBMapper.count(java.lang.Class<?>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public PaginatedQueryList<T> query(DynamoDBQueryExpression<T> queryExpression)
queryExpression
- The query expression.DynamoDBMapper.query(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public QueryResultPage<T> queryPage(DynamoDBQueryExpression<T> queryExpression)
queryExpression
- The query expression.DynamoDBMapper.query(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public int count(DynamoDBScanExpression scanExpression)
scanExpression
- The scan expression.DynamoDBMapper.count(java.lang.Class<?>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public PaginatedScanList<T> scan(DynamoDBScanExpression scanExpression)
scanExpression
- The scan expression.DynamoDBMapper.scan(java.lang.Class<T>, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public 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 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, com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)
public TableDescription describeTable()
AmazonDynamoDB.describeTable(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)
public TableDescription createTable(ProvisionedThroughput throughput)
throughput
- The provisioned throughput.AmazonDynamoDB.createTable(com.amazonaws.services.dynamodbv2.model.CreateTableRequest)
,
CreateTableRequest
public boolean createTableIfNotExists(ProvisionedThroughput throughput)
ResourceInUseException
if it
ialready exists.throughput
- The provisioned throughput.AmazonDynamoDB.createTable(com.amazonaws.services.dynamodbv2.model.CreateTableRequest)
,
CreateTableRequest
public TableDescription deleteTable()
AmazonDynamoDB.deleteTable(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)
,
DeleteTableRequest
public boolean deleteTableIfExists()
ResourceNotFoundException
if
it does not already exist.AmazonDynamoDB.deleteTable(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)
,
DeleteTableRequest
Copyright © 2013 Amazon Web Services, Inc. All Rights Reserved.