[JAVA-23452] Moved dynamodb to a separate module

This commit is contained in:
panos-kakos 2023-10-07 18:38:44 +03:00
parent 8aecd4aa86
commit 55296d07f4
13 changed files with 234 additions and 15 deletions

2
aws-modules/aws-dynamodb/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target/
.idea/

View File

@ -0,0 +1,7 @@
## AWS DYNAMODB
This module contains articles about AWS DynamoDB
### Relevant articles
- [Integration Testing with a Local DynamoDB Instance](https://www.baeldung.com/dynamodb-local-integration-tests)

View File

@ -0,0 +1,89 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-dynamodb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>aws-dynamodb</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>aws-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${dynamodblocal.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-plugins-version}</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope></includeScope>
<includeTypes>so,dll,dylib</includeTypes>
<outputDirectory>native-libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<gson.version>2.8.0</gson.version>
<dynamodblocal.version>1.21.1</dynamodblocal.version>
<commons-codec-version>1.10.L001</commons-codec-version>
<jets3t-version>0.9.4.0006L</jets3t-version>
<maven-plugins-version>3.1.1</maven-plugins-version>
</properties>
</project>

View File

@ -0,0 +1,51 @@
package com.baeldung.dynamodb.entity;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
@DynamoDBTable(tableName = "ProductInfo")
public class ProductInfo {
private String id;
private String msrp;
private String cost;
public ProductInfo() {
}
public ProductInfo(String cost, String msrp) {
this.msrp = msrp;
this.cost = cost;
}
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public String getId() {
return id;
}
@DynamoDBAttribute
public String getMsrp() {
return msrp;
}
@DynamoDBAttribute
public String getCost() {
return cost;
}
public void setId(String id) {
this.id = id;
}
public void setMsrp(String msrp) {
this.msrp = msrp;
}
public void setCost(String cost) {
this.cost = cost;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.dynamodb.repository;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public abstract class AbstractRepository<T, ID extends Serializable> {
protected DynamoDBMapper mapper;
protected Class<T> entityClass;
protected AbstractRepository() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
// This entityClass refers to the actual entity class in the subclass declaration.
// For instance, ProductInfoDAO extends AbstractDAO<ProductInfo, String>
// In this case entityClass = ProductInfo, and ID is String type
// which refers to the ProductInfo's partition key string value
this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
}
public void save(T t) {
mapper.save(t);
}
public T findOne(ID id) {
return mapper.load(entityClass, id);
}
/**
* <strong>WARNING:</strong> It is not recommended to perform full table scan
* targeting the real production environment.
*
* @return All items
*/
public List<T> findAll() {
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
return mapper.scan(entityClass, scanExpression);
}
public void setMapper(DynamoDBMapper dynamoDBMapper) {
this.mapper = dynamoDBMapper;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.dynamodb.repository;
import com.baeldung.dynamodb.entity.ProductInfo;
public class ProductInfoRepository extends AbstractRepository<ProductInfo, String> {
}

View File

@ -0,0 +1,4 @@
db_hostname=<RDS EndPoint>
db_username=username
db_password=password
db_database=mydb

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -49,10 +49,10 @@ public class ProductInfoRepositoryUnitTest {
@BeforeClass
public static void setupClass() {
Properties testProperties = loadFromFileInClasspath("test.properties")
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
.filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY)))
.filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY)))
.filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT)))
.orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);

View File

@ -30,17 +30,6 @@
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${dynamodblocal.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -5,6 +5,14 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-modules</artifactId>
<name>aws-modules</name>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.523</version>
<scope>compile</scope>
</dependency>
</dependencies>
<packaging>pom</packaging>
<parent>
@ -15,6 +23,7 @@
<modules>
<module>aws-app-sync</module>
<module>aws-dynamodb</module>
<module>aws-lambda-modules</module>
<module>aws-miscellaneous</module>
<module>aws-reactive</module>