Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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("nnconnecting to database...nn");
- //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("nnConnection made!nn");
- 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("nnExecuting...nn");
- 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("nnThats all copy using simple JDBC.nn");
- statement.close();
- conn.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment