ddb-create-table-example-01
Description
This example's purpose is to support the LocalStack on using the platform with Java AWS applications.
Prerequisites
- Maven 3.8.5+ & Java 17
- LocalStack
- Docker - for running LocalStack
- AWS CLI and awslocal
Run LocalStack
Project Structure
.
├── pom.xml
├── README.md
└── src
└── main
├── java
│ └── v2
│ └── dynamodb
│ └── DynamoDBService.java
└── resources
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cloud.localstack</groupId>
<artifactId>ddb-create-table-example-01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
</properties>
<dependencies>
<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>
</project>
DynamoDBService.java
package v2.dynamodb;
import java.net.URI;
import java.text.ParseException;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;
import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;
public class DynamoDBService {
// credentials that can be replaced with real AWS values
private static final String ACCESS_KEY = "test";
private static final String SECRET_KEY = "test";
private static String TABLE_NAME = "person";
private static AwsCredentialsProvider credentials = StaticCredentialsProvider.create(
AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY));
// create the dynamoDB client using the credentials and specific region
private static Region region = Region.US_EAST_1;
// create a dynamoDB client
private static DynamoDbClient dynamoDbClient = DynamoDbClient.builder()
.region(region)
.credentialsProvider(
credentials)
.endpointOverride(URI.create("https://localhost.localstack.cloud:4566"))
.build();
public static void main(String[] args) throws ParseException {
createTable(dynamoDbClient, "person", "id");
}
public static String createTable(DynamoDbClient ddb, String tableName, String key) {
DynamoDbWaiter dbWaiter = ddb.waiter();
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(AttributeDefinition.builder()
.attributeName(key)
.attributeType(ScalarAttributeType.S)
.build())
.keySchema(KeySchemaElement.builder()
.attributeName(key)
.keyType(KeyType.HASH)
.build())
.provisionedThroughput(ProvisionedThroughput.builder()
.readCapacityUnits(10L)
.writeCapacityUnits(10L)
.build())
.tableName(tableName)
.build();
String newTable;
try {
CreateTableResponse response = ddb.createTable(request);
DescribeTableRequest tableRequest = DescribeTableRequest.builder()
.tableName(tableName)
.build();
// Wait until the Amazon DynamoDB table is created.
WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest);
waiterResponse.matched().response().ifPresent(System.out::println);
newTable = response.tableDescription().tableName();
return newTable;
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
}
Running and Testing
mvn clean package
mvn exec:java -Dexec.mainClass="v2.dynamodb.DynamoDBService"
Output:
DescribeTableResponse(Table=TableDescription(AttributeDefinitions=[AttributeDefinition(AttributeName=id, AttributeType=S)], TableName=person, KeySchema=[KeySchemaElement(AttributeName=id, KeyType=HASH)], TableStatus=ACTIVE, CreationDateTime=2024-09-10T08:07:49.801Z, ProvisionedThroughput=ProvisionedThroughputDescription(LastIncreaseDateTime=1970-01-01T00:00:00Z, LastDecreaseDateTime=1970-01-01T00:00:00Z, NumberOfDecreasesToday=0, ReadCapacityUnits=10, WriteCapacityUnits=10), TableSizeBytes=0, ItemCount=0, TableArn=arn:aws:dynamodb:us-east-1:000000000000:table/person, TableId=0ab324d0-1be1-4222-a882-c2b26ad8bf88, Replicas=[], DeletionProtectionEnabled=false))
Source Code
https://github.com/ZbCiok/zjc-examples/tree/main/aws/aws/dynamodb/ddb-create-table-example-01
📄️ 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