IDynamoDbMapperpublic class DynamoDbMapper extends AbstractDynamoDbMapper
To use, define a domain class that represents an item in a DynamoDB table and annotate it with the annotations found in the software.amazon.awssdk.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
AbstractDynamoDbMapper.generateCreateTableRequest(Class) to construct the
CreateTableRequest for the table represented by your annotated class.
DynamoDBClient 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 SdkClientException, 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.
| Modifier and Type | Class | Description |
|---|---|---|
static class |
DynamoDbMapper.BatchGetItemException |
|
static class |
DynamoDbMapper.FailedBatch |
The return type of batchWrite, batchDelete and batchSave.
|
| Constructor | Description |
|---|---|
DynamoDbMapper(DynamoDBClient dynamoDb) |
Constructs a new mapper with the service object given, using the default
configuration.
|
DynamoDbMapper(DynamoDBClient ddb,
AwsCredentialsProvider s3CredentialProvider) |
Constructs a new mapper with the service object and S3 client cache
given, using the default configuration.
|
DynamoDbMapper(DynamoDBClient dynamoDb,
DynamoDbMapperConfig config) |
Constructs a new mapper with the service object and configuration given.
|
DynamoDbMapper(DynamoDBClient dynamoDb,
DynamoDbMapperConfig config,
AwsCredentialsProvider s3CredentialProvider) |
Constructs a new mapper with the service object, configuration, and S3
client cache given.
|
DynamoDbMapper(DynamoDBClient dynamoDb,
DynamoDbMapperConfig config,
AttributeTransformer transformer) |
Constructs a new mapper with the given service object, configuration,
and transform hook.
|
DynamoDbMapper(DynamoDBClient dynamoDb,
DynamoDbMapperConfig config,
AttributeTransformer transformer,
AwsCredentialsProvider s3CredentialsProvider) |
Constructor with all parameters.
|
| Modifier and Type | Method | Description |
|---|---|---|
Map<String,List<Object>> |
batchLoad(Iterable<? extends Object> itemsToGet,
DynamoDbMapperConfig config) |
Retrieves multiple items from multiple tables using their primary keys.
|
Map<String,List<Object>> |
batchLoad(Map<Class<?>,List<KeyPair>> itemsToGet,
DynamoDbMapperConfig config) |
Retrieves multiple items from multiple tables using their primary keys.
|
List<DynamoDbMapper.FailedBatch> |
batchWrite(Iterable<? extends Object> objectsToWrite,
Iterable<? extends Object> objectsToDelete,
DynamoDbMapperConfig config) |
Saves and deletes the objects given using one or more calls to the
DynamoDBClient.batchWriteItem(BatchWriteItemRequest) API. |
int |
count(Class<?> clazz,
DynamoDbScanExpression scanExpression,
DynamoDbMapperConfig config) |
Evaluates the specified scan expression and returns the count of matching items, without
returning any of the actual item data.
|
<T> int |
count(Class<T> clazz,
DynamoDbQueryExpression<T> queryExpression,
DynamoDbMapperConfig config) |
Evaluates the specified query expression and returns the count of matching items, without
returning any of the actual item data.
|
S3Link |
createS3Link(String s3region,
String bucketName,
String key) |
Creates an S3Link with the specified region, bucket name and key.
|
S3Link |
createS3Link(Region s3region,
String bucketName,
String key) |
Creates an S3Link with the specified region, bucket name and key.
|
<T> void |
delete(T object,
DynamoDbDeleteExpression deleteExpression,
DynamoDbMapperConfig config) |
Deletes the given object from its DynamoDB table using the provided deleteExpression and
provided configuration.
|
<T> CreateTableRequest |
generateCreateTableRequest(Class<T> clazz,
DynamoDbMapperConfig config) |
|
<T> DeleteTableRequest |
generateDeleteTableRequest(Class<T> clazz,
DynamoDbMapperConfig config) |
|
<T> DynamoDbMapperTableModel<T> |
getTableModel(Class<T> clazz,
DynamoDbMapperConfig config) |
Get the table model for the class using the provided configuration override.
|
<T> T |
load(Class<T> clazz,
Object hashKey,
Object rangeKey,
DynamoDbMapperConfig config) |
Returns an object with the given hash key, or null if no such object exists.
|
<T> T |
load(T keyObject,
DynamoDbMapperConfig config) |
Returns an object whose keys match those of the prototype key object given, or null if no
such item exists.
|
<T> T |
marshallIntoObject(Class<T> clazz,
Map<String,AttributeValue> itemAttributes,
DynamoDbMapperConfig config) |
|
<T> List<T> |
marshallIntoObjects(Class<T> clazz,
List<Map<String,AttributeValue>> itemAttributes,
DynamoDbMapperConfig config) |
|
<T,H,R> DynamoDbTableMapper<T,H,R> |
newTableMapper(Class<T> clazz) |
Creates a new table mapper using this mapper to perform operations.
|
<T> PaginatedParallelScanList<T> |
parallelScan(Class<T> clazz,
DynamoDbScanExpression scanExpression,
int totalSegments,
DynamoDbMapperConfig config) |
Scans through an Amazon DynamoDB table on logically partitioned segments in parallel.
|
<T> PaginatedQueryList<T> |
query(Class<T> clazz,
DynamoDbQueryExpression<T> queryExpression,
DynamoDbMapperConfig config) |
Queries an Amazon DynamoDB table and returns the matching results as an unmodifiable list of
instantiated objects.
|
<T> QueryResultPage<T> |
queryPage(Class<T> clazz,
DynamoDbQueryExpression<T> queryExpression,
DynamoDbMapperConfig config) |
Queries an Amazon DynamoDB table and returns a single page of matching results.
|
S3ClientCache |
s3ClientCache() |
Returns the underlying
S3ClientCache for accessing S3. |
<T> void |
save(T object,
DynamoDbSaveExpression saveExpression,
DynamoDbMapperConfig config) |
Saves an item in DynamoDB.
|
<T> PaginatedScanList<T> |
scan(Class<T> clazz,
DynamoDbScanExpression scanExpression,
DynamoDbMapperConfig config) |
Scans through an Amazon DynamoDB table and returns the matching results as an unmodifiable
list of instantiated objects.
|
<T> ScanResultPage<T> |
scanPage(Class<T> clazz,
DynamoDbScanExpression scanExpression,
DynamoDbMapperConfig config) |
Scans through an Amazon DynamoDB table and returns a single page of matching results.
|
batchDelete, batchDelete, batchLoad, batchLoad, batchSave, batchSave, batchWrite, count, count, createS3Link, delete, delete, delete, generateCreateTableRequest, generateDeleteTableRequest, getTableModel, load, load, load, load, marshallIntoObject, marshallIntoObjects, parallelScan, query, queryPage, save, save, save, scan, scanPagepublic DynamoDbMapper(DynamoDBClient dynamoDb)
dynamoDb - The service object to use for all service calls.DynamoDbMapperConfig.DEFAULTpublic DynamoDbMapper(DynamoDBClient dynamoDb, DynamoDbMapperConfig config)
dynamoDb - The service object to use for all service calls.config - The default configuration to use for all service calls. It can
be overridden on a per-operation basis.public DynamoDbMapper(DynamoDBClient ddb, AwsCredentialsProvider s3CredentialProvider)
ddb - The service object to use for all service calls.s3CredentialProvider - The credentials provider for accessing S3.
Relevant only if S3Link is involved.DynamoDbMapperConfig.DEFAULTpublic DynamoDbMapper(DynamoDBClient dynamoDb, DynamoDbMapperConfig config, AttributeTransformer transformer)
dynamoDb - the service object to use for all service callsconfig - the default configuration to use for all service calls. It
can be overridden on a per-operation basistransformer - The custom attribute transformer to invoke when serializing or
deserializing an object.public DynamoDbMapper(DynamoDBClient dynamoDb, DynamoDbMapperConfig config, AwsCredentialsProvider s3CredentialProvider)
dynamoDb - The service object to use for all service calls.config - The default configuration to use for all service calls. It can
be overridden on a per-operation basis.s3CredentialProvider - The credentials provider for accessing S3.
Relevant only if S3Link is involved.public DynamoDbMapper(DynamoDBClient dynamoDb, DynamoDbMapperConfig config, AttributeTransformer transformer, AwsCredentialsProvider s3CredentialsProvider)
dynamoDb - The service object to use for all service calls.config - The default configuration to use for all service calls. It can
be overridden on a per-operation basis.transformer - The custom attribute transformer to invoke when serializing or
deserializing an object.s3CredentialsProvider - The credentials provider for accessing S3.
Relevant only if S3Link is involved.public <T> DynamoDbMapperTableModel<T> getTableModel(Class<T> clazz, DynamoDbMapperConfig config)
IDynamoDbMappergetTableModel in interface IDynamoDbMappergetTableModel in class AbstractDynamoDbMapperpublic <T> T load(T keyObject,
DynamoDbMapperConfig config)
IDynamoDbMapperload in interface IDynamoDbMapperload in class AbstractDynamoDbMapperkeyObject - An object of the class to load with the keys values to match.config - Configuration for the service call to retrieve the object from DynamoDB. This
configuration overrides the default given at construction.public <T> T load(Class<T> clazz, Object hashKey, Object rangeKey, DynamoDbMapperConfig config)
IDynamoDbMapperload in interface IDynamoDbMapperload in class AbstractDynamoDbMapperclazz - The class to load, corresponding to a DynamoDB table.hashKey - The key of the object.rangeKey - The range key of the object, or null for tables without a range key.config - Configuration for the service call to retrieve the object from DynamoDB. This
configuration overrides the default given at construction.public <T> T marshallIntoObject(Class<T> clazz, Map<String,AttributeValue> itemAttributes, DynamoDbMapperConfig config)
marshallIntoObject in class AbstractDynamoDbMapperpublic <T> List<T> marshallIntoObjects(Class<T> clazz, List<Map<String,AttributeValue>> itemAttributes, DynamoDbMapperConfig config)
marshallIntoObjects in class AbstractDynamoDbMapperpublic <T> void save(T object,
DynamoDbSaveExpression saveExpression,
DynamoDbMapperConfig config)
IDynamoDbMapperDynamoDbMapperConfig.saveBehavior() value, to use either
DynamoDBClient.putItem(PutItemRequest) or
DynamoDBClient.updateItem(UpdateItemRequest):
save in interface IDynamoDbMappersave in class AbstractDynamoDbMapperobject - The object to save into DynamoDBsaveExpression - The options to apply to this save requestconfig - The configuration to use, which overrides the default provided at object
construction.DynamoDbMapperConfig.SaveBehaviorpublic <T> void delete(T object,
DynamoDbDeleteExpression deleteExpression,
DynamoDbMapperConfig config)
IDynamoDbMapperdelete in interface IDynamoDbMapperdelete in class AbstractDynamoDbMapperdeleteExpression - The options to apply to this delete requestconfig - Config override object. If DynamoDbMapperConfig.SaveBehavior.CLOBBER is supplied, version
fields will not be considered when deleting the object.public List<DynamoDbMapper.FailedBatch> batchWrite(Iterable<? extends Object> objectsToWrite, Iterable<? extends Object> objectsToDelete, DynamoDbMapperConfig config)
IDynamoDbMapperDynamoDBClient.batchWriteItem(BatchWriteItemRequest) API. Use mapper config to
control the retry strategy when UnprocessedItems are returned by the BatchWriteItem API
This method fails to save the batch if the size of an individual object in the batch exceeds 400 KB. For more information on batch restrictions see, http://docs.aws.amazon .com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
If one of the write requests is for a table that is not present, this method does not throw a ResourceNotFoundException but returns a FailedBatch which includes this exception and the unprocessed items.
batchWrite in interface IDynamoDbMapperbatchWrite in class AbstractDynamoDbMapperobjectsToWrite - A list of objects to save to DynamoDB. No version checks are performed, as
required by the DynamoDBClient.batchWriteItem(BatchWriteItemRequest) API.objectsToDelete - A list of objects to delete from DynamoDB. No version checks are performed,
as required by the DynamoDBClient.batchWriteItem(BatchWriteItemRequest)
API.config - Only DynamoDbMapperConfig.getTableNameOverride() and
DynamoDbMapperConfig.batchWriteRetryStrategy() are considered. If
TableNameOverride is specified, all objects in the two parameter lists will be
considered to belong to the given table override. In particular, this method
always acts as if SaveBehavior.CLOBBER was specified regardless of the
value of the config parameter.DynamoDbMapperConfig.getTableNameOverride(),
DynamoDbMapperConfig.batchWriteRetryStrategy()public Map<String,List<Object>> batchLoad(Iterable<? extends Object> itemsToGet, DynamoDbMapperConfig config)
IDynamoDbMapperbatchLoad in interface IDynamoDbMapperbatchLoad in class AbstractDynamoDbMapperitemsToGet - Key objects, corresponding to the class to fetch, with their primary key values
set.config - Only DynamoDbMapperConfig.getTableNameOverride() and
DynamoDbMapperConfig.getConsistentReads() are considered.public Map<String,List<Object>> batchLoad(Map<Class<?>,List<KeyPair>> itemsToGet, DynamoDbMapperConfig config)
IDynamoDbMapperDynamoDbMapper#batchLoad(List, DynamoDbMapperConfig)batchLoad in interface IDynamoDbMapperbatchLoad in class AbstractDynamoDbMapperitemsToGet - Map from class to load to list of primary key attributes.config - Only DynamoDbMapperConfig.getTableNameOverride() and
DynamoDbMapperConfig.getConsistentReads() are considered.public <T> PaginatedScanList<T> scan(Class<T> clazz, DynamoDbScanExpression scanExpression, DynamoDbMapperConfig config)
IDynamoDbMapperCallers should be aware that the returned list is unmodifiable, and any attempts to modify the list will result in an UnsupportedOperationException.
You can specify the pagination loading strategy for this scan operation. By default, the list returned is lazily loaded when possible.
scan in interface IDynamoDbMapperscan in class AbstractDynamoDbMapperT - The type of the objects being returned.clazz - The class annotated with DynamoDB annotations describing how to store the object
data in Amazon DynamoDB.scanExpression - Details on how to run the scan, including any filters to apply to limit results.config - The configuration to use for this scan, which overrides the default provided at
object construction.PaginatedScanList,
DynamoDbMapperConfig.PaginationLoadingStrategypublic <T> PaginatedParallelScanList<T> parallelScan(Class<T> clazz, DynamoDbScanExpression scanExpression, int totalSegments, DynamoDbMapperConfig config)
IDynamoDbMapperCallers should be aware that the returned list is unmodifiable, and any attempts to modify the list will result in an UnsupportedOperationException.
You can specify the pagination loading strategy for this parallel scan operation. By default, the list returned is lazily loaded when possible.
parallelScan in interface IDynamoDbMapperparallelScan in class AbstractDynamoDbMapperT - The type of the objects being returned.clazz - The class annotated with DynamoDB annotations describing how to store the object
data in Amazon DynamoDB.scanExpression - Details on how to run the scan, including any filters to apply to limit results.totalSegments - Number of total parallel scan segments. Range: 1 - 4096config - The configuration to use for this scan, which overrides the default provided at
object construction.PaginatedParallelScanList,
DynamoDbMapperConfig.PaginationLoadingStrategypublic <T> ScanResultPage<T> scanPage(Class<T> clazz, DynamoDbScanExpression scanExpression, DynamoDbMapperConfig config)
IDynamoDbMapperscanPage in interface IDynamoDbMapperscanPage in class AbstractDynamoDbMapperT - The type of the objects being returned.clazz - The class annotated with DynamoDB annotations describing how to store the object
data in Amazon DynamoDB.scanExpression - Details on how to run the scan, including any filters to apply to limit results.config - The configuration to use for this scan, which overrides the default provided at
object construction.public <T> PaginatedQueryList<T> query(Class<T> clazz, DynamoDbQueryExpression<T> queryExpression, DynamoDbMapperConfig config)
IDynamoDbMapperWhen the query is on any local/global secondary index, callers should be aware that the returned object(s) will only contain item attributes that are projected into the index. All the other unprojected attributes will be saved as type default values.
Callers should also be aware that the returned list is unmodifiable, and any attempts to modify the list will result in an UnsupportedOperationException.
You can specify the pagination loading strategy for this query operation. By default, the list returned is lazily loaded when possible.
query in interface IDynamoDbMapperquery in class AbstractDynamoDbMapperT - The type of the objects being returned.clazz - The class annotated with DynamoDB annotations describing how to store the object
data in Amazon DynamoDB.queryExpression - Details on how to run the query, including any conditions on the key valuesconfig - The configuration to use for this query, which overrides the default provided at
object construction.PaginatedQueryList,
DynamoDbMapperConfig.PaginationLoadingStrategypublic <T> QueryResultPage<T> queryPage(Class<T> clazz, DynamoDbQueryExpression<T> queryExpression, DynamoDbMapperConfig config)
IDynamoDbMapperqueryPage in interface IDynamoDbMapperqueryPage in class AbstractDynamoDbMapperT - The type of the objects being returned.clazz - The class annotated with DynamoDB annotations describing how to store the object
data in AWS DynamoDB.queryExpression - Details on how to run the query, including any conditions on the key valuesconfig - The configuration to use for this query, which overrides the default provided at
object construction.public int count(Class<?> clazz, DynamoDbScanExpression scanExpression, DynamoDbMapperConfig config)
IDynamoDbMapperThis operation will scan your entire table, and can therefore be very expensive. Use with caution.
count in interface IDynamoDbMappercount in class AbstractDynamoDbMapperclazz - The class mapped to a DynamoDB table.scanExpression - The parameters for running the scan.config - The configuration to use for this scan, which overrides the default provided at
object construction.public <T> int count(Class<T> clazz, DynamoDbQueryExpression<T> queryExpression, DynamoDbMapperConfig config)
IDynamoDbMappercount in interface IDynamoDbMappercount in class AbstractDynamoDbMapperclazz - The class mapped to a DynamoDB table.queryExpression - The parameters for running the scan.config - The mapper configuration to use for the query, which overrides the default
provided at object construction.public S3ClientCache s3ClientCache()
IDynamoDbMapperS3ClientCache for accessing S3.s3ClientCache in interface IDynamoDbMappers3ClientCache in class AbstractDynamoDbMapperpublic S3Link createS3Link(Region s3region, String bucketName, String key)
IDynamoDbMappercreateS3Link in interface IDynamoDbMappercreateS3Link in class AbstractDynamoDbMapperpublic S3Link createS3Link(String s3region, String bucketName, String key)
IDynamoDbMappercreateS3Link in interface IDynamoDbMappercreateS3Link in class AbstractDynamoDbMapperpublic <T> CreateTableRequest generateCreateTableRequest(Class<T> clazz, DynamoDbMapperConfig config)
generateCreateTableRequest in class AbstractDynamoDbMapperpublic <T> DeleteTableRequest generateDeleteTableRequest(Class<T> clazz, DynamoDbMapperConfig config)
generateDeleteTableRequest in class AbstractDynamoDbMapperpublic <T,H,R> DynamoDbTableMapper<T,H,R> newTableMapper(Class<T> clazz)
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.clazz - The object class.Copyright © 2017 Amazon Web Services, Inc. All Rights Reserved.