A repo demonstrating common patterns and use cases for DynamoDB using the AWS SDK v2 for java, including the Enhanced Client.
The various Java SDKs for DynamoDB are enumerated here: https://www.davidagood.com/dynamodb-java-basics/
- Have Java installed; This has been tested with Java 11 but may work with other versions
See Connect to Local DynamoDB under Features.
- Have an AWS account
- Have the AWS CLI installed and configured
If the environment variable DYNAMODB_LOCAL_URL
is not set, the default client expects
to get the credentials from the default credential provider chain,
same with the region.
You can run one of these:
src/main/java/com/davidagood/awssdkv2/dynamodb/App.java
src/main/java/com/davidagood/awssdkv2/dynamodb/AppBasic.java
- Working with Heterogeneous Item Collections in the DynamoDB Enhanced Client for Java
- DynamoDB Enhanced Client for Java: Missing Setters Cause Misleading Error or Unexpected Behavior
- DynamoDB Repository Layer Isolation in Java
See here: DynamoDB Repository Layer Isolation in Java
See all the tests ending in "IT", for example:
src/test/java/com/davidagood/awssdkv2/dynamodb/repository/DynamoDbIT.java
Some ideas are taken from the AWS SDK: here.
Build table schema manually, as opposed to using annotations. See here:
com.davidagood.awssdkv2.dynamodb.repository.StaticSchemaImmutableItem
This is described in the official docs here.
If the table does not exist, it is created at application startup time.
See com.davidagood.awssdkv2.dynamodb.repository.DynamoDbRepository.createTableIfNotExists
If you have Docker installed you can use DynamoDB Local or LocalStack to start a locally running DynamoDB.
To override the DynamoDB client to connect to the local instance, set the enviroment variable
DYNAMODB_LOCAL_URL
.
For example, here's the one-liner to run DynamoDB Local:
docker run -p 8000:8000 amazon/dynamodb-local
Once this is running you can run the example code with the environment variable
DYNAMODB_LOCAL_URL=http://localhost:8000
.
See com.davidagood.awssdkv2.dynamodb.App.buildDynamoDbClient
See com.davidagood.awssdkv2.dynamodb.repository.ImmutableBeanItem
.
- Versioned attribute, https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced#versionedrecordextension
- Integration testing with DynamoDBLocal
- Test/demo features such as:
- DynamoDbTable scan without filtering on type should fail when entity is marshalled, for example see Delivery#setType
- PutItem fails if item already exists, see com.davidagood.awssdkv2.dynamodb.App.insertCustomerDoNotOverwrite
- Test/demo features such as:
- Running DynamoDB locally via LocalStack
- Versioning strategy shown here: https://youtu.be/HaEPXoXVf2k?t=2294
- Pagination example
- Regular
- Immutable with Lombok
- Immutable without Lombok
- Encapsulated with MapStruct (DynamoDb Immutables not even necessary then?)
It may be occasionally useful to use the AWS CLI for troubleshooting while using DynamoDB Local. Here's an example of how to do that:
AWS_ACCESS_KEY_ID=dummy AWS_SECRET_ACCESS_KEY=dummy aws dynamodb list-tables --endpoint-url http://localhost:8000