简体   繁体   English

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

[英]AWS Redshift serverless - how to get the cluster id value

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

EDIT: [![enter image description here][2]][2]编辑:[![在此处输入图片描述][2]][2]

This is the screen of a serverless endpoint dashboard.这是无服务器端点仪表板的屏幕。 There is no cluster ID.没有集群 ID。 [1]: https://docs.aws.amazon.com/redshift/latest/mgmt/generating-user-credentials.html [2]: https://i.stack.imgur.com/VzvIs.png [1]: https://docs.aws.amazon.com/redshift/latest/mgmt/generating-user-credentials.html [2]: https://i.stack.imgur.com/VzvIs.png

Look at this Guide (a newer one) that talks about Connecting to Amazon Redshift Serverless.查看本指南(较新的指南),其中讨论了连接到 Amazon Redshift Serverless。 https://docs.aws.amazon.com/redshift/latest/mgmt/serverless-connecting.html 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.使用数据API连接到无服务器端点您还可以使用 Amazon Redshift 数据 API 连接到无服务器端点。 Leave off the cluster-identifier parameter in your AWS CLI calls to route your query to serverless endpoint.在您的 AWS CLI 调用中保留 cluster-identifier 参数以将您的查询路由到无服务器终端节点。

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 开始使用 Amazon Redshift Serverless

I loaded sample data and now have this.我加载了样本数据,现在有了这个。

在此处输入图像描述

Now I attemped to connect to it using software.amazon.awssdk.services.redshiftdata.RedshiftDataClient .现在我尝试使用software.amazon.awssdk.services.redshiftdata.RedshiftDataClient连接到它。

The Java V2 code: Java V2代码:

 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.请注意,没有集群 ID 或用户。 Only a database name ( sample_data_dev ).只有一个数据库名称 ( sample_data_dev )。 The call worked perfectly.通话效果很好。

在此处输入图像描述

HEre is the full code example that successfully queries data from a serverless instance using the AWS SDK for Java V2.这是完整的代码示例,它使用 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS 如何对 Redshift Spectrum 集群收费? - How does AWS charge the Redshift Spectrum cluster? 如何使用 aws cdk 代码将 iam 角色附加到现有的 redshift 集群 - How to attach iam role to existing redshift cluster using aws cdk code AWS + 无服务器 - 如何获取 Cognito 用户池生成的密钥 - AWS + Serverless - how to get at the secret key generated by cognito user pool 如何使用 AWS 的无服务器框架获取最新的 Layer 版本 Lambda - How to get latest Layer version with serverless framework for AWS Lambda 以编程方式使用 AWS CLI 确定是否删除了 Amazon Redshift 集群 - Programmatically using the AWS CLI to determine whether an Amazon Redshift cluster is deleted AWS::Serverless::Api - 缓存集群设置缓存生存时间 - AWS::Serverless::Api - Cache Cluster set cache time to live AWS CDK:将 Redshift 集群放入现有的 su.net 组 - AWS CDK: Put Redshift cluster into existing subnet group 如何将所有数据从一个红移集群复制到另一个红移集群 - How to copy all the data from one redshift cluster to another redshift cluster 如何在 AWS 无服务器 Function 上设置 Oauth 范围? - How to setup Oauth scopes at AWS Serverless Function? 如何在 aws serverless 上安排按需任务 - How to schedule on-demand tasks on aws serverless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM