Menu
Amazon Redshift
Management Guide (API Version 2012-12-01)

Using the AWS SDK for Java with Amazon Redshift

The AWS SDK for Java provides a class named AmazonRedshiftClient, which you can use to interact with Amazon Redshift. For information about downloading the AWS SDK for Java, go to AWS SDK for Java.

Note

The AWS SDK for Java provides thread-safe clients for accessing Amazon Redshift. As a best practice, your applications should create one client and reuse the client between threads.

The AmazonRedshiftClient class defines methods that map to underlying Amazon Redshift Query API actions. (These actions are described in the Amazon Redshift API Reference). When you call a method, you must create a corresponding request object and response object. The request object includes information that you must pass with the actual request. The response object include information returned from Amazon Redshift in response to the request.

For example, the AmazonRedshiftClient class provides the createCluster method to provision a cluster. This method maps to the underlying CreateCluster API action. You create a CreateClusterRequest object to pass information with the createCluster method.

Copy
AmazonRedshiftClient client = new AmazonRedshiftClient(credentials); client.setEndpoint("https://redshift.us-east-1.amazonaws.com/"); CreateClusterRequest request = new CreateClusterRequest() .withClusterIdentifier("exampleclusterusingjava") .withMasterUsername("masteruser") .withMasterUserPassword("12345678Aa") .withNodeType("ds1.xlarge") .withNumberOfNodes(2); Cluster createResponse = client.createCluster(request); System.out.println("Created cluster " + createResponse.getClusterIdentifier());

Running Java Examples for Amazon Redshift Using Eclipse

General Process of Running Java Code Examples Using Eclipse

  1. Create a new AWS Java Project in Eclipse.

    Follow the steps in Setting Up the AWS Toolkit for Eclipse in the AWS Toolkit for Eclipse Getting Started Guide.

  2. Copy the sample code from the section of this document that you are reading and paste it into your project as a new Java class file.

  3. Run the code.

Running Java Examples for Amazon Redshift from the Command Line

General Process of Running Java Code Examples from the Command Line

  1. Set up and test your environment as follows:

    1. Create a directory to work in and in it create src, bin, and sdk subfolders.

    2. Download the AWS SDK for Java and unzip it to the sdk subfolder you created. After you unzip the SDK, you should have four subdirectories in the sdk folder, including a lib and third-party folder.

    3. Supply your AWS credentials to the SDK for Java. For more information, go to Providing AWS Credentials in the AWS SDK for Java in the AWS SDK for Java Developer Guide.

    4. Ensure that you can run the Java program compiler (javac) and the Java application launcher (java) from your working directory. You can test by running the following commands:

      Copy
      javac -help java -help
  2. Put the code that you want to run in a .java file, and save the file in the src folder. To illustrate the process, we use the code from Managing Cluster Security Groups Using the AWS SDK for Java so that the file in the src directory is CreateAndModifyClusterSecurityGroup.java.

  3. Compile the code.

    Copy
    javac -cp sdk/lib/aws-java-sdk-1.3.18.jar -d bin src\CreateAndModifyClusterSecurityGroup.java

    If you are using a different version of the AWS SDK for Java, adjust the classpath (-cp) for your version.

  4. Run the code. In the following command, line breaks are added for readability.

    Copy
    java -cp "bin; sdk/lib/*; sdk/third-party/commons-logging-1.1.1/*; sdk/third-party/httpcomponents-client-4.1.1/*; sdk/third-party/jackson-core-1.8/*" CreateAndModifyClusterSecurityGroup

    Change the class path separator as needed for your operating system. For example, for Windows, the separator is ";" (as shown), and for Unix, it is ":". Other code examples may require more libraries than are shown in this example, or the version of the AWS SDK you are working with may have different third-party folder names. For these cases, adjust the classpath (-cp) as appropriate.

    To run samples in this document, use a version of the AWS SDK that supports Amazon Redshift. To get the latest version of the AWS SDK for Java, go to AWS SDK for Java.

Setting the Endpoint

By default, the AWS SDK for Java uses the endpoint https://redshift.us-east-1.amazonaws.com/. You can set the endpoint explicitly with the client.setEndpoint method as shown in the following Java code snippet.

Copy
client = new AmazonRedshiftClient(credentials); client.setEndpoint("https://redshift.us-east-1.amazonaws.com/");

For a list of supported AWS regions where you can provision a cluster, go to the Regions and Endpoints section in the Amazon Web Services Glossary.