• 配置文档
  1. hive_jdbc_url=jdbc:hive2://192.168.0.22:10000/default
  2. hive.dbname=xxxxx
  3. hive_jdbc_username=root
  4. hive_jdbc_password=123456
  5.  
  6. #配置初始化大小、最小、最大
  7. hive_initialSize=20
  8. hive_minIdle=20
  9. hive_maxActive=500
  10.  
  11. #配置获取连接等待超时的时间
  12. hive_maxWait=60000
  • Jar包引入(Maven)
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid</artifactId>
  4. <version>1.0.26</version>
  5. </dependency>
  • 代码实现
  1. public class HiveDataSourceUtil {
  2. private static DruidDataSource hiveDataSource = new DruidDataSource();
  3. public static Connection conn = null;
  4. private static final Logger log = LoggerFactory.getLogger(HiveDataSourceUtil.class);
  5.  
  6. public static DruidDataSource getHiveDataSource() {
  7. if(hiveDataSource.isInited()){
  8. return hiveDataSource;
  9. }
  10.  
  11. try {
  12. Properties dsProp = PropertiesUtil.getDataSourceProp();
  13. //基本属性 url、user、password
  14. hiveDataSource.setUrl(dsProp.getProperty("hive_jdbc_url"));
  15. hiveDataSource.setUsername(dsProp.getProperty("hive_jdbc_username"));
  16. hiveDataSource.setPassword(dsProp.getProperty("hive_jdbc_password"));
  17.  
  18. //配置初始化大小、最小、最大
  19. hiveDataSource.setInitialSize(Integer.parseInt(dsProp.getProperty("hive_initialSize")));
  20. hiveDataSource.setMinIdle(Integer.parseInt(dsProp.getProperty("hive_minIdle")));
  21. hiveDataSource.setMaxActive(Integer.parseInt(dsProp.getProperty("hive_maxActive")));
  22.  
  23. //配置获取连接等待超时的时间
  24. hiveDataSource.setMaxWait(Integer.parseInt(dsProp.getProperty("hive_maxWait")));
  25.  
  26. //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  27. hiveDataSource.setTimeBetweenEvictionRunsMillis(60000);
  28.  
  29. //配置一个连接在池中最小生存的时间,单位是毫秒
  30. hiveDataSource.setMinEvictableIdleTimeMillis(300000);
  31.  
  32. // hiveDataSource.setValidationQuery("select * from xxxx");
  33. hiveDataSource.setTestWhileIdle(false);
  34. // hiveDataSource.setTestOnBorrow(false);
  35. // hiveDataSource.setTestOnReturn(false);
  36.  
  37. //打开PSCache,并且指定每个连接上PSCache的大小
  38. hiveDataSource.setPoolPreparedStatements(true);
  39. hiveDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
  40.  
  41. //配置监控统计拦截的filters
  42. // hiveDataSource.setFilters("stat");
  43.  
  44. hiveDataSource.init();
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. closeHiveDataSource();
  48. }
  49. return hiveDataSource;
  50. }
  51.  
  52. /**
  53. *@Description:关闭Hive连接池
  54. */
  55. public static void closeHiveDataSource(){
  56. if(hiveDataSource != null){
  57. hiveDataSource.close();
  58. }
  59. }
  60.  
  61. /**
  62. *
  63. *@Description:获取Hive连接
  64. *@return
  65. */
  66. public static Connection getHiveConn(){
  67. try {
  68. hiveDataSource = getHiveDataSource();
  69. conn = hiveDataSource.getConnection();
  70. } catch (SQLException e) {
  71. log.error("--"+e+":获取Hive连接失败!");
  72. }
  73. return conn;
  74. }
  75.  
  76. /**
  77. *@Description:关闭Hive数据连接
  78. */
  79. public static void closeConn(){
  80. try {
  81. if(conn != null){
  82. conn.close();
  83. }
  84. } catch (SQLException e) {
  85. log.error("--"+e+":关闭Hive-conn连接失败!");
  86. }
  87. }
  88.  
  89. public static void main(String[] args) throws Exception {
  90. DataSource ds = HiveDataSourceUtil.getHiveDataSource();
  91. Connection conn = ds.getConnection();
  92. Statement stmt = null;
  93. if(conn == null){
  94. System.out.println("null");
  95. }else{
  96. System.out.println("conn");
  97. stmt = conn.createStatement();
  98. ResultSet res = stmt.executeQuery("select * from xxxx t");
  99. int i = 0;
  100. while(res.next()){
  101. if(i<10){
  102. System.out.println(res.getString(1));
  103. i++;
  104. }
  105. }
  106. }
  107.  
  108. stmt.close();
  109. conn.close();
  110. }
  111. }
  • 服务端服务开启
  1. 打开远程端口:hive --service hiveserver2 &
  2. 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. Appium Remote webdriver调用

    remote webdriver的模板 默认开启4723端口接受webdriver请求 默认开启4724用于和android通讯 #coding:utf-8 #Import the common pa ...

  2. 联想THINKPAD E40的快捷键怎么关闭?哪些F1 F2 F3的键我需要用到 但是每次都按FN 太烦人了

    1.开机时,按F1进入BIOS,依次选择CONFIG--Keyboard/Mouse,2.在Change to "f1-f12 keys"选项中,更改设置为Legacy或者Defa ...

  3. flowable EngineConfiguration的实现分析(2)

    EngineConfiguration的实现类是一个抽象类:AbstractEngineConfiguration 一.引擎配置的分类 继承 AbsractEngineConfiguration的子类 ...

  4. ranch分析学习(四)

    经过的前面的梳理,整个ranch框架的结构,大致有了一个清晰的脉络,即使我说的不是很清楚大家也基本能阅读懂源码.下面我继续分析剩下的的几个文件. 7.ranch_transport.erl 这个文件是 ...

  5. [译]贵宾犬咬伤了TLS

    原文链接:https://community.qualys.com/blogs/securitylabs/2014/12/08/poodle-bites-tls 原文发表时间:2014.12.8 今天 ...

  6. BZOJ3688 折线统计【树状数组优化DP】

    Description 二维平面上有n个点(xi, yi),现在这些点中取若干点构成一个集合S,对它们按照x坐标排序,顺次连接,将会构成一些连续上升.下降的折线,设其数量为f(S).如下图中,1-&g ...

  7. openvpn搭建笔记

    #整理openvpn安装 #1安装上传/下载软件 yum -y install openssh-clients lrzsz #2更新时间 yum -y install ntpdatentpdate t ...

  8. 像黑客一样!Chrome 完全键盘操作指南(原生快捷键 + Vimium 插件)

    有那么一波小伙伴,多数时候都不需要用到鼠标,通常他们正好是“黑客”.当你开始使用键盘操作一切时,便能体会到无需用鼠标瞄准按钮时的干脆,无需在键盘和鼠标之间移动手时的轻松. Chrome 原生自带大量快 ...

  9. SmartSql 动态仓储

    动态代理仓储 SmartSql源码:https://github.com/Ahoo-Wang/SmartSql 简介 动态代理仓储(SmartSql.DyRepository)组件是SmartSql非 ...

  10. hdfs会出现的一些问题

    实训的第一篇博客献给坑了我的hdfs…… 由于电脑的原因,突然花屏,虚拟机来不及挂起.之后发现50070端口进不去,查看jps进程发现没有namenode 先用stop-all.sh命令再start还 ...