/dynamodb-java-sdk-v2

Working with DynamoDB using the AWS SDK v2 for Java including the Enhanced Client

Primary LanguageJava

AWS SDK v2 DynamoDB including Enhanced Client

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/

Prerequisites

  • Have Java installed; This has been tested with Java 11 but may work with other versions

Connecting to DynamoDB

DynamoDB Local or LocalStack

See Connect to Local DynamoDB under Features.

Connect to Live AWS DynamoDB

  • 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.

Running The Code

You can run one of these:

  • src/main/java/com/davidagood/awssdkv2/dynamodb/App.java
  • src/main/java/com/davidagood/awssdkv2/dynamodb/AppBasic.java

Blog Posts Referencing This Repo

Features

Repository Layer Isolation

See here: DynamoDB Repository Layer Isolation in Java

Integration Testing with DynamoDB Local

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.

Enhanced Client using Static Schema

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.

Create Table If Not Exists

If the table does not exist, it is created at application startup time.

See com.davidagood.awssdkv2.dynamodb.repository.DynamoDbRepository.createTableIfNotExists

Connect to Local DynamoDB

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

Immutable Value Classes Using @DynamoDbImmutable

See com.davidagood.awssdkv2.dynamodb.repository.ImmutableBeanItem.

TODO

Entities

  • Regular
  • Immutable with Lombok
  • Immutable without Lombok
  • Encapsulated with MapStruct (DynamoDb Immutables not even necessary then?)

Point AWS CLI to DynamoDB Local

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