Amazon Redshift
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门


此部分介绍了如何以编程方式连接到集群。如果您使用的应用程序(如 SQL Workbench/J)能够为您管理客户端连接,则可跳过这一部分。

使用 Java 连接到集群

当您使用 Java 来以编程方式连接到集群时,不管是否使用服务器身份验证均可实现。如果您计划使用服务器身份验证,请按照配置连接的安全选项中的说明将 Amazon Redshift 服务器证书放入密钥存储库中。您可以通过在运行代码时指定属性来使用 keystore,如下所示:<path to keystore><keystore password>

例 :使用 Java 连接到集群



props.setProperty("ssl", "true");


有关运行以下示例的分步说明,请参阅 使用 Eclipse 运行 Amazon Redshift 的 Java 示例

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ // snippet-sourcedescription:[ConnectToClusterExample demonstrates how to connect to an Amazon Redshift cluster and run a sample query.] // snippet-service:[redshift] // snippet-keyword:[Java] // snippet-keyword:[Amazon Redshift] // snippet-keyword:[Code Sample] // snippet-keyword:[Connect] // snippet-keyword:[JDBC] // snippet-sourcetype:[full-example] // snippet-sourcedate:[2019-02-01] // snippet-sourceauthor:[AWS] // snippet-start:[] package connection; import java.sql.*; import java.util.Properties; public class ConnectToCluster { //Redshift driver: "jdbc:redshift://"; static final String dbURL = "***jdbc cluster connection string ****"; static final String MasterUsername = "***master user name***"; static final String MasterUserPassword = "***master user password***"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //Dynamically load driver at runtime. //Redshift JDBC 4.1 driver: //Redshift JDBC 4 driver: Class.forName(""); //Open a connection and define properties. System.out.println("Connecting to database..."); Properties props = new Properties(); //Uncomment the following line if using a keystore. //props.setProperty("ssl", "true"); props.setProperty("user", MasterUsername); props.setProperty("password", MasterUserPassword); conn = DriverManager.getConnection(dbURL, props); //Try a simple query. System.out.println("Listing system tables..."); stmt = conn.createStatement(); String sql; sql = "select * from information_schema.tables;"; ResultSet rs = stmt.executeQuery(sql); //Get the data from the result set. while({ //Retrieve two columns. String catalog = rs.getString("table_catalog"); String name = rs.getString("table_name"); //Display values. System.out.print("Catalog: " + catalog); System.out.println(", Name: " + name); } rs.close(); stmt.close(); conn.close(); }catch(Exception ex){ //For convenience, handle all errors here. ex.printStackTrace(); }finally{ //Finally block to close resources. try{ if(stmt!=null) stmt.close(); }catch(Exception ex){ }// nothing we can do try{ if(conn!=null) conn.close(); }catch(Exception ex){ ex.printStackTrace(); } } System.out.println("Finished connectivity test."); } } // snippet-end:[]

使用 .NET 连接到集群

当您使用 .NET (C#) 以编程方式连接到集群时,不管是否使用服务器身份验证均可实现。如果您计划使用服务器身份验证,请按照配置连接的安全选项中的说明下载 Amazon Redshift 服务器证书,然后将证书转换成格式正确的 .NET 代码。

例 使用 .NET 连接到集群

以下示例可连接到集群,并会运行可返回系统表的示例查询。它不会显示服务器身份验证。即使您的数据库中没有数据也可以使用此示例。此示例使用 System.Data.Odbc Namespace,它是面向 ODBC 的 .NET Framework 数据提供商。

using System; using System.Data; using System.Data.Odbc; namespace { class ConnectToClusterExample { public static void Main(string[] args) { DataSet ds = new DataSet(); DataTable dt = new DataTable(); // Server, e.g. "" string server = "***provide server name part of connection string****"; // Port, e.g. "5439" string port = "***provide port***"; // MasterUserName, e.g. "masteruser". string masterUsername = "***provide master user name***"; // MasterUserPassword, e.g. "mypassword". string masterUserPassword = "***provide master user password***"; // DBName, e.g. "dev" string DBName = "***provide name of database***"; string query = "select * from information_schema.tables;"; try { // Create the ODBC connection string. //Redshift ODBC Driver - 64 bits /* string connString = "Driver={Amazon Redshift (x64)};" + String.Format("Server={0};Database={1};" + "UID={2};PWD={3};Port={4};SSL=true;Sslmode=Require", server, DBName, masterUsername, masterUserPassword, port); */ //Redshift ODBC Driver - 32 bits string connString = "Driver={Amazon Redshift (x86)};" + String.Format("Server={0};Database={1};" + "UID={2};PWD={3};Port={4};SSL=true;Sslmode=Require", server, DBName, masterUsername, masterUserPassword, port); // Make a connection using the psqlODBC provider. OdbcConnection conn = new OdbcConnection(connString); conn.Open(); // Try a simple query. string sql = query; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); da.Fill(ds); dt = ds.Tables[0]; foreach (DataRow row in dt.Rows) { Console.WriteLine(row["table_catalog"] + ", " + row["table_name"]); } conn.Close(); Console.ReadKey(); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } } } }