【问题标题】:Amazon Redshift: Insert data into table from S3 using Java APIAmazon Redshift:使用 Java API 从 S3 将数据插入表中
【发布时间】:2013-07-16 04:13:19
【问题描述】:

我目前在 S3 中有一个文件。我想使用 Java AWS SDK 发出命令,以获取这些数据并将其放入 RedShift 表中。如果表不存在,我也想创建表。我一直找不到任何明确的例子来说明如何做到这一点,所以我想知道我是不是走错了路?我应该使用标准 postgres java 连接器而不是 AWS 开发工具包吗?

【问题讨论】:

  • 连接 (docs.aws.amazon.com/redshift/latest/mgmt/…) 并提交您的 CREATE TABLE 和 COPY 命令
  • 你设法让它工作了吗?你有任何博客文章或与如何完成相关的任何内容吗?交易
  • 正确的方法是使用jdbc驱动,把redshift当作psql数据库。这是我为 ruby​​ 程序员发布的示例。 stackoverflow.com/questions/24438238/…
  • 为了我自己的学习,你为什么决定从:S3 -> Redshift而不是S3 -> Kinesis -> Redshift
  • Kinesis 不是 S3 和 Redshift 之间的桥梁。 Kinesis 是一个端点,您可以将数据流式传输到……处理它……并将该处理数据放入 S3 和/或 Redshift

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


【解决方案1】:

【讨论】:

  • stackoverflow 的一个不错的实用程序似乎是重新表述文档功能的真正实用程序/应用程序,然后提供指向文档的链接。我要花一些时间才能在 AWS 网站上找到这些文档。
【解决方案2】:

伙计们的回答主要是为了达到目的。

我想发布一个可以从 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();
        }
    }
}

【讨论】:

  • 您是如何在 redshift 中配置示例数据库的?你是如何创建用户/密码的?
  • @RoyalTiger 我们已经允许从一个 EC2 实例连接到 Resdshift,从该实例我们可以使用 root 帐户使用 psql 终端连接到 redshift。完成后,其他一切只需要查找 SQL 并执行即可。
  • 当然,我相信我们需要在尝试连接到 redshift 之前使用一些脚本创建一个数据库,不是吗?
猜你喜欢
  • 1970-01-01
  • 2018-09-12
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 2018-04-25
  • 2018-07-18
相关资源
最近更新 更多