• Hive使用druid做连接池代码实现


    • 配置文档
    hive_jdbc_url=jdbc:hive2://192.168.0.22:10000/default
    hive.dbname=xxxxx
    hive_jdbc_username=root
    hive_jdbc_password=123456
    
    #配置初始化大小、最小、最大
    hive_initialSize=20
    hive_minIdle=20
    hive_maxActive=500
    
    #配置获取连接等待超时的时间
    hive_maxWait=60000
    • Jar包引入(Maven)
            <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>1.0.26</version>
            </dependency>    
    • 代码实现
    public class HiveDataSourceUtil {
        private static DruidDataSource hiveDataSource = new DruidDataSource();
        public static Connection conn = null;
        private static final Logger log = LoggerFactory.getLogger(HiveDataSourceUtil.class); 
    
        public static DruidDataSource getHiveDataSource() {
            if(hiveDataSource.isInited()){  
                return hiveDataSource;
            }         
                       
            try {       
                Properties dsProp = PropertiesUtil.getDataSourceProp();
                //基本属性 url、user、password
                hiveDataSource.setUrl(dsProp.getProperty("hive_jdbc_url"));
                hiveDataSource.setUsername(dsProp.getProperty("hive_jdbc_username"));
                hiveDataSource.setPassword(dsProp.getProperty("hive_jdbc_password"));
    
                //配置初始化大小、最小、最大
                hiveDataSource.setInitialSize(Integer.parseInt(dsProp.getProperty("hive_initialSize")));
                hiveDataSource.setMinIdle(Integer.parseInt(dsProp.getProperty("hive_minIdle")));
                hiveDataSource.setMaxActive(Integer.parseInt(dsProp.getProperty("hive_maxActive")));
    
                //配置获取连接等待超时的时间
                hiveDataSource.setMaxWait(Integer.parseInt(dsProp.getProperty("hive_maxWait")));
    
                //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
                hiveDataSource.setTimeBetweenEvictionRunsMillis(60000);
    
                //配置一个连接在池中最小生存的时间,单位是毫秒
                hiveDataSource.setMinEvictableIdleTimeMillis(300000);
    
    //            hiveDataSource.setValidationQuery("select * from xxxx");
                hiveDataSource.setTestWhileIdle(false);
    //            hiveDataSource.setTestOnBorrow(false);
    //            hiveDataSource.setTestOnReturn(false);
    
                //打开PSCache,并且指定每个连接上PSCache的大小
                hiveDataSource.setPoolPreparedStatements(true);
                hiveDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
       
                //配置监控统计拦截的filters
    //            hiveDataSource.setFilters("stat");
    
                hiveDataSource.init();
            } catch (SQLException e) {
                e.printStackTrace();
                closeHiveDataSource();
            }
            return hiveDataSource;
        }
    
        /**
         *@Description:关闭Hive连接池
         */
        public static void closeHiveDataSource(){
            if(hiveDataSource != null){
                hiveDataSource.close();
            }
        }  
    
        /**
         * 
         *@Description:获取Hive连接 
         *@return
         */
        public static Connection getHiveConn(){
            try {
                hiveDataSource = getHiveDataSource();
                conn = hiveDataSource.getConnection();
            } catch (SQLException e) {
                log.error("--"+e+":获取Hive连接失败!");
            }
            return conn;
        }
    
        /**
         *@Description:关闭Hive数据连接
         */
        public static void closeConn(){
            try {
                if(conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                log.error("--"+e+":关闭Hive-conn连接失败!");
            }
        }
       
    
        public static void main(String[] args) throws Exception {
            DataSource ds = HiveDataSourceUtil.getHiveDataSource();
            Connection conn = ds.getConnection();
            Statement stmt = null;
            if(conn == null){  
                System.out.println("null");
            }else{
                System.out.println("conn");
                stmt = conn.createStatement();
                ResultSet res = stmt.executeQuery("select * from xxxx t");  
                int i = 0;
                while(res.next()){
                    if(i<10){
                        System.out.println(res.getString(1));
                        i++;
                    }
                }
            }
    
            stmt.close();  
            conn.close();
        }
    }

    • 服务端服务开启
    打开远程端口:hive --service hiveserver2 &   
    PS自己不懂的话,可以找运维人员

  • 相关阅读:
    企业微信授权微信开发者工具
    liunx Python3中pip3安装模块出错,找不到SSL
    superagent 调用java接口,处理http请求
    Android开发一 application 应用界面主题Theme使用方法
    HTML5的Video标签的属性,方法和事件汇总
    多个select下拉框,选中当前某一项,其他下拉框去掉选中的值
    input range滑块插件 Powerange
    thinkphp 获取session的方法
    thinkphp I()方法获取不到ajax传值
    js验证图片上传大小,格式以及宽高
  • 原文地址:https://www.cnblogs.com/liuyp-ken/p/9933599.html
Copyright © 2020-2023  润新知