Redshift is a data warehouse from Amazon that supports SQL. It’s a managed solution on AWS cloud.
We can connect to Redshift database from Java using JDBC and query tables in just like any other database.
I am showing here code that connects to Redshift and queries table schema information.
package com.hadoopnotes;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class RedshiftTest {
public static void main(String[] args) {
Connection connection;
String redshiftUrl = "jdbc:redshift://ip_address:5439/dbatabase";
try {
DriverManager.registerDriver(new com.amazon.redshift.jdbc41.Driver());
Properties driverProperties = new Properties();
driverProperties.put("user", "myuser");
driverProperties.put("password", "mypassword");
connection = DriverManager.getConnection(redshiftUrl, driverProperties);
String schemaSql = "set search_path to myschema";
String sql = "select \"column\", type from pg_table_def where schemaname='myschema' and tablename = 'mytable'";
System.out.println("SQL:" + sql);
Statement stmt = null;
try {
stmt = connection.createStatement();
stmt.execute(schemaSql);
System.out.println("Running query now...");
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
// this prints column name and its type
System.out.println(rs.getString(1) + "--" + rs.getString(2));
}
} finally {
if (stmt != null)
stmt.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
If using Maven to build this Java project, add the following dependency.
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc41</artifactId>
<version>1.2.10.1009</version>
</dependency>
Build project and run it.
mvn package java -cp target/myjar.jar com.hadoopnotes.RedshiftTest
Or, from command line, compile and run:
javac -cp redshift-jdbc41-1.2.10.1009.jar com/hadoopnotes/RedshiftTest.java java -cp .:redshift-jdbc41-1.2.10.1009.jar com.hadoopnotes.RedshiftTest