Amazon Redshift:使用Java API将数据从S3插入表中

时间:2013-07-17 14:34:12

标签: amazon-web-services amazon-s3 amazon-redshift

我目前在S3中有一个文件。我想使用Java AWS SDK发出命令,以获取此数据并将其放入RedShift表中。如果表不存在,我还想创建表。我一直无法找到关于如何做到这一点的任何明确的例子,所以我想知道我是否采取了错误的方式?我应该使用标准的postgres java连接器而不是AWS SDK吗?

2 个答案:

答案 0 :(得分:9)

答案 1 :(得分:0)

大多数人的答案都是有目的的。

我想发布一个能正常执行从S3复制到Redshift表的Java JDBC代码。希望对其他人有帮助。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

public class RedShiftJDBC {
    public static void main(String[] args) {

        Connection conn = null;
        Statement statement = null;
        try {
            //Even postgresql driver will work too. You need to make sure to choose postgresql url instead of redshift.
            //Class.forName("org.postgresql.Driver");
            //Make sure to choose appropriate Redshift Jdbc driver and its jar in classpath
            Class.forName("com.amazon.redshift.jdbc42.Driver");
            Properties props = new Properties();
            props.setProperty("user", "username***");
            props.setProperty("password", "password****");

            System.out.println("\n\nconnecting to database...\n\n");
            //In case you are using postgreSQL jdbc driver.
            //conn = DriverManager.getConnection("jdbc:postgresql://********8-your-to-redshift.redshift.amazonaws.com:5439/example-database", props);

            conn = DriverManager.getConnection("jdbc:redshift://********url-to-redshift.redshift.amazonaws.com:5439/example-database", props);

            System.out.println("\n\nConnection made!\n\n");

            statement = conn.createStatement();

            String command = "COPY my_table from 's3://path/to/csv/example.csv' CREDENTIALS 'aws_access_key_id=******;aws_secret_access_key=********' CSV DELIMITER ',' ignoreheader 1";

            System.out.println("\n\nExecuting...\n\n");

            statement.executeUpdate(command);
            //you must need to commit, if you realy want to have data saved, otherwise it will not appear if you query from other session.
            conn.commit();
            System.out.println("\n\nThats all copy using simple JDBC.\n\n");
            statement.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}