• 配置文档
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做连接池代码实现的更多相关文章

  1. DBCP、c3p0、Druid三大连接池区别

    DBCP.c3p0.Druid三大连接池区别 一.连接池优势 如果一个项目中如果需要多个连接,如果一直获取连接,断开连接,这样比较浪费资源: 如果创建一个池,用池来管理Connection,这样就可以 ...

  2. SpringBoot整合Druid数据连接池

    SpringBoot整合Druid数据连接池 Druid是什么? Druid是Alibaba开源的的数据库连接池.Druid能够提供强大的监控和扩展功能. 在哪里下载druid maven中央仓库: ...

  3. spring 5.x 系列第6篇 —— 整合 mybatis + druid 连接池 (代码配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 项目目录结构 1.创建maven工程,除了Spring基本依赖外,还需要导 ...

  4. 【SpringBoot笔记】SpringBoot整合Druid数据连接池

    废话少说,按SpringBoot的老套路来. [step1]:添加依赖 <!-- 数据库连接池 --> <dependency> <groupId>com.alib ...

  5. c3p0,dbcp与druid 三大连接池的区别[转]

    说到druid,这个是在开源中国开源项目中看到的,说是比较好的数据连接池.于是乎就看看.扯淡就到这. 下面就讲讲用的比较多的数据库连接池.(其实我最先接触的是dbcp这个) 1)DBCP DBCP是一 ...

  6. node-mysql中的连接池代码学习

    node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙.下面是node-mysql中的连接池的部分 ...

  7. JDBC创建mysql连接池代码

    1.底层实现类(DBConnection) package JDBC.JDBCPool.MyJDBCPool; import java.sql.Connection; import java.sql. ...

  8. spring boot配置druid数据连接池

    Druid是阿里巴巴开源项目中一个数据库连接池. Druid是一个jdbc组合,包含三个部分, 1.DruidDriver代理Driver,能够提供基于Filter-Chain模式得插件体系2.Dru ...

  9. 【Mysql】SpringBoot阿里Druid数据源连接池配置

    一.pom.xml添加 <!-- 配置数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> &l ...

随机推荐

  1. avr 烧录失败

    用Atmel studio 6.0 配置mkII烧录器 使用上位机bat程序烧录 提示错误:firmware is old... 1参考(关于FUSe setting) http://www.cnbl ...

  2. Openlayers3中实现地图的切割

    概述: 本文讲述如何在Openlayers3中结合canvas实现对地图的切割. 效果: 全图 切割北京区域 切割河北区域 实现: <html xmlns="http://www.w3 ...

  3. if条件判断

    if 条件判断的是布尔值,常用的有以下几种 1.in 在不在它里面,返回的是布尔值 names='zhangsan lisi xiaoming' print("zhangsan" ...

  4. ICE实现服务器客户端

    本文将结合实际项目,做一个基于ice的实际项目实例应用,该实例完成客户端调用服务端接口完成消息发送,计算的功能.1,创建java项目ICEServer,导入ice.jar. 2,在项目下创建slice ...

  5. Vue之拦截与响应拦截

    vue中有一个拦截方法,当我们发起请求或者请求回来的时候,我们需要做一定的数据过滤或者拦截 下面是在开发项目时进行的一个axios的请求封装: 拦截器:就是我们在请求之前进行的一个操作比如说,我们可以 ...

  6. day4(带)

    int is_his_file(char*filename,char *username) { int ret; struct passwd *user_info;// struct stat sta ...

  7. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  8. Jsp邮件找回密码全攻略

    [来源网络  http://www.2cto.com/kf/201502/376374.html] 一般大型网站我们登录的时候,密码忘了都有个功能可以找回密码. 细数下大致的方法: 1.直接把密码发送 ...

  9. BZOJ1085 SCOI2005 骑士精神【IDA* 启发式迭代加深】

    BZOJ1085 SCOI2005 骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐 ...

  10. vs2013 快捷键

      //////////////// 编辑:   ctrl+-(shift+ctrl+-):移动光标到上次位置或相反,比如定位一个函数,转到函数定义后想回到函数使用处,则用ctrl+-,若又想回到函数 ...