banner
Matt

Matt

Matt-xlog
telegram
github

【JAVA】【工具類】hive自定義連接池

import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.sql.*;
import java.util.LinkedList;

/*** 類名: ImpalaJdbcUtils
 * * 描述:impala jdbc的工具類
 * * 自定義連接工具池
 * * **/
public class ImpalaJdbcUtils {
    private static final Logger log = LoggerFactory.getLogger(ImpalaJdbcUtils.class);
    /*** 描述:驅動類 <br>*/
    private static String DRIVER_CLASS = "org.apache.hive.jdbc.HiveDriver";
    private static String CONNECTION_URL = "jdbc:hive2://*****/default;principal=impala/";
    /*** 連接池**/
    private static LinkedList<Connection> pool = new LinkedList<Connection>();//初始化10個連接

    static {
        try {
            for (int i = 0; i < 15; i++) {
                Connection conn = getImpalaConnection();
                //得到一個連接
                pool.add(conn);
            }
        } catch (Exception e) {
            log.error("資料庫連接池初始化失敗,請檢查配置", e);
        }
    }
    /*** 從連接池中獲取連接**/
    public static Connection getConnection() throws SQLException {
        // 取出連接池中一個連接
        Connection conn = null;
        // 測試連接是否超時,超時重新連接
        try {
            conn = pool.removeFirst();
            //執行以下連接,查看是否異常
            String sql = "show DATABASES";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            rs.close();
            ps.close();
        }catch (Exception e){
            log.warn("連接異常,嘗試重新生成連接!");
            conn = getImpalaConnection();
        }
        // 刪除第一個連接返回
        log.info("取出一個連接剩餘 " + pool.size() + "個連接!");
        return conn;
    }

    /*** @Description 釋放連接***/
    public static void releaseConnection(Connection conn) {
        pool.add(conn);
        log.info("將連接放回到連接池中,數量:" + pool.size());
    }
    /*** @Description 獲取impala連接* **/
    public static Connection getImpalaConnection() {
        try {
            Object o = UserGroupInformation.getLoginUser().doAs((PrivilegedAction<Object>) () -> {
                Connection connection = null;
                try {
                    Class.forName(DRIVER_CLASS);
                } catch (ClassNotFoundException e) {
                    log.error("加載impala驅動失敗", e);
                }
                try {
                    connection = DriverManager.getConnection(CONNECTION_URL);
                } catch (SQLException e) {
                    log.error("獲取jdbc連接失敗", e);
                }
                return connection;
            });
            return (Connection) o;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*** 關閉jdbc連接* **/
    public static void disconnect(Connection connection, ResultSet res, PreparedStatement ps) {
        try {
            if (res != null){
                res.close();
            }
            if (ps != null){
                ps.close();
            }
            if (connection != null) {
                connection.close();
                ImpalaJdbcUtils.releaseConnection(connection);
            }
        } catch (SQLException e) {
            log.error("jdbc連接關閉失敗", e);
        }
    }

    //不使用連接池
    public Connection getConnectSync(){
        try {
            Object o = UserGroupInformation.getLoginUser().doAs((PrivilegedAction<Object>) () -> {
                Connection connection = null;
                try {
                    Class.forName(DRIVER_CLASS);
                } catch (ClassNotFoundException e) {
                    log.error("加載impala驅動失敗", e);
                }
                try {
                    connection = DriverManager.getConnection(CONNECTION_URL);
                } catch (SQLException e) {
                    log.error("獲取jdbc連接失敗", e);
                }
                return connection;
            });
            return (Connection) o;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}         
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。