使用JDBC与AWS RedShift连接



我想使用JDBC将数据连接到我的Amazon Redshift表中。我编写了以下代码,但在行类别上遇到错误。Forname

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class RedShiftDataEmitter {
    static final String redshiftUrl = "jdbc:redshift://xxxxxxxxx:5439/xxxxxx";
    static final String masterUsername = "xxxxxxx";
    static final String password = "xxxxxxx";
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.amazon.redshift.jdbc41.Driver");
            Properties properties = new Properties();
            properties.setProperty("user", masterUsername);
            properties.setProperty("password", password);
            connection = DriverManager.getConnection(redshiftUrl, properties);
            // Further code to follow
        } catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
    }
}

只是一个头,我可以使用SQL Workbench连接到同一集群。我的pom.xml如下

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-redshift -->
  <dependency>
      <groupId>com.amazon.redshift</groupId>
      <artifactId>redshift-jdbc41</artifactId>
      <version>1.2.1.1001</version>
  </dependency>
  1. 首先在pom.xml中添加存储库,

<repositories>
  <repository>
      <id>redshift</id>
      <url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
  </repository>
</repositories>

  1. 现在添加Amazon Redshift驱动程序Jar Maven dependeny

<dependency>
  <groupId>com.amazon.redshift</groupId>
  <artifactId>redshift-jdbc42</artifactId>
  <version>1.2.1.1001</version>
</dependency>  

使用此驱动程序 com.amazon.redshift.jdbc42.driver

清洁构建该项目现在应该工作。

我找到了答案,问题是Maven无法识别红移依赖性。您将必须手动下载并添加罐子(外部罐子(。

最新更新