SpringBoot 2.X集成Hive-jdbc 3.1.1
最近公司有一个需求,需求的内容是根据用户页面选择的参数条件查询Hive,数量量大致是300万以内,要求3秒响应.使用的其它的技术就不要说了,先说说SpingBoot集成Hive-jdbc吧,网上虽然有完整的集成方案,但是根据方案来实现总是遇到各种各样的问题,一会日志包问题 一会jetty问题,各种烦心的异常.这次蹭着这个机会来说说我是怎么集成的.
先贴上我的pom.xml相关依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hive</groupId>
<artifactId>hive-shims</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
连接数据库肯定需要DataSource配置,我这里用的阿里系的.
@Configuration
@ConfigurationProperties(prefix = "hive")
public class HiveDruidConfig { private String url;
private String user;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize; @Bean(name = "hiveDruidDataSource")
@Qualifier("hiveDruidDataSource")
public DruidDataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(user);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName); // pool configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return datasource;
} // 此处省略各个属性的get和set方法 @Bean(name = "hiveDruidTemplate")
public JdbcTemplate hiveDruidTemplate(@Qualifier("hiveDruidDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
配置完成以后,我们就需要写工具类了
HiveRepository.java
@Service
public class HiveRepository{ @Autowired
private JdbcTemplate hiveJdbcTemplate; /**
* <li>Description: TODO </li>
*/
@PostConstruct
public void createTable() {
/*建表SQL语句*/
StringBuffer sql = new StringBuffer("create table IF NOT EXISTS ");
sql.append("bus_receiver ");
sql.append("(id BIGINT comment '主键ID' " +
",name STRING comment '姓名' " +
",address STRING comment '地址'" +
",en_name STRING comment '拼音名字'" +
",member_family INT comment '家庭成员'" +
",createDate DATE comment '创建时') ");
sql.append(" ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'"); // 定义分隔符
sql.append(" STORED AS TEXTFILE"); // 作为文本存储*/
hiveJdbcTemplate.execute(sql.toString());
} /**
* <li>Description: TODO </li>
*
* @param pathFile TODO
*/
public void loadData(String pathFile){
String sql = "LOAD DATA INPATH '"+pathFile+"' INTO TABLE bus_receiver";
hiveJdbcTemplate.execute(sql);
} /**
* <li>Description: TODO </li>
*
* @param busReceiverEntity 实体
*/
public void insert(BusReceiverEntity busReceiverEntity) {
hiveJdbcTemplate.update("insert into bus_receiver(id,name,address,en_name,member_family) values(?,?,?,?,?)",
new PreparedStatementSetter(){
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, busReceiverEntity.getId());
ps.setString(2,busReceiverEntity.getName());
ps.setString(3,busReceiverEntity.getAddress());
ps.setString(4,busReceiverEntity.getEnName());
ps.setInt(5,busReceiverEntity.getMemberFamily());
}
}
);
} public void deleteAll(){
String sql = "insert overwrite table bus_receiver select * from bus_receiver where 1=0";
hiveJdbcTemplate.execute(sql);
}
}
最后贴上配置文件:
hive:
url: jdbc:hive2://XXX:10000/test
driver-class-name: org.apache.hive.jdbc.HiveDriver
filters: stat
initialSize: 2
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: false
maxPoolPreparedStatementPerConnectionSize: 200
需要注意的是,再启动项目的时候需要将servlet-api放到JAVA_HOME/jre/lib/ext目录下 以上方案并非完全原创.
SpringBoot 2.X集成Hive-jdbc 3.1.1的更多相关文章
- spark集成hive遭遇mysql check失败的问题
问题: spark集成hive,启动spark-shell或者spark-sql的时候,报错: INFO MetaStoreDirectSql: MySQL check failed, assumin ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- SpringBoot(七):集成DataSource 与 Druid监控配置
绑定DataSource:Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource,Druid是Java语言中最好的数据库连接池,并且能够提供 ...
- 高可用Hadoop平台-集成Hive HAProxy
1.概述 这篇博客是接着<高可用Hadoop平台>系列讲,本篇博客是为后面用 Hive 来做数据统计做准备的,介绍如何在 Hadoop HA 平台下集成高可用的 Hive 工具,下面我打算 ...
- Springboot 和 Mybatis集成开发
Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...
- 3.12-3.16 Hbase集成hive、sqoop、hue
一.Hbase集成hive https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration 1.说明 Hive与HBase整合在一起 ...
- SpringBoot系列之集成Druid配置数据源监控
SpringBoot系列之集成Druid配置数据源监控 继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven Intel ...
- SpringBoot系列之集成Mybatis教程
SpringBoot系列之集成Mybatis教程 环境准备:IDEA + maven 本博客通过例子的方式,介绍Springboot集成Mybatis的两种方法,一种是通过注解实现,一种是通过xml的 ...
- SpringBoot系列之集成logback实现日志打印(篇二)
SpringBoot系列之集成logback实现日志打印(篇二) 基于上篇博客SpringBoot系列之集成logback实现日志打印(篇一)之后,再写一篇博客进行补充 logback是一款开源的日志 ...
随机推荐
- Java VisualVM无法检测到本地java程序 的 解决办法
win10系统下启动jvisualvm应用,报"VisualVM无法检测到本地java程序"的错误!在网上查了一些方法, 大概原因有2种: 1.操作系统的临时文件目录所在的磁盘格式 ...
- 巩固java(三)---java修饰符
正文: 下面的表格列出了java中修饰符的一些信息: 修饰符名称 类型 类 变量 方法 abstract 非访问控制符 抽象类 -- 抽象方法 final ...
- Hbase中HMaster作用
HMaster在功能上主要负责Table表和HRegion的管理工作,具体包括: 1.管理用户对Table表的增.删.改.查操作: 2.管理HRegion服务器的负载均衡,调整HRegion分布: 3 ...
- java-直接选择排序
直接选择排序是一种简单的排序方法,它每次从当前待排序的区间中选择出最小的元素,把该元素与该区间的第一个元素交换. 第一次从a[0]~a[n-1]中选取最小值,与a0]交换,第二次从a[1]~a[n-1 ...
- IntelliJ IDEA(十) :常用操作
IDEA功能详细,快捷键繁多,但是实际开发时不是所有都能用上,如果我们熟悉一些常用的也足够满足我们日常开发了,多的也只是提高我们的B格. 1.自定义主题 IDEA默认的主题有三款,分别是Intelli ...
- java集合框架之HashMap
参考http://how2j.cn/k/collection/collection-hashmap/365.html#nowhere HashMap的键值对 HashMap储存数据的方式是-- 键值对 ...
- Keepalived + nginx实现高可用性和负载均衡
在前面的一篇中讲到了Heartbeat作为高可用服务架构的解决方案,今天有试验了一种全新的解决方案,即采用Keepalived来实现这个功能. Keepalived 是一种高性能的服务器高可用或热备解 ...
- nodejs-5.1 ejs模板引擎
ejs官方文档:https://ejs.bootcss.com/ 1.什么是 EJS? "E" 代表 "effective",即[高效]. EJS 是一套简单的 ...
- Spring Cloud Feign的文件上传实现
在Spring Cloud封装的Feign中并不直接支持传文件,但可以通过引入Feign的扩展包来实现,本来就来具体说说如何实现. 原文:http://blog.didispace.com/sprin ...
- TCP报文解析
概述 在<网络基础总结(一)>总结了TCP建立连接和断开连接的流程,然而TCP协议远比我所了解的复杂得多,我所知的可以说就冰山一角,所总结的也只是纸上谈兵,仅仅只能对TCP有个肤浅的认识, ...