AWS Redshift 采坑记

2021-12-27 00:00:00 专区 订阅 付费 是一个 配置

1.不要使用快速启动集群的方式建立,否则vpc是一个巨坑

 

2.要配置对应的Role 并配置化 role arn

3..net 连接类

using Amazon.Redshift;
using Amazon.Redshift.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Text;

namespace eXconsole
{
     public  class redshiftdb
    {
        public void TestConnectivityOnMasterNode(string clusterName)
        {
            using (IAmazonRedshift redshiftClient = new AmazonRedshiftClient())
            {
                try
                {
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest() { ClusterIdentifier = clusterName };
                    DescribeClustersResponse describeClustersResponse = redshiftClient.DescribeClustersAsync().GetAwaiter().GetResult();
                    Cluster firstMatch = describeClustersResponse.Clusters[];

                    String mainDbName = firstMatch.DBName;
                    String endpointAddress = firstMatch.Endpoint.Address;
                    int endpointPort = firstMatch.Endpoint.Port;
                    string masterUsername = firstMatch.MasterUsername;
                    string password = "your master password";


                    string odbcConnectionString = string.Concat("Driver={PostgreSQL Unicode}; Server=", endpointAddress
                        , "; Database=", mainDbName, "; UID=", masterUsername, "; PWD=", password
                        , "; Port=", endpointPort);
                    string odbcConnectionDsn = "DSN=UrlsRedShiftDb";
                    string query = "SELECT datname FROM pg_database WHERE datistemplate = false;";

                    using (OdbcConnection conn = new OdbcConnection(odbcConnectionString))
                    {
                        try
                        {
                            conn.Open();
                            OdbcDataAdapter da = new OdbcDataAdapter(query, conn);
                            da.Fill(ds);
                            dt = ds.Tables[];
                            foreach (DataRow row in dt.Rows)
                            {
                                Console.WriteLine(row["datname"]);
                            }

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception caught while communicating with RedShift master node: ");
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (AmazonRedshiftException e)
                {
                    Console.WriteLine("Postgresql command execution on master node has failed.");
                    Console.WriteLine("Amazon error code: {0}",
                        string.IsNullOrEmpty(e.ErrorCode) ? "None" : e.ErrorCode);
                    Console.WriteLine("Exception message: {0}", e.Message);
                }
            }
        }
    }
}

相关文章