• 配置文档
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自己不懂的话,可以找运维人员

最新文章

  1. godaddy域名使用DNSPod做DNS解析图文教程
  2. Java--&gt;Json解析网页数据
  3. 有了第一台自己开发的pro,开心,明天分享最近整理逆向分析ios的一些东西
  4. 单机安装HBase
  5. H5文件操作API
  6. MD5和DES加密方法
  7. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
  8. Windows server 2008 下iis7 架设ftp服务器
  9. 一天一个Java基础——反射
  10. spring &lt;form:checkboxes&gt; tag and css class
  11. Sql时间函数
  12. “canvas画布仿window系统自带画图软件&quot;项目的思考
  13. Intellij16创建Spring-Mybatis项目创(填)建(坑)记录,解决IDEA下找不到xml文件的问题
  14. 解决OrangePi 耳机孔没有声音
  15. JAVA 动态代理原理和实现
  16. Python练手例子(5)
  17. MP3文件结构解析(超详细)
  18. C++STL中的unique函数解析
  19. Hadoop 管理工具HUE配置-集成Unix用户和用户组
  20. [uart]linux uart应用层配置

热门文章

  1. canvas 画布 文字描边
  2. 剑指offer--50.滑动窗口的最大值
  3. New Concept English three (24)
  4. DataTable和实体类通过反射相互转换
  5. 开源一款ftp软件——filezilla
  6. cocos2d-x 发布win32 release版本后找不到msvcr110.dll
  7. 关于dyld: Library not loaded
  8. dom4j 改变XML声明和编码格式
  9. selenium-java,cookie登陆
  10. linux自学(三)之开启虚拟机