How to create a Redshift Cluster using Java SDK

+1 vote
Can someone help me with the code to create Redshift cluster using Java SDK?
Feb 25, 2019 in AWS by Tushar
1,398 views

1 answer to this question.

+1 vote

This is the code to create a Redshift Cluster.

import java.io.IOException;

import com.amazonaws.services.redshift.AmazonRedshift;
import com.amazonaws.services.redshift.AmazonRedshiftClientBuilder;
import com.amazonaws.services.redshift.model.*;

public class CreateAndModifyCluster {

    public static AmazonRedshift client;
    public static String clusterIdentifier = "democluster";
    public static long sleepTime = 20;

    public static void main(String[] args) throws IOException {

        // Default client using the {@link com.amazonaws.auth.DefaultAWSCredentialsProviderChain}
        client = AmazonRedshiftClientBuilder.defaultClient();

        try {
            createCluster();
            waitForClusterReady();
            describeClusters();
            modifyCluster();
            describeClusters();
        } catch (Exception e) {
            System.err.println("Operation failed: " + e.getMessage());
        }
    }

    private static void createCluster() {
        CreateClusterRequest request = new CreateClusterRequest()
                .withClusterIdentifier(clusterIdentifier)
                .withMasterUsername("masteruser")
                .withMasterUserPassword("password123")
                .withNodeType("dc2.large")
                .withNumberOfNodes(2)
                .withClusterSubnetGroupName("priyajclustersubnet");

        Cluster createResponse = client.createCluster(request);
        System.out.println("Created cluster " + createResponse.getClusterIdentifier());
    }

    private static void describeClusters() {
        DescribeClustersRequest request = new DescribeClustersRequest()
                .withClusterIdentifier(clusterIdentifier);

        DescribeClustersResult result = client.describeClusters(request);
        printResult(result);
    }

    private static void modifyCluster() {
        ModifyClusterRequest request = new ModifyClusterRequest()
                .withClusterIdentifier(clusterIdentifier)
                .withPreferredMaintenanceWindow("sat:07:30-sun:08:00");

        client.modifyCluster(request);
        System.out.println("Modified cluster " + clusterIdentifier);

    }

    private static void printResult(DescribeClustersResult result)
    {
        if (result == null)
        {
            System.out.println("Describe clusters result is null.");
            return;
        }

        System.out.println("Cluster property:");
        System.out.format("Preferred Maintenance Window: %s\n", result.getClusters().get(0).getPreferredMaintenanceWindow());
    }

    private static void waitForClusterReady() throws InterruptedException {
        Boolean clusterReady = false;
        System.out.println("Wating for cluster to become available.");
        while (!clusterReady) {
            DescribeClustersResult result = client.describeClusters(new DescribeClustersRequest()
                                                                            .withClusterIdentifier(clusterIdentifier));
            String status = (result.getClusters()).get(0).getClusterStatus();
            if (status.equalsIgnoreCase("available")) {
                clusterReady = true;
            }
            else {
                System.out.print(".");
                Thread.sleep(sleepTime*1000);
            }
        }
    }
}

Hope this helps.

answered Feb 25, 2019 by Priyaj
• 58,100 points

Related Questions In AWS

0 votes
1 answer

How to create a DynamoDB table using Java SDK?

The code to create a table in ...READ MORE

answered Feb 23, 2019 in AWS by Priyaj
• 58,100 points
2,458 views
0 votes
1 answer

How to create a RedShift Cluster using command line?

Hi@akhtar, To create a cluster in Virtual Private ...READ MORE

answered Nov 26, 2020 in AWS by MD
• 95,460 points
943 views
0 votes
1 answer

How to create a Cluster Security Group in RedShift?

Hi@akhtar, You can create a new Amazon AWS Redshift ...READ MORE

answered Nov 27, 2020 in AWS by MD
• 95,460 points
923 views
0 votes
1 answer

How to create a Cluster Snapshot in AWS RedShift?

Hi@akhtar, You can create a manual snapshot of the ...READ MORE

answered Nov 27, 2020 in AWS by MD
• 95,460 points
751 views
+1 vote
1 answer

How to create a redshift cluster?

You can create a Redshift Cluster using ...READ MORE

answered Feb 25, 2019 in AWS by Priyaj
• 58,100 points
831 views
0 votes
1 answer

How to create a redshift cluster using AWS CLI?

You can create your cluster using AWS ...READ MORE

answered Feb 25, 2019 in AWS by Priyaj
• 58,100 points
1,195 views
0 votes
1 answer
0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

The Java code for creating an AWS ...READ MORE

answered Feb 27, 2019 in AWS by Priyaj
• 58,100 points
1,479 views
0 votes
1 answer

How to create a VPC using Java SDK?

You can create a VPC using the ...READ MORE

answered Feb 21, 2019 in AWS by Priyaj
• 58,100 points
2,654 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP