AWS Redshift serverless - how to get the cluster id value

Viewed 1016
1 Answers

Look at this Guide (a newer one) that talks about Connecting to Amazon Redshift Serverless. https://docs.aws.amazon.com/redshift/latest/mgmt/serverless-connecting.html

See this information that answers your question:

Connecting to the serverless endpoint with the Data API You can also use the Amazon Redshift Data API to connect to serverless endpoint. Leave off the cluster-identifier parameter in your AWS CLI calls to route your query to serverless endpoint.

UPDATE

I wanted to test this to make sure that a successful connection can be made. I followed this doc to setup a Serverless instance.

Get started with Amazon Redshift Serverless

I loaded sample data and now have this.

enter image description here

Now I attemped to connect to it using software.amazon.awssdk.services.redshiftdata.RedshiftDataClient.

The Java V2 code:

 try {
            ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder()
                    .database(database)
                    .sql(sqlStatement)
                    .build();

            ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest);
            return response.id();

        } catch (RedshiftDataException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

Notice there is no cluster id or user. Only a database name (sample_data_dev). The call worked perfectly.

enter image description here

HEre is the full code example that successfully queries data from a serverless instance using the AWS SDK for Java V2.

package com.example.redshiftdata;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.redshiftdata.model.*;
import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementRequest;
import java.util.List;


/**
 * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class RetrieveDataServerless {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    RetrieveData <database> <sqlStatement>  \n\n" +
                "Where:\n" +
                "    database - the name of the database (for example, sample_data_dev). \n" +
                "    sqlStatement - the sql statement to use. \n"  ;

        String database = "sample_data_dev"  ;  
        String sqlStatement =  "Select * from tickit.sales" ; 
        Region region = Region.US_WEST_2;
        RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder()
                .region(region)
                .build();

        String id =  performSQLStatement(redshiftDataClient, database, sqlStatement);
        System.out.println("The identifier of the statement is "+id);
        checkStatement(redshiftDataClient,id );
        getResults(redshiftDataClient, id);
        redshiftDataClient.close();
    }

    public static void checkStatement(RedshiftDataClient redshiftDataClient,String sqlId ) {

        try {

            DescribeStatementRequest statementRequest = DescribeStatementRequest.builder()
                    .id(sqlId)
                    .build() ;

            // Wait until the sql statement processing is finished.
            boolean finished = false;
            String status = "";
            while (!finished) {

                DescribeStatementResponse response = redshiftDataClient.describeStatement(statementRequest);
                status = response.statusAsString();
                System.out.println("..."+status);

                if (status.compareTo("FINISHED") == 0) {
                    break;
                }
                Thread.sleep(1000);
            }

            System.out.println("The statement is finished!");

        } catch (RedshiftDataException | InterruptedException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }

    public static String performSQLStatement(RedshiftDataClient redshiftDataClient,
                                             String database,
                                             String sqlStatement) {

        try {
            ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder()
                    .database(database)
                    .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();

            GetStatementResultResponse response = redshiftDataClient.getStatementResult(resultRequest);

            // Iterate through the List element where each element is a List object.
            List<List<Field>> dataList = response.records();

            // Print out the records.
            for (List list: dataList) {

                for (Object myField:list) {

                    Field field = (Field) myField;
                    String value = field.stringValue();
                    if (value != null)
                        System.out.println("The value of the field is " + value);
                }
            }

        } catch (RedshiftDataException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
}
Related