Home > database >  AWS Redshift serverless - how to get the cluster id value
AWS Redshift serverless - how to get the cluster id value

Time:03-25

I'm following the AWS documentation about how to connect to redshift [generating user credentials][1] But the get-cluster-credentials API requires a cluster id parameter, which i don't have for a serverless endpoint. What id should I use?

EDIT: [![enter image description here][2]][2]

This is the screen of a serverless endpoint dashboard. There is no cluster ID. [1]: 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