import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class HiveConnect {
private Connection conn;
final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public HiveConnect(String password) throws ClassNotFoundException, SQLException {
String driverName = "org.apache.hive.jdbc.HiveDriver";
URI hiveUri = null;
try {
hiveUri= new URI("jdbc:hive2://basan:8443/;ssl=true;sslTrustStore=/Users/basan/Documents/workspace-sts-3.8.4.RELEASE/g-conumption-spring/ssl-config/prod/truststore.jks;trustStorePassword=password;transportMode=http;httpPath=gateway/re/hive");
} catch (URISyntaxException e) {
e.printStackTrace();
}
String dbHost = hiveUri.toString();
String dbUser = "basan";
Class.forName(driverName);
conn = DriverManager.getConnection(dbHost, dbUser, password);
}
public ResultSet query(String qString) throws SQLException {
Statement statement = conn.createStatement();
return statement.executeQuery(qString);
}
public Connection getConnection() {
return conn;
}
public void queryHive(String query) throws SQLException {
System.out.println(query);
ResultSet rs = this.query(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
}
public void close() {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws FileNotFoundException {
try {
String password = args[0];
HiveConnect h = new HiveConnect(password);
h.queryHive("select * from fnd.basn_pkg_track_hdr limit 10");
h.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment