Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Amazon Software License (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://aws.amazon.com/asl/
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.util.List;

import com.amazonaws.services.redshift.AmazonRedshiftClient;
import com.amazonaws.services.redshift.model.Cluster;

import com.amazonaws.services.redshift.model.DescribeClustersRequest;
import com.amazonaws.services.redshift.model.DescribeClustersResult;
import com.amazonaws.services.redshift.model.Endpoint;

public class Main {
    /**
     * Gets the JDBC URL associated with an active Redshift cluster.
     * 
     * @param client
     *            The {@link AmazonRedshiftClient} with read permissions
     * @param clusterIdentifier
     *            The unique Redshift cluster identifier
     * @return JDBC URL for the Redshift cluster
     */
    public static String getClusterURL(AmazonRedshiftClient client, String clusterIdentifier) {
        DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest();
        describeClustersRequest.setClusterIdentifier(clusterIdentifier);
        DescribeClustersResult describeClustersResult = client.describeClusters(describeClustersRequest);
        List<Cluster> clusters = describeClustersResult.getClusters();
        if (!clusters.isEmpty()) {
            return toJDBC(clusters.get(0).getEndpoint(), clusters.get(0).getDBName());
        }
        return null;
    }

    /**
     * Helper method to convert a Redshift {@link Endpoint} and database name to JDBC connection
     * String
     * 
     * @param endpoint
     *            The Redshit Endpoint to convert to connection String
     * @param databaseName
     *            The database name for the Redshift cluster
     * @return The JDBC connection String associated with the Endpoint and database name
     */
    private static String toJDBC(Endpoint endpoint, String databaseName) {
        StringBuilder jdbc = new StringBuilder();
        jdbc.append("jdbc:postgresql://");
        jdbc.append(endpoint.getAddress());
        jdbc.append(":" + endpoint.getPort());
        jdbc.append("/" + databaseName);
        return jdbc.toString();
    }
}