SDK for Java 2.x を使用した Amazon Redshift の例 - AWS SDK for Java 2.x

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

SDK for Java 2.x を使用した Amazon Redshift の例

次のコード例は、Amazon Redshift AWS SDK for Java 2.x で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。ここでは GitHub、コンテキスト内でコードを設定および実行する方法の手順を確認できます。

開始方法

以下のコード例は、Amazon Redshift の使用を開始する方法を示しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.redshift.RedshiftClient; import software.amazon.awssdk.services.redshift.paginators.DescribeClustersIterable; /** * 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 */ public class HelloRedshift { public static void main(String[] args) { Region region = Region.US_EAST_1; RedshiftClient redshiftClient = RedshiftClient.builder() .region(region) .build(); listClustersPaginator(redshiftClient); } public static void listClustersPaginator(RedshiftClient redshiftClient) { DescribeClustersIterable clustersIterable = redshiftClient.describeClustersPaginator(); clustersIterable.stream() .flatMap(r -> r.clusters().stream()) .forEach(cluster -> System.out .println(" Cluster identifier: " + cluster.clusterIdentifier() + " status = " + cluster.clusterStatus())); } }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「describeClusters」を参照してください。

アクション

次の例は、CreateCluster を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

クラスターを作成する。

public static void createCluster(RedshiftClient redshiftClient, String clusterId, String masterUsername, String masterUserPassword) { try { CreateClusterRequest clusterRequest = CreateClusterRequest.builder() .clusterIdentifier(clusterId) .masterUsername(masterUsername) .masterUserPassword(masterUserPassword) .nodeType("ra3.4xlarge") .publiclyAccessible(true) .numberOfNodes(2) .build(); CreateClusterResponse clusterResponse = redshiftClient.createCluster(clusterRequest); System.out.println("Created cluster " + clusterResponse.cluster().clusterIdentifier()); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスCreateCluster」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、CreateTable を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void createTable(RedshiftDataClient redshiftDataClient, String clusterId, String databaseName, String userName) { try { ExecuteStatementRequest createTableRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .dbUser(userName) .database(databaseName) .sql("CREATE TABLE Movies (" + "id INT PRIMARY KEY, " + "title VARCHAR(100), " + "year INT)") .build(); redshiftDataClient.executeStatement(createTableRequest); System.out.println("Table created: Movies"); } catch (RedshiftDataException e) { System.err.println("Error creating table: " + e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスCreateTable」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DeleteCluster を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

クラスターを削除します。

public static void deleteRedshiftCluster(RedshiftClient redshiftClient, String clusterId) { try { DeleteClusterRequest deleteClusterRequest = DeleteClusterRequest.builder() .clusterIdentifier(clusterId) .skipFinalClusterSnapshot(true) .build(); DeleteClusterResponse response = redshiftClient.deleteCluster(deleteClusterRequest); System.out.println("The status is " + response.cluster().clusterStatus()); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDeleteCluster」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DescribeClusters を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

クラスターを記述する

public static void waitForClusterReady(RedshiftClient redshiftClient, String clusterId) { boolean clusterReady = false; String clusterReadyStr; System.out.println("Waiting for cluster to become available. This may take a few mins."); try { DescribeClustersRequest clustersRequest = DescribeClustersRequest.builder() .clusterIdentifier(clusterId) .build(); long startTime = System.currentTimeMillis(); // Loop until the cluster is ready. while (!clusterReady) { DescribeClustersResponse clusterResponse = redshiftClient.describeClusters(clustersRequest); List<Cluster> clusterList = clusterResponse.clusters(); for (Cluster cluster : clusterList) { clusterReadyStr = cluster.clusterStatus(); if (clusterReadyStr.contains("available")) clusterReady = true; else { long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.printf("Elapsed Time: %02d:%02d - Waiting for cluster... %n", minutes, seconds); TimeUnit.SECONDS.sleep(5); } } } long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.println(String.format("Cluster is available! Total Elapsed Time: %02d:%02d", minutes, seconds)); } catch (RedshiftException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDescribeClusters」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、DescribeStatement を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void checkStatement(RedshiftDataClient redshiftDataClient, String sqlId) { try { DescribeStatementRequest statementRequest = DescribeStatementRequest.builder() .id(sqlId) .build(); String status; while (true) { DescribeStatementResponse response = redshiftDataClient.describeStatement(statementRequest); status = response.statusAsString(); System.out.println("..." + status); if (status.compareTo("FAILED") == 0 ) { System.out.println("The Query Failed. Ending program"); System.exit(1); } else if (status.compareTo("FINISHED") == 0) { break; } TimeUnit.SECONDS.sleep(1); } System.out.println("The statement is finished!"); } catch (RedshiftDataException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスDescribeStatement」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、GetStatementResult を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

ステートメントの結果を確認します。

public static void getResults(RedshiftDataClient redshiftDataClient, String statementId) { try { GetStatementResultRequest resultRequest = GetStatementResultRequest.builder() .id(statementId) .build(); // Extract and print the field values using streams. GetStatementResultResponse response = redshiftDataClient.getStatementResult(resultRequest); response.records().stream() .flatMap(List::stream) .map(Field::stringValue) .filter(value -> value != null) .forEach(value -> System.out.println("The Movie title field is " + value)); } catch (RedshiftDataException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスGetStatementResult」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、Insert を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

public static void popTable(RedshiftDataClient redshiftDataClient, String clusterId, String databaseName, String userName, String fileName, int number) throws IOException { JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); Iterator<JsonNode> iter = rootNode.iterator(); ObjectNode currentNode; int t = 0; while (iter.hasNext()) { if (t == number) break; currentNode = (ObjectNode) iter.next(); int year = currentNode.get("year").asInt(); String title = currentNode.get("title").asText(); // Use SqlParameter to avoid SQL injection. List<SqlParameter> parameterList = new ArrayList<>(); String sqlStatement = "INSERT INTO Movies VALUES( :id , :title, :year);"; // Create the parameters. SqlParameter idParam = SqlParameter.builder() .name("id") .value(String.valueOf(t)) .build(); SqlParameter titleParam= SqlParameter.builder() .name("title") .value(title) .build(); SqlParameter yearParam = SqlParameter.builder() .name("year") .value(String.valueOf(year)) .build(); parameterList.add(idParam); parameterList.add(titleParam); parameterList.add(yearParam); try { ExecuteStatementRequest insertStatementRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .sql(sqlStatement) .database(databaseName) .dbUser(userName) .parameters(parameterList) .build(); redshiftDataClient.executeStatement(insertStatementRequest); System.out.println("Inserted: " + title + " (" + year + ")"); t++; } catch (RedshiftDataException e) { System.err.println("Error inserting data: " + e.getMessage()); System.exit(1); } } System.out.println(t + " records were added to the Movies table. "); }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Insert」を参照してください。

次の例は、ModifyCluster を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

クラスターを変更します。

public static void modifyCluster(RedshiftClient redshiftClient, String clusterId) { try { ModifyClusterRequest modifyClusterRequest = ModifyClusterRequest.builder() .clusterIdentifier(clusterId) .preferredMaintenanceWindow("wed:07:30-wed:08:00") .build(); ModifyClusterResponse clusterResponse = redshiftClient.modifyCluster(modifyClusterRequest); System.out.println("The modified cluster was successfully modified and has " + clusterResponse.cluster().preferredMaintenanceWindow() + " as the maintenance window"); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • API の詳細については、「 API リファレンスModifyCluster」の「」を参照してください。 AWS SDK for Java 2.x

次の例は、Query を使用する方法を説明しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

テーブルに対してクエリを実行します。

public static String queryMoviesByYear(RedshiftDataClient redshiftDataClient, String database, String dbUser, int year, String clusterId) { try { String sqlStatement = " SELECT * FROM Movies WHERE year = :year"; SqlParameter yearParam= SqlParameter.builder() .name("year") .value(String.valueOf(year)) .build(); ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .database(database) .dbUser(dbUser) .parameters(yearParam) .sql(sqlStatement) .build(); ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest); return response.id(); } catch (RedshiftDataException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }
  • API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「Query」を参照してください。

シナリオ

次のコード例は、Amazon Redshift のテーブル、項目、クエリを操作する方法を示しています。

SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.redshift.RedshiftClient; import software.amazon.awssdk.services.redshift.model.Cluster; import software.amazon.awssdk.services.redshift.model.CreateClusterRequest; import software.amazon.awssdk.services.redshift.model.CreateClusterResponse; import software.amazon.awssdk.services.redshift.model.DeleteClusterRequest; import software.amazon.awssdk.services.redshift.model.DeleteClusterResponse; import software.amazon.awssdk.services.redshift.model.DescribeClustersRequest; import software.amazon.awssdk.services.redshift.model.DescribeClustersResponse; import software.amazon.awssdk.services.redshift.model.ModifyClusterRequest; import software.amazon.awssdk.services.redshift.model.ModifyClusterResponse; import software.amazon.awssdk.services.redshift.model.RedshiftException; import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient; import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementRequest; import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementResponse; import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementRequest; import software.amazon.awssdk.services.redshiftdata.model.ExecuteStatementResponse; import software.amazon.awssdk.services.redshiftdata.model.Field; import software.amazon.awssdk.services.redshiftdata.model.GetStatementResultRequest; import software.amazon.awssdk.services.redshiftdata.model.GetStatementResultResponse; import software.amazon.awssdk.services.redshiftdata.model.ListDatabasesRequest; import software.amazon.awssdk.services.redshiftdata.model.RedshiftDataException; import software.amazon.awssdk.services.redshiftdata.model.SqlParameter; import software.amazon.awssdk.services.redshiftdata.paginators.ListDatabasesIterable; import com.fasterxml.jackson.core.JsonParser; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.concurrent.TimeUnit; /** * 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 these tasks: * * 1. Prompts the user for a unique cluster ID or use the default value. * 2. Creates a Redshift cluster with the specified or default cluster Id value. * 3. Waits until the Redshift cluster is available for use. * 4. Lists all databases using a pagination API call. * 5. Creates a table named "Movies" with fields ID, title, and year. * 6. Inserts a specified number of records into the "Movies" table by reading the Movies JSON file. * 7. Prompts the user for a movie release year. * 8. Runs a SQL query to retrieve movies released in the specified year. * 9. Modifies the Redshift cluster. * 10. Prompts the user for confirmation to delete the Redshift cluster. * 11. If confirmed, deletes the specified Redshift cluster. */ public class RedshiftScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws Exception { final String usage = """ Usage: <jsonFilePath>\s Where: jsonFilePath - The path to the Movies JSON file (you can locate that file in ../../../resources/sample_files/movies.json) """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String jsonFilePath = args[0]; String userName; String userPassword; String databaseName = "dev" ; Scanner scanner = new Scanner(System.in); Region region = Region.US_EAST_1; RedshiftClient redshiftClient = RedshiftClient.builder() .region(region) .build(); RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder() .region(region) .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon Redshift SDK Getting Started scenario."); System.out.println(""" This Java program demonstrates how to interact with Amazon Redshift by using the AWS SDK for Java (v2).\s Amazon Redshift is a fully managed, petabyte-scale data warehouse service hosted in the cloud. The program's primary functionalities include cluster creation, verification of cluster readiness,\s list databases, table creation, data population within the table, and execution of SQL statements. Furthermore, it demonstrates the process of querying data from the Movie table.\s Upon completion of the program, all AWS resources are cleaned up. """); System.out.println("Lets get started..."); System.out.println("Please enter your user name (default is awsuser)"); String user = scanner.nextLine(); userName = user.isEmpty() ? "awsuser" : user; System.out.println(DASHES); System.out.println("Please enter your user password (default is AwsUser1000)"); String userpass = scanner.nextLine(); userPassword = userpass.isEmpty() ? "AwsUser1000" : userpass; System.out.println(DASHES); System.out.println(DASHES); System.out.println("A Redshift cluster refers to the collection of computing resources and storage that work together to process and analyze large volumes of data."); System.out.println("Enter a cluster id value (default is redshift-cluster-movies): "); String userClusterId = scanner.nextLine(); String clusterId = userClusterId.isEmpty() ? "redshift-cluster-movies" : userClusterId; createCluster(redshiftClient, clusterId, userName, userPassword); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Wait until "+clusterId +" is available."); System.out.print("Press Enter to continue..."); scanner.nextLine(); waitForClusterReady(redshiftClient, clusterId); System.out.println(DASHES); System.out.println(DASHES); String databaseInfo = """ When you created $clusteridD, the dev database is created by default and used in this scenario.\s To create a custom database, you need to have a CREATEDB privilege.\s For more information, see the documentation here: https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_DATABASE.html. """.replace("$clusteridD", clusterId); System.out.println(databaseInfo); System.out.print("Press Enter to continue..."); scanner.nextLine(); System.out.println(DASHES); System.out.println(DASHES); System.out.println("List databases in "+clusterId); System.out.print("Press Enter to continue..."); scanner.nextLine(); listAllDatabases(redshiftDataClient, clusterId, userName, databaseName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Now you will create a table named Movies."); System.out.print("Press Enter to continue..."); scanner.nextLine(); createTable(redshiftDataClient, clusterId, databaseName, userName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Populate the Movies table using the Movies.json file."); System.out.println("Specify the number of records you would like to add to the Movies Table."); System.out.println("Please enter a value between 50 and 200."); int numRecords; do { System.out.print("Enter a value: "); while (!scanner.hasNextInt()) { System.out.println("Invalid input. Please enter a value between 50 and 200."); System.out.print("Enter a year: "); scanner.next(); } numRecords = scanner.nextInt(); } while (numRecords < 50 || numRecords > 200); popTable(redshiftDataClient, clusterId, databaseName, userName, jsonFilePath, numRecords); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Query the Movies table by year. Enter a value between 2012-2014."); int movieYear; do { System.out.print("Enter a year: "); while (!scanner.hasNextInt()) { System.out.println("Invalid input. Please enter a valid year between 2012 and 2014."); System.out.print("Enter a year: "); scanner.next(); } movieYear = scanner.nextInt(); scanner.nextLine(); } while (movieYear < 2012 || movieYear > 2014); String id = queryMoviesByYear(redshiftDataClient, databaseName, userName, movieYear, clusterId); System.out.println("The identifier of the statement is " + id); checkStatement(redshiftDataClient, id); getResults(redshiftDataClient, id); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Now you will modify the Redshift cluster."); System.out.print("Press Enter to continue..."); scanner.nextLine(); modifyCluster(redshiftClient, clusterId); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Would you like to delete the Amazon Redshift cluster? (y/n)"); String delAns = scanner.nextLine().trim(); if (delAns.equalsIgnoreCase("y")) { System.out.println("You selected to delete " +clusterId); System.out.print("Press Enter to continue..."); scanner.nextLine(); deleteRedshiftCluster(redshiftClient, clusterId); } else { System.out.println("The "+clusterId +" was not deleted"); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("This concludes the Amazon Redshift SDK Getting Started scenario."); System.out.println(DASHES); } public static void listAllDatabases(RedshiftDataClient redshiftDataClient, String clusterId, String dbUser, String database) { try { ListDatabasesRequest databasesRequest = ListDatabasesRequest.builder() .clusterIdentifier(clusterId) .dbUser(dbUser) .database(database) .build(); ListDatabasesIterable listDatabasesIterable = redshiftDataClient.listDatabasesPaginator(databasesRequest); listDatabasesIterable.stream() .flatMap(r -> r.databases().stream()) .forEach(db -> System.out .println("The database name is : " + db)); } catch (RedshiftDataException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void deleteRedshiftCluster(RedshiftClient redshiftClient, String clusterId) { try { DeleteClusterRequest deleteClusterRequest = DeleteClusterRequest.builder() .clusterIdentifier(clusterId) .skipFinalClusterSnapshot(true) .build(); DeleteClusterResponse response = redshiftClient.deleteCluster(deleteClusterRequest); System.out.println("The status is " + response.cluster().clusterStatus()); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void popTable(RedshiftDataClient redshiftDataClient, String clusterId, String databaseName, String userName, String fileName, int number) throws IOException { JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); Iterator<JsonNode> iter = rootNode.iterator(); ObjectNode currentNode; int t = 0; while (iter.hasNext()) { if (t == number) break; currentNode = (ObjectNode) iter.next(); int year = currentNode.get("year").asInt(); String title = currentNode.get("title").asText(); // Use SqlParameter to avoid SQL injection. List<SqlParameter> parameterList = new ArrayList<>(); String sqlStatement = "INSERT INTO Movies VALUES( :id , :title, :year);"; // Create the parameters. SqlParameter idParam = SqlParameter.builder() .name("id") .value(String.valueOf(t)) .build(); SqlParameter titleParam= SqlParameter.builder() .name("title") .value(title) .build(); SqlParameter yearParam = SqlParameter.builder() .name("year") .value(String.valueOf(year)) .build(); parameterList.add(idParam); parameterList.add(titleParam); parameterList.add(yearParam); try { ExecuteStatementRequest insertStatementRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .sql(sqlStatement) .database(databaseName) .dbUser(userName) .parameters(parameterList) .build(); redshiftDataClient.executeStatement(insertStatementRequest); System.out.println("Inserted: " + title + " (" + year + ")"); t++; } catch (RedshiftDataException e) { System.err.println("Error inserting data: " + e.getMessage()); System.exit(1); } } System.out.println(t + " records were added to the Movies table. "); } public static void checkStatement(RedshiftDataClient redshiftDataClient, String sqlId) { try { DescribeStatementRequest statementRequest = DescribeStatementRequest.builder() .id(sqlId) .build(); String status; while (true) { DescribeStatementResponse response = redshiftDataClient.describeStatement(statementRequest); status = response.statusAsString(); System.out.println("..." + status); if (status.compareTo("FAILED") == 0 ) { System.out.println("The Query Failed. Ending program"); System.exit(1); } else if (status.compareTo("FINISHED") == 0) { break; } TimeUnit.SECONDS.sleep(1); } System.out.println("The statement is finished!"); } catch (RedshiftDataException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void modifyCluster(RedshiftClient redshiftClient, String clusterId) { try { ModifyClusterRequest modifyClusterRequest = ModifyClusterRequest.builder() .clusterIdentifier(clusterId) .preferredMaintenanceWindow("wed:07:30-wed:08:00") .build(); ModifyClusterResponse clusterResponse = redshiftClient.modifyCluster(modifyClusterRequest); System.out.println("The modified cluster was successfully modified and has " + clusterResponse.cluster().preferredMaintenanceWindow() + " as the maintenance window"); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String queryMoviesByYear(RedshiftDataClient redshiftDataClient, String database, String dbUser, int year, String clusterId) { try { String sqlStatement = " SELECT * FROM Movies WHERE year = :year"; SqlParameter yearParam= SqlParameter.builder() .name("year") .value(String.valueOf(year)) .build(); ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .database(database) .dbUser(dbUser) .parameters(yearParam) .sql(sqlStatement) .build(); ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest); return response.id(); } catch (RedshiftDataException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } public static void getResults(RedshiftDataClient redshiftDataClient, String statementId) { try { GetStatementResultRequest resultRequest = GetStatementResultRequest.builder() .id(statementId) .build(); // Extract and print the field values using streams. GetStatementResultResponse response = redshiftDataClient.getStatementResult(resultRequest); response.records().stream() .flatMap(List::stream) .map(Field::stringValue) .filter(value -> value != null) .forEach(value -> System.out.println("The Movie title field is " + value)); } catch (RedshiftDataException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void waitForClusterReady(RedshiftClient redshiftClient, String clusterId) { boolean clusterReady = false; String clusterReadyStr; System.out.println("Waiting for cluster to become available. This may take a few mins."); try { DescribeClustersRequest clustersRequest = DescribeClustersRequest.builder() .clusterIdentifier(clusterId) .build(); long startTime = System.currentTimeMillis(); // Loop until the cluster is ready. while (!clusterReady) { DescribeClustersResponse clusterResponse = redshiftClient.describeClusters(clustersRequest); List<Cluster> clusterList = clusterResponse.clusters(); for (Cluster cluster : clusterList) { clusterReadyStr = cluster.clusterStatus(); if (clusterReadyStr.contains("available")) clusterReady = true; else { long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.printf("Elapsed Time: %02d:%02d - Waiting for cluster... %n", minutes, seconds); TimeUnit.SECONDS.sleep(5); } } } long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.println(String.format("Cluster is available! Total Elapsed Time: %02d:%02d", minutes, seconds)); } catch (RedshiftException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void createTable(RedshiftDataClient redshiftDataClient, String clusterId, String databaseName, String userName) { try { ExecuteStatementRequest createTableRequest = ExecuteStatementRequest.builder() .clusterIdentifier(clusterId) .dbUser(userName) .database(databaseName) .sql("CREATE TABLE Movies (" + "id INT PRIMARY KEY, " + "title VARCHAR(100), " + "year INT)") .build(); redshiftDataClient.executeStatement(createTableRequest); System.out.println("Table created: Movies"); } catch (RedshiftDataException e) { System.err.println("Error creating table: " + e.getMessage()); System.exit(1); } } public static void createCluster(RedshiftClient redshiftClient, String clusterId, String masterUsername, String masterUserPassword) { try { CreateClusterRequest clusterRequest = CreateClusterRequest.builder() .clusterIdentifier(clusterId) .masterUsername(masterUsername) .masterUserPassword(masterUserPassword) .nodeType("ra3.4xlarge") .publiclyAccessible(true) .numberOfNodes(2) .build(); CreateClusterResponse clusterResponse = redshiftClient.createCluster(clusterRequest); System.out.println("Created cluster " + clusterResponse.cluster().clusterIdentifier()); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } } }