SHARE
TWEET

Untitled

a guest Aug 18th, 2018 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.util.Properties;
  5.  
  6. public class RedShiftJDBC {
  7.     public static void main(String[] args) {
  8.  
  9.         Connection conn = null;
  10.         Statement statement = null;
  11.         try {
  12.             //Even postgresql driver will work too. You need to make sure to choose postgresql url instead of redshift.
  13.             //Class.forName("org.postgresql.Driver");
  14.             //Make sure to choose appropriate Redshift Jdbc driver and its jar in classpath
  15.             Class.forName("com.amazon.redshift.jdbc42.Driver");
  16.             Properties props = new Properties();
  17.             props.setProperty("user", "username***");
  18.             props.setProperty("password", "password****");
  19.  
  20.             System.out.println("nnconnecting to database...nn");
  21.             //In case you are using postgreSQL jdbc driver.
  22.             //conn = DriverManager.getConnection("jdbc:postgresql://********8-your-to-redshift.redshift.amazonaws.com:5439/example-database", props);
  23.  
  24.             conn = DriverManager.getConnection("jdbc:redshift://********url-to-redshift.redshift.amazonaws.com:5439/example-database", props);
  25.  
  26.             System.out.println("nnConnection made!nn");
  27.  
  28.             statement = conn.createStatement();
  29.  
  30.             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";
  31.  
  32.             System.out.println("nnExecuting...nn");
  33.  
  34.             statement.executeUpdate(command);
  35.             //you must need to commit, if you realy want to have data saved, otherwise it will not appear if you query from other session.
  36.             conn.commit();
  37.             System.out.println("nnThats all copy using simple JDBC.nn");
  38.             statement.close();
  39.             conn.close();
  40.         } catch (Exception ex) {
  41.             ex.printStackTrace();
  42.         }
  43.     }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top