ddb-enh-client-spring-boot-1
We will describe two ways for accessing DynamoDB from Spring applications:
- Using DynamoDB module of Spring Data
- Using Enhanced Client for DynamoDB which is part of AWS SDK 2.0.
DynamoDB Enhanced Client Spring Boot
Description
Using Enhanced Client for DynamoDB which is part of AWS SDK 2.0.
The Enhanced DynamoDB Client module provides a higher level API to execute database / dynamodb operations directly with the data classes in our application.
Prerequisites
- JDK 17+
- Maven 3.8.5+ & Java 17
- LocalStack
- Docker - for running LocalStack
Run Docker & LocalStack
Create table
awslocal dynamodb create-table \
--table-name person \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
Project Structure
.
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── jreact
│ │ └── dynamodb
│ │ ├── AwsServicesConfig.java // @Configuration
│ │ ├── DynamoDBService.java // @SpringBootApplication
│ │ ├── PersonBirthdateConverter.java
│ │ └── Person.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
├── example
└── jreact
└── dynamodb
└── DemoApplicationTests.java
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jreact</groupId>
<artifactId>ddb-enh-client-spring-boot-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dynamodb-enh-client-spring-boot-1</name>
<description>Project dynamodb-enh-client-spring-boot-1</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb-enhanced</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>ch.qos.logback</groupId>-->
<!-- <artifactId>logback-classic</artifactId>-->
<!-- <version>1.4.14</version>-->
<!-- </dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.20.47</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
AwsServicesConfig.java
package com.jreact.dynamodb;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import java.net.URI;
@Configuration
public class AwsServicesConfig {
Region region = Region.US_EAST_1;
String accessKeyId = "test";
String secretAccessKey = "test";
@Bean
public DynamoDbClient dynamoDbClient(AwsCredentialsProvider awsCredentialsProvider) {
return DynamoDbClient.builder()
.region(region)
.credentialsProvider(awsCredentialsProvider)
//.endpointOverride(URI.create("http://localhost:4566"))
.endpointOverride(URI.create("https://localhost.localstack.cloud:4566"))
.build();
}
@Bean
public DynamoDbEnhancedClient dynamoDbEnhancedClient(DynamoDbClient dynamoDbClient) {
return DynamoDbEnhancedClient.builder()
.dynamoDbClient(dynamoDbClient)
.build();
}
@Bean
public AwsCredentialsProvider awsCredentialsProvider() {
AwsCredentials awsCredentials = AwsBasicCredentials.create(
accessKeyId,
secretAccessKey
);
return StaticCredentialsProvider.create(awsCredentials);
}
}
Running and Testing
mvn clean package
mvn spring-boot:run
Output:
\\ ...
Retrieved Person: Person{id='1600012356', name='John Doe', birthdate=Mon Jan 01 00:00:00 CET 1979}
Source Code
https://github.com/ZbCiok/zjc-examples/tree/main/aws/aws/dynamodb/ddb-enh-client-spring-boot-1
📄️ ddb-create-table-example-01
Description
📄️ ddb-write-read-example-01
Description
📄️ ddb-delete-table-example-01
Description
📄️ ddb-enh-client-spring-boot-1
We will describe two ways for accessing DynamoDB from Spring applications:
📄️ ddb-spring-data-1
We will describe two ways for accessing DynamoDB from Spring applications:
📄️ Spring Boot SNS Lambda DynamoDB
Description