dnet-hadoop/dhp-workflows/dhp-java-action-dbconnection/src/main/java/eu/dnetlib/oa/graph/usagestats/export/Simple.java

117 lines
3.9 KiB
Java
Executable File

package eu.dnetlib.oa.graph.usagestats.export;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Simple {
public static void main(final String[] args) throws Exception {
System.out.println("=============================================");
System.out.println("Staring");
System.out.println("=============================================");
// Connection string that works OK
// String connectionUrl =
// "jdbc:hive2://iis-cdh5-test-m3.ocean.icm.edu.pl:10000/stats_wf_db_galexiou_oozie_beta;UID=spyros;PWD=XXXXXX;UseNativeQuery=1";
// The following connectio string also works
String connectionUrl = "jdbc:hive2://iis-cdh5-test-m3.ocean.icm.edu.pl:10000/;UseNativeQuery=1";
String jdbcDriverName = "org.apache.hive.jdbc.HiveDriver";
System.out.println("\n=============================================");
System.out.println("Using Connection URL: " + connectionUrl);
System.out.println("USing JDBC Driver " + jdbcDriverName);
Connection con = null;
try {
System.out.println("Step 1");
Class.forName(jdbcDriverName);
System.out.println("Step 2");
con = DriverManager.getConnection(connectionUrl);
System.out.println("Step 3");
Statement stmt = con.createStatement();
System.out.println("Step 4");
ResultSet rs = stmt.executeQuery("select personid, lastname from test_db_spyros.persons");
System.out.println("\n== Begin Query Results ======================");
// print the results to the console
while (rs.next()) {
// the example query returns one String column
System.out.println(rs.getString(1) + " | " + rs.getString(2));
}
System.out.println("== End Query Results =======================\n\n");
System.out.println("==> Drop if exists");
// Drop table if exists
stmt.execute("DROP TABLE IF EXISTS test_db_spyros.Persons2");
System.out.println("==> Create table");
// Create table
stmt
.execute(
"CREATE TABLE test_db_spyros.Persons2 (personid int, lastname varchar(255), firstname varchar(255)) clustered by (personid) into 100 buckets stored as orc tblproperties('transactional'='true')");
System.out.println("==> Insert");
// Insert
stmt
.execute(
"INSERT INTO test_db_spyros.persons2 (personid, lastname, firstname) VALUES ('1', 'ZoupZoup', 'Spyros')");
System.out.println("==> Select 1 on persons2");
// Select the inserted values
rs = stmt.executeQuery("select personid, lastname, firstname from test_db_spyros.persons2");
while (rs.next()) {
// the example query returns one String column
System.out.println(rs.getString(1) + " | " + rs.getString(2) + " | " + rs.getString(3));
}
System.out.println("==> Update");
// Update
stmt
.execute(
"UPDATE test_db_spyros.persons2 SET lastname='ZoupZoup2', firstname='Spyros2' WHERE personid=1");
System.out.println("==> Select 2 on persons2");
// Select the updated values
rs = stmt.executeQuery("select personid, lastname, firstname from test_db_spyros.persons2");
while (rs.next()) {
// the example query returns one String column
System.out.println(rs.getString(1) + " | " + rs.getString(2) + " | " + rs.getString(3));
}
} catch (SQLException e) {
System.out.println("Ex 1");
e.printStackTrace();
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("Ex 2");
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
try {
con.close();
} catch (Exception e) {
// swallow
System.out.println("Ex 3");
e.printStackTrace();
System.out.println(e.getMessage());
}
}
System.out.println("=============================================");
System.out.println("Ending");
System.out.println("=============================================");
}
}