DynamoDB driver
Table of Contents
# Introduction
This section explains the Mongock Driver for DynamoDB and how to use it.
# DynamoDB driver options and compatibility
Mongock provides the DynamoDBDriver
, which is compatible with the library com.amazonaws:aws-java-sdk-dynamodb
1.x.x.
You can also use the Mongock spring extension to get advantage from the autconfigure approach with Springboot.
# DynamoDB common configuration
When setting configuration via properties file, it must be prefixed by mongock.dynamo-db
# Properties
Property | Description | Type | Default value |
---|---|---|---|
provisionedThroughput.readCapacityUnits | Exactly the same DynamoDB parameter readCapacityUnits. For more information, visit the official DynamodDB documentation for readCapacityUnits. | Long | 50 |
provisionedThroughput.writeCapacityUnits | Exactly the same DynamoDB parameter writeCapacityUnits. For more information, visit the official DynamoDB documentation for writeCapacityUnits. | Long | 50 |
# Get started
Following the get started section, this covers steps 3 and 5 and 6.
# Add maven dependency for the driver (step 2)
# Standalone
<!--Standalone use--><dependency> <groupId>io.mongock</groupId> <artifactId>dynamodb-driver</artifactId></dependency>
# With Springboot
<dependency> <groupId>io.mongock</groupId> <artifactId>dynamodb-springboot-driver</artifactId></dependency>
# Build the driver (setps 5)
This step is only required for builder approach. Mongock handles it when autoconfiguration is enabled.
These classes provide the same two static initializers- withDefaultLock(AmazonDynamoDBClient dynamoDBClient)
- withLockStrategy(AmazonDynamoDBClient dynamoDBClient, long lockAcquiredForMillis, long lockQuitTryingAfterMillis,long lockTryFrequencyMillis)
DynamoDBDriver driver = DynamoDBDriver.withDefaultLock(dynamoDBClient);
# Driver extra configuration (step 6)
# Transactions
Due to the DynamoDB API design, in order to work with transactions, the DynamoDB client needs to use TransactWriteItemsRequest
. To abstract the user from this and provide the most convenient experience, a ChangeUnit needs to inject the class DynamoDBTransactionItems
provided by Mongock, to add the items that will take part of the transaction
Also take into account that DynamoDB only allows 25 elements in a single transaction and one of those items must be the Mongock's ChangeEntry, so the user can add up to 24 items to the transaction.
The following code shows how to use transactions with DynamoDB driver.
@Execution public void execution(DynamoDBTransactionItems client) { Put put = new Put().withTableName(tableName).withItem(myEntity.getMapTtributes()); transactionItems.addChangeEntry(new TransactWriteItem().withPut(put)) }
# Examples
Please visit out example projects in [this repo](https://github.com/mongock/mongock-examples/tree/master/dynamodb) for more information
# Example autoconfiguration with Springboot
mongock:mongock: dynamo-db: provisionedThroughput: readCapacityUnits: 100 writeCapacityUnits: 100
@EnableMongock@SpringBootApplicationpublic class QuickStartApp { public static void main(String[] args) { SpringApplicationBuilder().sources(QuickStartApp.class)().run(args); } @Bean public AmazonDynamoDBClient amazonDynamoDBClient() { return (AmazonDynamoDBClient) AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(SERVICE_ENDPOINT, REGION)) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY))) .build(); }}
# Example: DynamoDB standalone
DynamoDBDriver driver = DynamoDBDriver.withDefaultLock(dynamoDBClient);driver.setProvisionedThroughput(new ProvisionedThroughput(100L, 100L));
# Example: DynamoDB Springboot
@EnableMongock@SpringBootApplicationpublic class QuickStartApp { //AmazonDynamoDBClient beans needs to be injected, so the Mongock context can build the driver @Bean public AmazonDynamoDBClient amazonDynamoDBClient() { return (AmazonDynamoDBClient) AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(SERVICE_ENDPOINT, REGION)) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY))) .build(); }}