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 ...
随机推荐
- Shell 命令行,实现一个获取任意位数的随机密码的脚本
Shell 命令行,实现一个获取任意位数的随机密码的脚本 每次我们想要获得一个密码的时候都很头疼,于是我之前自己用nodejs写了一个 Shell 脚本.这两天在学习 bash Shell 所以,想用 ...
- Java并发编程之CountDownLatch,CyclicBarrier实现一组线程相互等待、唤醒
java多线程应用场景不少,有时自己编写代码又不太容易实现,好在concurrent包提供了不少实现类,还有google的guava包更是提供了一些最佳实践,这让我们在面对一些多线程的场景时,有了不少 ...
- 深入理解Hystrix之文档翻译
转载请标明出处: http://blog.csdn.net/forezp/article/details/75333088 本文出自方志朋的博客 什么是Hystrix 在分布式系统中,服务与服务之间依 ...
- [置顶]
AI大行其道,你准备好了吗?—谨送给徘徊于转行AI的程序员
前言 近年来,随着 Google 的 AlphaGo 打败韩国围棋棋手李世乭之后,机器学习尤其是深度学习的热潮席卷了整个IT界.所有的互联网公司,尤其是 Google 微软,百度,腾讯等巨头,无不在布 ...
- Emgu cv 学习笔记
http://www.cnblogs.com/CoverCat/p/5003363.html emgu中imagebox与picturebox imagebox 是emgu 设置好厚,新出现的控件 ...
- VSS虚拟交换系统
下面介绍一下如何在CISCO交换机上配置VSS,具体配置如下: //在CISCO1 上配置vss域,两台设备都要在同一个域中 Cisco-(confgi)#switch virtual domain ...
- 2017《Java技术》预备作业01
2017<Java技术>预备作业 1.学习使用MarkDown 本学期的随笔都将使用MarkDown格式,要求熟练掌握MarkDown语法,学会如何使用标题,插入超链接,列表,插入图片,插 ...
- 【排序】希尔排序,C++实现
原创博文,转载请注明出处! 本文代码的github地址 # 基本思路 希尔排序是”直接插入排序“的改进版,也称为“缩小增量排序”.基本原理:先将待排序的数组元素分成多个序列,然后对每个子序 ...
- 在 Visual Studio中 使用Apache Cordova 开发安卓、iOS程序(自定义图标和闪屏)
方法1 1台安装有linux的电脑,虚拟机也行,并安装imagemagick软件包.我用的是linux mint,直接在软件管理器中安装即可 把https://github.com/shamork/c ...
- Yahoo关于性能优化的N条规则
本来这是个老生常谈的问题,上周自成又分享了一些性能优化的建议,我这里再做一个全面的Tips整理,谨作为查阅型的文档,不妥之处,还请指正: 一. Yahoo的规则条例: 谨记:80%-90%的终端响应时 ...