forked from D-Net/dnet-hadoop
109 lines
2.8 KiB
Java
109 lines
2.8 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
|
|
package eu.dnetlib.oa.graph.usagestats.export;
|
|
|
|
/**
|
|
* @author D. Pierrakos, S. Zoupanos
|
|
*/
|
|
/**
|
|
* @author D. Pierrakos, S. Zoupanos
|
|
*/
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.Properties;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
public abstract class ConnectDB {
|
|
|
|
public static Connection DB_HIVE_CONNECTION;
|
|
public static Connection DB_IMPALA_CONNECTION;
|
|
|
|
private static String dbHiveUrl;
|
|
private static String dbImpalaUrl;
|
|
private static String usageStatsDBSchema;
|
|
private static String statsDBSchema;
|
|
private final static Logger log = Logger.getLogger(ConnectDB.class);
|
|
|
|
private final static boolean createAlwaysNewConnection = true;
|
|
|
|
static void init() throws ClassNotFoundException {
|
|
|
|
dbHiveUrl = ExecuteWorkflow.dbHiveUrl;
|
|
dbImpalaUrl = ExecuteWorkflow.dbImpalaUrl;
|
|
usageStatsDBSchema = ExecuteWorkflow.usageStatsDBSchema;
|
|
statsDBSchema = ExecuteWorkflow.statsDBSchema;
|
|
|
|
Class.forName("org.apache.hive.jdbc.HiveDriver");
|
|
}
|
|
|
|
public static Connection getHiveConnection() throws SQLException {
|
|
if (!createAlwaysNewConnection) {
|
|
if (DB_HIVE_CONNECTION != null && !DB_HIVE_CONNECTION.isClosed()) {
|
|
return DB_HIVE_CONNECTION;
|
|
} else {
|
|
DB_HIVE_CONNECTION = connectHive();
|
|
|
|
return DB_HIVE_CONNECTION;
|
|
}
|
|
} else {
|
|
if (DB_HIVE_CONNECTION != null && !DB_HIVE_CONNECTION.isClosed())
|
|
DB_HIVE_CONNECTION.close();
|
|
|
|
DB_HIVE_CONNECTION = connectHive();
|
|
return DB_HIVE_CONNECTION;
|
|
}
|
|
}
|
|
|
|
public static Connection getImpalaConnection() throws SQLException {
|
|
if (!createAlwaysNewConnection) {
|
|
if (DB_IMPALA_CONNECTION != null && !DB_IMPALA_CONNECTION.isClosed()) {
|
|
return DB_IMPALA_CONNECTION;
|
|
} else {
|
|
DB_IMPALA_CONNECTION = connectImpala();
|
|
|
|
return DB_IMPALA_CONNECTION;
|
|
}
|
|
} else {
|
|
if (DB_IMPALA_CONNECTION != null && !DB_IMPALA_CONNECTION.isClosed())
|
|
DB_IMPALA_CONNECTION.close();
|
|
|
|
DB_IMPALA_CONNECTION = connectImpala();
|
|
return DB_IMPALA_CONNECTION;
|
|
}
|
|
}
|
|
|
|
public static String getUsageStatsDBSchema() {
|
|
return ConnectDB.usageStatsDBSchema;
|
|
}
|
|
|
|
public static String getStatsDBSchema() {
|
|
return ConnectDB.statsDBSchema;
|
|
}
|
|
|
|
private static Connection connectHive() throws SQLException {
|
|
|
|
Connection connection = DriverManager.getConnection(dbHiveUrl);
|
|
Statement stmt = connection.createStatement();
|
|
log.debug("Opened database successfully");
|
|
|
|
return connection;
|
|
}
|
|
|
|
private static Connection connectImpala() throws SQLException {
|
|
|
|
Connection connection = DriverManager.getConnection(dbImpalaUrl);
|
|
Statement stmt = connection.createStatement();
|
|
log.debug("Opened database successfully");
|
|
|
|
return connection;
|
|
}
|
|
|
|
}
|