我目前在 S3 中有一个文件。我想使用 Java AWS SDK 发出命令,以获取这些数据并将其放入 RedShift 表中。如果表不存在,我也想创建表。我一直找不到任何明确的例子来说明如何做到这一点,所以我想知道我是不是走错了路?我应该使用标准 postgres java 连接器而不是 AWS 开发工具包吗?
问问题
9133 次
2 回答
10
连接(http://docs.aws.amazon.com/redshift/latest/mgmt/connecting-in-code.html#connecting-in-code-java)并提交您的 CREATE TABLE 和 COPY 命令
于 2013-07-19T10:55:28.033 回答
1
伙计们的回答大部分都是为了目的。
我想发布一个工作 java JDBC 代码,它完全从 S3 复制到 Redshift 表。我希望它会帮助别人。
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();
}
}
}
于 2018-08-18T11:19:53.267 回答