AWS Redshift 无服务器 - 如何获取集群 ID 值

AWS Redshift serverless - how to get the cluster id value

我正在关注有关如何连接到 redshift [生成用户凭证][1] 的 AWS 文档 但是 get-cluster-credentials API 需要一个集群 ID 参数,我没有用于无服务器端点的参数。我应该使用什么 ID?

编辑: [![在此处输入图片描述][2]][2]

这是无服务器端点仪表板的屏幕。没有集群 ID。 [1]: https://docs.aws.amazon.com/redshift/latest/mgmt/generating-user-credentials.html [2]: https://i.stack.imgur.com/VzvIs.png

查看本指南(较新的指南),其中讨论了如何连接到 Amazon Redshift Serverless。 https://docs.aws.amazon.com/redshift/latest/mgmt/serverless-connecting.html

查看此信息可以回答您的问题:

使用数据连接到无服务器端点API 您还可以使用 Amazon Redshift Data API 连接到无服务器端点。在您的 AWS CLI 调用中保留 cluster-identifier 参数以将您的查询路由到无服务器端点。

更新

我想对此进行测试以确保可以成功建立连接。我按照此文档设置了无服务器实例。

Get started with Amazon Redshift Serverless

我加载了示例数据,现在有了这个。

现在我尝试使用 software.amazon.awssdk.services.redshiftdata.RedshiftDataClient 连接到它。

JavaV2代码:

 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 "";
    }

请注意,没有集群 ID 或用户。只有一个数据库名称 (sample_data_dev)。通话完美。

这里是使用 Java V2 的 AWS SDK 从无服务器实例成功查询数据的完整代码示例。

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);
        }
    }
}