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自己不懂的话,可以找运维人员
Hive使用druid做连接池代码实现的更多相关文章
- DBCP、c3p0、Druid三大连接池区别
		DBCP.c3p0.Druid三大连接池区别 一.连接池优势 如果一个项目中如果需要多个连接,如果一直获取连接,断开连接,这样比较浪费资源: 如果创建一个池,用池来管理Connection,这样就可以 ... 
- SpringBoot整合Druid数据连接池
		SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ... 
- spring 5.x 系列第6篇 —— 整合 mybatis + druid 连接池 (代码配置方式)
		源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ... 
- 【SpringBoot笔记】SpringBoot整合Druid数据连接池
		废话少说,按SpringBoot的老套路来. [step1]:添加依赖 <!-- 数据库连接池 --> <dependency> <groupId>com.alib ... 
- c3p0,dbcp与druid 三大连接池的区别[转]
		说到druid,这个是在开源中国开源项目中看到的,说是比较好的数据连接池.于是乎就看看.扯淡就到这. 下面就讲讲用的比较多的数据库连接池.(其实我最先接触的是dbcp这个) 1)DBCP DBCP是一 ... 
- node-mysql中的连接池代码学习
		node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙.下面是node-mysql中的连接池的部分 ... 
- JDBC创建mysql连接池代码
		1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ... 
- spring boot配置druid数据连接池
		Druid是阿里巴巴开源项目中一个数据库连接池. Druid是一个jdbc组合,包含三个部分, 1.DruidDriver代理Driver,能够提供基于Filter-Chain模式得插件体系2.Dru ... 
- 【Mysql】SpringBoot阿里Druid数据源连接池配置
		一.pom.xml添加 <!-- 配置数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> &l ... 
随机推荐
- nodejs之log4js日志记录模块简单配置使用
			在我的一个node express项目中,使用了log4js来生成日志并且保存到文件里,生成的文件如下: 文件名字叫:access.log 如果在配置log4js的时候允许了同时存在多个备份log文件 ... 
- 【http】HTTP请求方法 之 OPTIONS
			OPTIONS方法是用于请求获得由Request-URI标识的资源在请求/响应的通信过程中可以使用的功能选项.通过这个方法,客户端可以在采取具体资源请求之前,决定对该资源采取何种必要措施,或者了解服务 ... 
- node 适合 5000 人同时在线左右的 游戏开发
			游戏开发性能的一些讨论 上面这个问题是在游戏上线前的一个性能顾虑 (但他确实是node多进程通讯间的一个比较麻烦的问题,数据一大就会出现性能上的瓶颈) 我们项目(手游)已经上线了,单服最高同时在线4. ... 
- zookeeper的c API 单线程与多线程问题 cli_st和cli_mt
			同样的程序,在centos和ubuntu上都没有问题,在solaris上问题却多多,据说是solaris管理更加严格. zookeeper_init方法,在传入一个错误的host也能初始化出一个非空的 ... 
- 对map参数进行排序
			/** * Map转换成url参数 by csl * * @param map * @param isSort 是否排序 * @return */ ... 
- Java调用本地方法又是怎么一回事
			JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ... 
- BitArray编写埃拉托斯特尼筛法(原书错误,学习更正)
			刚开始代码无法运行,修改后原书代码可以运行了,可是书本的思想还是错的. 虽然接下来的都是讲错误的思想下的“错误”的修改. 原书缺了窗体控件的代码,虽然在VS下不需要手动写窗体的代码,但是刚开始确实也不 ... 
- Linux操作系统设置SSH及SFTP通过密钥登录
			如果你使用过Linux操作系统的VPS或其他服务器,可能在登录时经常会提示你有多少次登录失败的记录. 这种登录失败的记录实际上也就是攻击者使用脚本自动扫描全网的IP然后进行筛选和测试,最终脚本会使用内 ... 
- hexo+github部署
			废话不多少,接着上次配置的环境进行github部署. 拥有自己的github 如果还没有github的账号就注册一个吧,传送门:GitHub官网:http://www.github.com 创建一个创 ... 
- 条件和循环(More Control Flow Tools)
			1.if语句 >>>a=7 >>> if a<0: ... print 'Negative changed to zero' ... elif a==0: . ... 
