A simple mapper for converting to and from DynamoDB AttributeValues and POJOs using Jackson.
Dynamo Mapper is still in development. Currently, only single gets, puts, updates, and deletes are supported. More is planned in the future and submissions are welcome.
Dynamo Mapper is available from Maven Central.
implementation 'com.autonomouslogic.dynamomapper:dynamo-mapper:version'
<dependency>
<groupId>com.autonomouslogic.dynamomapper</groupId>
<artifactId>dynamo-mapper</artifactId>
<version>version</version>
</dependency>
Dynamo Mapper functions as a wrapper around the existing AWS DynamoDB v2 SDK. First, you need to construct a DynamoDB client object and a Jackson ObjectMapper, and then pass those to the Dynamo Mapper builder. If you do not provide a DynamoDB client or Jackson ObjectMapper, defaults will be used.
var dynamoMapper = DynamoMapper.builder()
.client(DynamoDbClient.create())
.objectMapper(new ObjectMapper())
.tableNameDecorator(decorator) // optional
.build();
Or, for the asynchronous API:
var asyncDynamoMapper = DynamoAsyncMapper.builder()
.client(DynamoDbAsyncClient.create())
.objectMapper(new ObjectMapper())
.tableNameDecorator(decorator) // optional
.build();
Dynamo Mapper relies on Jackson for its serialization. Generally, anything that works in Jackson will work in Dynamo Mapper. Only a few extra annotations are required.
@DynamoTableName("table-name")
public class ModelObject {
@JsonProperty
@DynamoPrimaryKey
private String primaryKey;
@JsonProperty
private String otherValue;
/* etc. */
}
Using the synchronous API:
var item = dynamoMapper.getItemFromPrimaryKey("key-value", ModelObject.class).item();
Using the asynchronous API:
dynamoMapper.getItemFromPrimaryKey("key-value", ModelObject.class).thenAccept(response -> {
var item = response.item();
});
Using the synchronous API:
var item = new ModelObject().setPrimaryKey("key-value");
var response = dynamoMapper.putItemFromKeyObject(item);
Using the asynchronous API:
var item = new ModelObject().setPrimaryKey("key-value");
dynamoMapper.putItemFromKeyObject(item).thenAccept(response -> {
// etc.
});
- Jackson will include all null values, to prevent this taking up space in DynamoDB, use
@JsonInclude(JsonInclude.Include.NON_NULL)
- To properly serialize BigDecimals, use
objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN)
- To encode
java.time
objects properly, usejackson-datatype-jsr310
The DynamoDBMapper in the v1 AWS SDK and the DynamoDB Enhanced Client in the v2 AWS SDK both provide similar mapping functionality.
This idea for this library was originally created to provide the same mapping as the v1 mapper, but for the v2 SDK, before the enhanced client was released. The v2 SDK now provides this mapping functionality officially, but has a few short-comings. For instance, the annotations must be added to methods and not properties. This makes Lombok models useless. Jackson is a widely used and mature library. with advanced features that will never, and shouldn't, be implemented in the enhanced client.
There are some cons to doing it this way. For instance, it's not possible to have a different schema in the DynamoDB and JSON versions of an object.
Feature | Dynamo Mapper | DynamoDBMapper (v1) | DynamoDb Enhanced Client (v2) |
---|---|---|---|
Synchronous API | Yes | Yes | Yes |
Asynchronous API | Yes | No | Yes |
Lombok compatible | Yes | Partial? | No? |
- https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.html
- https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/dynamodb-enhanced-client.html
- https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/package-summary.html
- https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/dynamodb/package-summary.html
Dynamo Mapper follows semantic versioning.
Dynamo Mapper follows Palantir with tabs.
Automatic code formatting can be done by running ./gradlew spotlessApply
.
Dynamo Mapper is licensed under the MIT-0 license.
Type | Status |
---|---|
LGTM | |
CodeClimate | |
SonarCloud | |
Libraries.io | |
Snyk | |
Codecov | |
Synatype Lift | link |