From a60c9ae9bac8109de34d46b7d57e4a11484cb018 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Wed, 20 Nov 2024 10:41:47 -0500 Subject: [PATCH] updated a DynamoDB example to address a tic --- .../dynamodb/ScenarioPartiQLBatch.java | 751 ++++++++++-------- 1 file changed, 399 insertions(+), 352 deletions(-) diff --git a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/ScenarioPartiQLBatch.java b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/ScenarioPartiQLBatch.java index d7377957a5a..be5aede9212 100644 --- a/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/ScenarioPartiQLBatch.java +++ b/javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/ScenarioPartiQLBatch.java @@ -4,6 +4,7 @@ package com.example.dynamodb; // snippet-start:[dynamodb.java2.scenario.partiql.batch.import] + import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; @@ -24,6 +25,7 @@ import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter; + import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -32,380 +34,425 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * + *

* This Java example performs the following tasks: - * + *

* 1. Creates the Amazon DynamoDB movie table with a partition and sort key. * 2. Puts new records into the table using a BatchExecuteStatement * 3. Updates items using a BatchExecuteStatement. * 4. Deletes items by using a BatchExecuteStatement. * 5. Deletes the table. - * + *

* To see another code example with more options using PartiQL, see the * ScenarioPartiQ code example. */ // snippet-start:[dynamodb.java2.scenario.partiql.batch.main] public class ScenarioPartiQLBatch { - public static void main(String[] args) throws IOException { - String tableName = "MoviesPartiQBatch"; - Region region = Region.US_EAST_1; - DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build(); - - System.out.println("******* Creating an Amazon DynamoDB table named " + tableName - + " with a key named year and a sort key named title."); - createTable(ddb, tableName); - - System.out.println("******* Adding multiple records into the " + tableName - + " table using a batch command."); - putRecordBatch(ddb); - - System.out.println("******* Updating multiple records using a batch command."); - updateTableItemBatch(ddb); - - System.out.println("******* Deleting multiple records using a batch command."); - deleteItemBatch(ddb); - - System.out.println("******* Deleting the Amazon DynamoDB table."); - deleteDynamoDBTable(ddb, tableName); - ddb.close(); + public static void main(String[] args) throws IOException { + String tableName = "MoviesPartiQBatch"; + Region region = Region.US_EAST_1; + DynamoDbClient ddb = DynamoDbClient.builder() + .region(region) + .build(); + + System.out.println("******* Creating an Amazon DynamoDB table named " + tableName + + " with a key named year and a sort key named title."); + createTable(ddb, tableName); + + System.out.println("******* Adding multiple records into the " + tableName + + " table using a batch command."); + putRecordBatch(ddb); + + // Update multiple movies by using the BatchExecute statement. + String title1 = "Star Wars"; + int year1 = 1977; + String title2 = "Wizard of Oz"; + int year2 = 1939; + + System.out.println("Updating two movies with producer information: " + title1 + " and " + title2 + "."); + getBatch(ddb, tableName, title1, title2, year1, year2); + + System.out.println("******* Updating multiple records using a batch command."); + updateTableItemBatch(ddb); + + System.out.println("******* Deleting multiple records using a batch command."); + deleteItemBatch(ddb); + + System.out.println("******* Deleting the Amazon DynamoDB table."); + deleteDynamoDBTable(ddb, tableName); + ddb.close(); + } + + public static boolean getBatch(DynamoDbClient ddb, String tableName, String title1, String title2, int year1, int year2) { + String getBatch = "SELECT * FROM " + tableName + " WHERE title = ? AND year = ?"; + + List statements = new ArrayList<>(); + statements.add(BatchStatementRequest.builder() + .statement(getBatch) + .parameters(AttributeValue.builder().s(title1).build(), + AttributeValue.builder().n(String.valueOf(year1)).build()) + .build()); + statements.add(BatchStatementRequest.builder() + .statement(getBatch) + .parameters(AttributeValue.builder().s(title2).build(), + AttributeValue.builder().n(String.valueOf(year2)).build()) + .build()); + + BatchExecuteStatementRequest batchExecuteStatementRequest = BatchExecuteStatementRequest.builder() + .statements(statements) + .build(); + + try { + BatchExecuteStatementResponse response = ddb.batchExecuteStatement(batchExecuteStatementRequest); + if (!response.responses().isEmpty()) { + response.responses().forEach(r -> { + System.out.println(r.item().get("title") + "\\t" + r.item().get("year")); + }); + return true; + } else { + System.out.println("Couldn't find either " + title1 + " or " + title2 + "."); + return false; + } + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + return false; } - - public static void createTable(DynamoDbClient ddb, String tableName) { - DynamoDbWaiter dbWaiter = ddb.waiter(); - ArrayList attributeDefinitions = new ArrayList<>(); - - // Define attributes. - attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("year") - .attributeType("N") - .build()); - - attributeDefinitions.add(AttributeDefinition.builder() - .attributeName("title") - .attributeType("S") - .build()); - - ArrayList tableKey = new ArrayList<>(); - KeySchemaElement key = KeySchemaElement.builder() - .attributeName("year") - .keyType(KeyType.HASH) - .build(); - - KeySchemaElement key2 = KeySchemaElement.builder() - .attributeName("title") - .keyType(KeyType.RANGE) // Sort - .build(); - - // Add KeySchemaElement objects to the list. - tableKey.add(key); - tableKey.add(key2); - - CreateTableRequest request = CreateTableRequest.builder() - .keySchema(tableKey) - .provisionedThroughput(ProvisionedThroughput.builder() - .readCapacityUnits(new Long(10)) - .writeCapacityUnits(new Long(10)) - .build()) - .attributeDefinitions(attributeDefinitions) - .tableName(tableName) - .build(); - - try { - CreateTableResponse response = ddb.createTable(request); - DescribeTableRequest tableRequest = DescribeTableRequest.builder() - .tableName(tableName) - .build(); - - // Wait until the Amazon DynamoDB table is created. - WaiterResponse waiterResponse = dbWaiter - .waitUntilTableExists(tableRequest); - waiterResponse.matched().response().ifPresent(System.out::println); - String newTable = response.tableDescription().tableName(); - System.out.println("The " + newTable + " was successfully created."); - - } catch (DynamoDbException e) { - System.err.println(e.getMessage()); - System.exit(1); - } + } + + public static void createTable(DynamoDbClient ddb, String tableName) { + DynamoDbWaiter dbWaiter = ddb.waiter(); + ArrayList attributeDefinitions = new ArrayList<>(); + + // Define attributes. + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName("year") + .attributeType("N") + .build()); + + attributeDefinitions.add(AttributeDefinition.builder() + .attributeName("title") + .attributeType("S") + .build()); + + ArrayList tableKey = new ArrayList<>(); + KeySchemaElement key = KeySchemaElement.builder() + .attributeName("year") + .keyType(KeyType.HASH) + .build(); + + KeySchemaElement key2 = KeySchemaElement.builder() + .attributeName("title") + .keyType(KeyType.RANGE) // Sort + .build(); + + // Add KeySchemaElement objects to the list. + tableKey.add(key); + tableKey.add(key2); + + CreateTableRequest request = CreateTableRequest.builder() + .keySchema(tableKey) + .provisionedThroughput(ProvisionedThroughput.builder() + .readCapacityUnits(10L) + .writeCapacityUnits(10L) + .build()) + .attributeDefinitions(attributeDefinitions) + .tableName(tableName) + .build(); + + try { + CreateTableResponse response = ddb.createTable(request); + DescribeTableRequest tableRequest = DescribeTableRequest.builder() + .tableName(tableName) + .build(); + + // Wait until the Amazon DynamoDB table is created. + WaiterResponse waiterResponse = dbWaiter + .waitUntilTableExists(tableRequest); + waiterResponse.matched().response().ifPresent(System.out::println); + String newTable = response.tableDescription().tableName(); + System.out.println("The " + newTable + " was successfully created."); + + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + System.exit(1); } - - public static void putRecordBatch(DynamoDbClient ddb) { - String sqlStatement = "INSERT INTO MoviesPartiQBatch VALUE {'year':?, 'title' : ?, 'info' : ?}"; - try { - // Create three movies to add to the Amazon DynamoDB table. - // Set data for Movie 1. - List parameters = new ArrayList<>(); - - AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue att2 = AttributeValue.builder() - .s("My Movie 1") - .build(); - - AttributeValue att3 = AttributeValue.builder() - .s("No Information") - .build(); - - parameters.add(att1); - parameters.add(att2); - parameters.add(att3); - - BatchStatementRequest statementRequestMovie1 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parameters) - .build(); - - // Set data for Movie 2. - List parametersMovie2 = new ArrayList<>(); - AttributeValue attMovie2 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attMovie2A = AttributeValue.builder() - .s("My Movie 2") - .build(); - - AttributeValue attMovie2B = AttributeValue.builder() - .s("No Information") - .build(); - - parametersMovie2.add(attMovie2); - parametersMovie2.add(attMovie2A); - parametersMovie2.add(attMovie2B); - - BatchStatementRequest statementRequestMovie2 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersMovie2) - .build(); - - // Set data for Movie 3. - List parametersMovie3 = new ArrayList<>(); - AttributeValue attMovie3 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attMovie3A = AttributeValue.builder() - .s("My Movie 3") - .build(); - - AttributeValue attMovie3B = AttributeValue.builder() - .s("No Information") - .build(); - - parametersMovie3.add(attMovie3); - parametersMovie3.add(attMovie3A); - parametersMovie3.add(attMovie3B); - - BatchStatementRequest statementRequestMovie3 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersMovie3) - .build(); - - // Add all three movies to the list. - List myBatchStatementList = new ArrayList<>(); - myBatchStatementList.add(statementRequestMovie1); - myBatchStatementList.add(statementRequestMovie2); - myBatchStatementList.add(statementRequestMovie3); - - BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() - .statements(myBatchStatementList) - .build(); - - BatchExecuteStatementResponse response = ddb.batchExecuteStatement(batchRequest); - System.out.println("ExecuteStatement successful: " + response.toString()); - System.out.println("Added new movies using a batch command."); - - } catch (DynamoDbException e) { - System.err.println(e.getMessage()); - System.exit(1); - } + } + + public static void putRecordBatch(DynamoDbClient ddb) { + String sqlStatement = "INSERT INTO MoviesPartiQBatch VALUE {'year':?, 'title' : ?, 'info' : ?}"; + try { + // Create three movies to add to the Amazon DynamoDB table. + // Set data for Movie 1. + List parameters = new ArrayList<>(); + + AttributeValue att1 = AttributeValue.builder() + .n(String.valueOf("1977")) + .build(); + + AttributeValue att2 = AttributeValue.builder() + .s("Star Wars") + .build(); + + AttributeValue att3 = AttributeValue.builder() + .s("No Information") + .build(); + + parameters.add(att1); + parameters.add(att2); + parameters.add(att3); + + BatchStatementRequest statementRequestMovie1 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parameters) + .build(); + + // Set data for Movie 2. + List parametersMovie2 = new ArrayList<>(); + AttributeValue attMovie2 = AttributeValue.builder() + .n(String.valueOf("1939")) + .build(); + + AttributeValue attMovie2A = AttributeValue.builder() + .s("Wizard of Oz") + .build(); + + AttributeValue attMovie2B = AttributeValue.builder() + .s("No Information") + .build(); + + parametersMovie2.add(attMovie2); + parametersMovie2.add(attMovie2A); + parametersMovie2.add(attMovie2B); + + BatchStatementRequest statementRequestMovie2 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersMovie2) + .build(); + + // Set data for Movie 3. + List parametersMovie3 = new ArrayList<>(); + AttributeValue attMovie3 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue attMovie3A = AttributeValue.builder() + .s("My Movie 3") + .build(); + + AttributeValue attMovie3B = AttributeValue.builder() + .s("No Information") + .build(); + + parametersMovie3.add(attMovie3); + parametersMovie3.add(attMovie3A); + parametersMovie3.add(attMovie3B); + + BatchStatementRequest statementRequestMovie3 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersMovie3) + .build(); + + // Add all three movies to the list. + List myBatchStatementList = new ArrayList<>(); + myBatchStatementList.add(statementRequestMovie1); + myBatchStatementList.add(statementRequestMovie2); + myBatchStatementList.add(statementRequestMovie3); + + BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() + .statements(myBatchStatementList) + .build(); + + BatchExecuteStatementResponse response = ddb.batchExecuteStatement(batchRequest); + System.out.println("ExecuteStatement successful: " + response.toString()); + System.out.println("Added new movies using a batch command."); + + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + System.exit(1); } - - public static void updateTableItemBatch(DynamoDbClient ddb) { - String sqlStatement = "UPDATE MoviesPartiQBatch SET info = 'directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack' where year=? and title=?"; - List parametersRec1 = new ArrayList<>(); - - // Update three records. - AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue att2 = AttributeValue.builder() - .s("My Movie 1") - .build(); - - parametersRec1.add(att1); - parametersRec1.add(att2); - - BatchStatementRequest statementRequestRec1 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec1) - .build(); - - // Update record 2. - List parametersRec2 = new ArrayList<>(); - AttributeValue attRec2 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attRec2a = AttributeValue.builder() - .s("My Movie 2") - .build(); - - parametersRec2.add(attRec2); - parametersRec2.add(attRec2a); - BatchStatementRequest statementRequestRec2 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec2) - .build(); - - // Update record 3. - List parametersRec3 = new ArrayList<>(); - AttributeValue attRec3 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attRec3a = AttributeValue.builder() - .s("My Movie 3") - .build(); - - parametersRec3.add(attRec3); - parametersRec3.add(attRec3a); - BatchStatementRequest statementRequestRec3 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec3) - .build(); - - // Add all three movies to the list. - List myBatchStatementList = new ArrayList<>(); - myBatchStatementList.add(statementRequestRec1); - myBatchStatementList.add(statementRequestRec2); - myBatchStatementList.add(statementRequestRec3); - - BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() - .statements(myBatchStatementList) - .build(); - - try { - BatchExecuteStatementResponse response = ddb.batchExecuteStatement(batchRequest); - System.out.println("ExecuteStatement successful: " + response.toString()); - System.out.println("Updated three movies using a batch command."); - - } catch (DynamoDbException e) { - System.err.println(e.getMessage()); - System.exit(1); - } - System.out.println("Item was updated!"); + } + + public static void updateTableItemBatch(DynamoDbClient ddb) { + String sqlStatement = "UPDATE MoviesPartiQBatch SET info = 'directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack' where year=? and title=?"; + List parametersRec1 = new ArrayList<>(); + + // Update three records. + AttributeValue att1 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue att2 = AttributeValue.builder() + .s("My Movie 1") + .build(); + + parametersRec1.add(att1); + parametersRec1.add(att2); + + BatchStatementRequest statementRequestRec1 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec1) + .build(); + + // Update record 2. + List parametersRec2 = new ArrayList<>(); + AttributeValue attRec2 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue attRec2a = AttributeValue.builder() + .s("My Movie 2") + .build(); + + parametersRec2.add(attRec2); + parametersRec2.add(attRec2a); + BatchStatementRequest statementRequestRec2 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec2) + .build(); + + // Update record 3. + List parametersRec3 = new ArrayList<>(); + AttributeValue attRec3 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue attRec3a = AttributeValue.builder() + .s("My Movie 3") + .build(); + + parametersRec3.add(attRec3); + parametersRec3.add(attRec3a); + BatchStatementRequest statementRequestRec3 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec3) + .build(); + + // Add all three movies to the list. + List myBatchStatementList = new ArrayList<>(); + myBatchStatementList.add(statementRequestRec1); + myBatchStatementList.add(statementRequestRec2); + myBatchStatementList.add(statementRequestRec3); + + BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() + .statements(myBatchStatementList) + .build(); + + try { + BatchExecuteStatementResponse response = ddb.batchExecuteStatement(batchRequest); + System.out.println("ExecuteStatement successful: " + response.toString()); + System.out.println("Updated three movies using a batch command."); + + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + System.exit(1); } - - public static void deleteItemBatch(DynamoDbClient ddb) { - String sqlStatement = "DELETE FROM MoviesPartiQBatch WHERE year = ? and title=?"; - List parametersRec1 = new ArrayList<>(); - - // Specify three records to delete. - AttributeValue att1 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue att2 = AttributeValue.builder() - .s("My Movie 1") - .build(); - - parametersRec1.add(att1); - parametersRec1.add(att2); - - BatchStatementRequest statementRequestRec1 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec1) - .build(); - - // Specify record 2. - List parametersRec2 = new ArrayList<>(); - AttributeValue attRec2 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attRec2a = AttributeValue.builder() - .s("My Movie 2") - .build(); - - parametersRec2.add(attRec2); - parametersRec2.add(attRec2a); - BatchStatementRequest statementRequestRec2 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec2) - .build(); - - // Specify record 3. - List parametersRec3 = new ArrayList<>(); - AttributeValue attRec3 = AttributeValue.builder() - .n(String.valueOf("2022")) - .build(); - - AttributeValue attRec3a = AttributeValue.builder() - .s("My Movie 3") - .build(); - - parametersRec3.add(attRec3); - parametersRec3.add(attRec3a); - - BatchStatementRequest statementRequestRec3 = BatchStatementRequest.builder() - .statement(sqlStatement) - .parameters(parametersRec3) - .build(); - - // Add all three movies to the list. - List myBatchStatementList = new ArrayList<>(); - myBatchStatementList.add(statementRequestRec1); - myBatchStatementList.add(statementRequestRec2); - myBatchStatementList.add(statementRequestRec3); - - BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() - .statements(myBatchStatementList) - .build(); - - try { - ddb.batchExecuteStatement(batchRequest); - System.out.println("Deleted three movies using a batch command."); - - } catch (DynamoDbException e) { - System.err.println(e.getMessage()); - System.exit(1); - } + System.out.println("Item was updated!"); + } + + public static void deleteItemBatch(DynamoDbClient ddb) { + String sqlStatement = "DELETE FROM MoviesPartiQBatch WHERE year = ? and title=?"; + List parametersRec1 = new ArrayList<>(); + + // Specify three records to delete. + AttributeValue att1 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue att2 = AttributeValue.builder() + .s("My Movie 1") + .build(); + + parametersRec1.add(att1); + parametersRec1.add(att2); + + BatchStatementRequest statementRequestRec1 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec1) + .build(); + + // Specify record 2. + List parametersRec2 = new ArrayList<>(); + AttributeValue attRec2 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue attRec2a = AttributeValue.builder() + .s("My Movie 2") + .build(); + + parametersRec2.add(attRec2); + parametersRec2.add(attRec2a); + BatchStatementRequest statementRequestRec2 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec2) + .build(); + + // Specify record 3. + List parametersRec3 = new ArrayList<>(); + AttributeValue attRec3 = AttributeValue.builder() + .n(String.valueOf("2022")) + .build(); + + AttributeValue attRec3a = AttributeValue.builder() + .s("My Movie 3") + .build(); + + parametersRec3.add(attRec3); + parametersRec3.add(attRec3a); + + BatchStatementRequest statementRequestRec3 = BatchStatementRequest.builder() + .statement(sqlStatement) + .parameters(parametersRec3) + .build(); + + // Add all three movies to the list. + List myBatchStatementList = new ArrayList<>(); + myBatchStatementList.add(statementRequestRec1); + myBatchStatementList.add(statementRequestRec2); + myBatchStatementList.add(statementRequestRec3); + + BatchExecuteStatementRequest batchRequest = BatchExecuteStatementRequest.builder() + .statements(myBatchStatementList) + .build(); + + try { + ddb.batchExecuteStatement(batchRequest); + System.out.println("Deleted three movies using a batch command."); + + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + System.exit(1); } + } - public static void deleteDynamoDBTable(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!"); - } + public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { + DeleteTableRequest request = DeleteTableRequest.builder() + .tableName(tableName) + .build(); - private static ExecuteStatementResponse executeStatementRequest(DynamoDbClient ddb, String statement, - List parameters) { - ExecuteStatementRequest request = ExecuteStatementRequest.builder() - .statement(statement) - .parameters(parameters) - .build(); + try { + ddb.deleteTable(request); - return ddb.executeStatement(request); + } catch (DynamoDbException e) { + System.err.println(e.getMessage()); + System.exit(1); } + System.out.println(tableName + " was successfully deleted!"); + } + + private static ExecuteStatementResponse executeStatementRequest(DynamoDbClient ddb, String statement, + List parameters) { + ExecuteStatementRequest request = ExecuteStatementRequest.builder() + .statement(statement) + .parameters(parameters) + .build(); + + return ddb.executeStatement(request); + } } // snippet-end:[dynamodb.java2.scenario.partiql.batch.main] \ No newline at end of file