public class HiveUtil { private static Logger logger = LoggerFactory.getLogger(HiveUtil.class); private static BasicDataSource basicDataSource = null; private static String HIVE_DRIVER = null; private static String HIVE_URL = null; static { HIVE_DRIVER = ConfigProperties.getInstance().getValue("hive.driver"); HIVE_URL = ConfigProperties.getInstance().getValue("hive.url"); //创建数据源对象 if (basicDataSource == null) { basicDataSource = new BasicDataSource(); //设置连接数据库的驱动 basicDataSource.setDriverClassName(HIVE_DRIVER); //设置连接数据库的url basicDataSource.setUrl(HIVE_URL); //设置连接数据库的用户名 basicDataSource.setUsername("root"); //设置连接数据库的密码 basicDataSource.setPassword(""); //设置连接池启动时的初始值 basicDataSource.setInitialSize(5); //设置连接池的最大值 basicDataSource.setMaxActive(256); //最大空闲值.当经过一个高峰时间后,连接池可以慢慢将用不到的连接慢慢释放一部分,一直减少到maxIdle为止 basicDataSource.setMaxIdle(20); //最小空闲值.当空闲的连接数少于该值时,连接池就会预申请一些连接,以避免洪峰来时再申请而造成的性能开销 basicDataSource.setMinIdle(5); } } public static Connection getConnection() throws SQLException { return basicDataSource.getConnection(); } public static Statement getStatement(Connection connection) throws SQLException { return connection.createStatement(); } public static PreparedStatement getPreparedStatement(Connection connection, String sql) throws SQLException { return connection.prepareStatement(sql); } public static void switchDB(Connection connection, String database) throws SQLException { Statement statement = getStatement(connection); if (StringUtils.isNotEmpty(database)) { statement.execute("use " + database); } } public static void createTable(Connection connection, String database, String tableName, String sql) throws SQLException { Statement statement = getStatement(connection); if (StringUtils.isNotEmpty(tableName)) { statement.execute("drop table if exists " + tableName); statement.execute(sql); } close(statement); } public static void listTables(Connection connection, String database) throws SQLException { Statement statement = getStatement(connection); ResultSet resultSet = statement.executeQuery("show tables"); while (resultSet.next()) { logger.info(resultSet.getString("tab_name")); } close(resultSet); close(statement); } public static void listTableStructure(Connection connection, String database, String tableName) throws SQLException { Statement statement = getStatement(connection); ResultSet resultSet = statement.executeQuery("describe " + tableName); while (resultSet.next()) { logger.info(resultSet.getString("col_name") + " | " + resultSet.getString("data_type") + " | " + resultSet.getString("comment")); } close(resultSet); } public static void dropTable(Connection connection, String database, String tableName) throws SQLException { Statement statement = getStatement(connection); if (StringUtils.isNotEmpty(tableName)) { statement.execute("drop table if exists " + tableName); } close(statement); } public static void execute(Connection connection, String sql, Boolean isPrepared) throws SQLException { if (isPrepared) { PreparedStatement preparedStatement = getPreparedStatement(connection, sql); ResultSet resultSet = preparedStatement.executeQuery(); close(resultSet); close(preparedStatement); } else { Statement statement = getStatement(connection); statement.execute(sql); close(statement); } } public static ResultSet list(Connection connection, PreparedStatement preparedStatement, Object[] objs) throws SQLException { if (ArrayUtils.isNotEmpty(objs)) { for (int i = 0; i < objs.length; i++) { preparedStatement.setObject(i, objs[i]); } } return preparedStatement.executeQuery(); } public static void close(Connection connection) throws SQLException { if (connection != null) { connection.close(); connection = null; } } public static void close(Statement statement) throws SQLException { if (statement != null) { statement.close(); statement = null; } } public static void close(PreparedStatement preparedStatement) { if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } preparedStatement = null; } } public static void close(ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } resultSet = null; } } }
Hive工具类
猜你喜欢
转载自hogwartsrow.iteye.com/blog/2371327
今日推荐
周排行