使用JDBC连接AWS Redshift

时间:2017-05-06 16:27:28

标签: java amazon-web-services jdbc

我想使用JDBC连接数据并将数据插入到我的Amazon Redshift表中。我编写了以下代码,但在Class.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>

2 个答案:

答案 0 :(得分:6)

  1. 首先在pom.xml中添加资源库,
  2. <repositories>
      <repository>
          <id>redshift</id>
          <url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
      </repository>
    </repositories>

    1. 现在添加Amazon redshift Driver jar maven dependeny
    2. <dependency>
        <groupId>com.amazon.redshift</groupId>
        <artifactId>redshift-jdbc42</artifactId>
        <version>1.2.1.1001</version>
      </dependency>  

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

      清理构建项目,现在应该可以使用。

答案 1 :(得分:2)

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