Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java sdk for copying to Redshift

Is it possible to fire a copy command from S3 To Redshift through java jdbc connection?

Example: copy test from 's3://' CREDENTIALS 'aws_access_key_id=xxxxxxx;aws_secret_access_key=xxxxxxxxx'

like image 815
vijay Avatar asked Aug 27 '13 12:08

vijay


People also ask

Which COPY command would you use to load the data to Redshift?

The syntax to specify the files to be loaded by using a manifest file is as follows: copy <table_name> from 's3://<bucket_name>/<manifest_file>' authorization manifest; The table to be loaded must already exist in the database.

What is the most efficient and fastest way to load data into Redshift?

A COPY command is the most efficient way to load a table. You can also add data to your tables using INSERT commands, though it is much less efficient than using COPY. The COPY command is able to read from multiple data files or multiple data streams simultaneously.


1 Answers

Yes try code as below

String dbURL = "jdbc:postgresql://x.y.us-east-1.redshift.amazonaws.com:5439/dev";
String MasterUsername = "userame";
String MasterUserPassword = "password";

           Connection conn = null;
            Statement stmt = null;
            try{
               //Dynamically load postgresql driver at runtime.
               Class.forName("org.postgresql.Driver");


               System.out.println("Connecting to database...");
               Properties props = new Properties();


               props.setProperty("user", MasterUsername);
               props.setProperty("password", MasterUserPassword);
               conn = DriverManager.getConnection(dbURL, props);
               stmt = conn.createStatement();
               String sql="copy test from 's3://' CREDENTIALS     'aws_access_key_id=xxxxxxx;aws_secret_access_key=xxxxxxxxx'"
                int j = stmt.executeUpdate(sql);

                stmt.close();
               conn.close();
            }catch(Exception ex){
               //For convenience, handle all errors here.
               ex.printStackTrace();
            }
like image 68
Sandesh Deshmane Avatar answered Oct 03 '22 20:10

Sandesh Deshmane