ddb-delete-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-delete-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.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;
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 {
deleteTable(dynamoDbClient, "person");
}
public static void deleteTable(DynamoDbClient ddb, String tableName) {
DeleteTableRequest request = DeleteTableRequest.builder()
.tableName(tableName)
.build();
try {
ddb.deleteTable(request);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println(tableName + " was successfully deleted!");
}
}
Running and Testing
mvn clean package
mvn exec:java -Dexec.mainClass="v2.dynamodb.DynamoDBService"
Output:
person was successfully deleted!
Source Code
https://github.com/ZbCiok/zjc-examples/tree/main/aws/aws/dynamodb/ddb-delete-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