【云计算】SpringBoot整合hive-jdbc教程
小标 2019-01-14 来源 : 阅读 2787 评论 0

摘要:本文主要向大家介绍了【云计算】SpringBoot整合hive-jdbc教程,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】SpringBoot整合hive-jdbc教程,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。


本文将对如何在Springboot项目中整合hive-jdbc进行简单示例和介绍,项目的完整目录层次如下图所示。



添加依赖与配置


首先,需要在工程POM文件中引入hive-jdbc所需的Maven依赖。


  
   org.springframework.boot
   spring-boot-starter-web
  
  
   com.alibaba
   druid-spring-boot-starter
   1.1.1
  
  
   org.springframework.boot
   spring-boot-starter-jdbc
  
  
   org.springframework.data
   spring-data-hadoop
   2.5.0.RELEASE
  
  
   org.apache.hive
   hive-jdbc
   2.3.3
   
    
     org.eclipse.jetty.aggregate
     *
    
   
  
  
   org.apache.tomcat
   tomcat-jdbc
  
  
   jdk.tools
   jdk.tools
   1.8
   system
   ${JAVA_HOME}/lib/tools.jar
  


然后,在核心配置文件application.yml中添加数据源相关配置。


hive:
  url: jdbc:hive2://172.16.250.234:10000/hive
  driver-class-name: org.apache.hive.jdbc.HiveDriver
  type: com.alibaba.druid.pool.DruidDataSource
  user: hadoop
  password: Pure@123
  # 下面为连接池的补充设置,应用到上面所有数据源中
  # 初始化大小,最小,最大
  initialSize: 1
  minIdle: 3
  maxActive: 20
  # 配置获取连接等待超时的时间
  maxWait: 60000
  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  timeBetweenEvictionRunsMillis: 60000
  # 配置一个连接在池中最小生存的时间,单位是毫秒
  minEvictableIdleTimeMillis: 30000
  validationQuery: select 1
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false
  # 打开PSCache,并且指定每个连接上PSCache的大小
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20


配置数据源与JdbcTemplate


我们可以使用SpringBoot默认的 org.apache.tomcat.jdbc.pool.DataSource 数据源,并使用这个数据源装配一个JdbcTemplate。



import org.apache.tomcat.jdbc.pool.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class HiveJdbcConfig {

 private static final Logger logger = LoggerFactory.getLogger(HiveJdbcConfig.class);

 @Autowired
 private Environment env;

 @Bean(name = "hiveJdbcDataSource")
 @Qualifier("hiveJdbcDataSource")
 public DataSource dataSource() {
  DataSource dataSource = new DataSource();
  dataSource.setUrl(env.getProperty("hive.url"));
  dataSource.setDriverClassName(env.getProperty("hive.driver-class-name"));
  dataSource.setUsername(env.getProperty("hive.user"));
  dataSource.setPassword(env.getProperty("hive.password"));
  logger.debug("Hive DataSource Inject Successfully...");
  return dataSource;
 }

 @Bean(name = "hiveJdbcTemplate")
 public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveJdbcDataSource") DataSource dataSource) {
  return new JdbcTemplate(dataSource);
 }

}


我们也可以使用数据源,本例中使用的是Druid数据源,其配置内容如下。


import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

@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 maxActive;
 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 DataSource 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.setMaxActive(maxActive);
  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);
 }

}


使用DataSource操作 Hive


import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用 DataSource 操作 Hive
 */
@RestController
@RequestMapping("/hive")
public class HiveDataSourceController {

 private static final Logger logger = LoggerFactory.getLogger(HiveDataSourceController.class);

 @Autowired
 @Qualifier("hiveJdbcDataSource")
 org.apache.tomcat.jdbc.pool.DataSource jdbcDataSource;

 @Autowired
 @Qualifier("hiveDruidDataSource")
 DataSource druidDataSource;

 /**
  * 列举当前Hive库中的所有数据表
  */
 @RequestMapping("/table/list")
 public List listAllTables() throws SQLException {
  List list = new ArrayList();
  // Statement statement = jdbcDataSource.getConnection().createStatement();
  Statement statement = druidDataSource.getConnection().createStatement();
  String sql = "show tables";
  logger.info("Running: " + sql);
  ResultSet res = statement.executeQuery(sql);
  while (res.next()) {
   list.add(res.getString(1));
  }
  return list;
 }

 /**
  * 查询Hive库中的某张数据表字段信息
  */
 @RequestMapping("/table/describe")
 public List describeTable(String tableName) throws SQLException {
  List list = new ArrayList();
  // Statement statement = jdbcDataSource.getConnection().createStatement();
  Statement statement = druidDataSource.getConnection().createStatement();
  String sql = "describe " + tableName;
  logger.info("Running: " + sql);
  ResultSet res = statement.executeQuery(sql);
  while (res.next()) {
   list.add(res.getString(1));
  }
  return list;
 }

 /**
  * 查询指定tableName表中的数据
  */
 @RequestMapping("/table/select")
 public List selectFromTable(String tableName) throws SQLException {
  // Statement statement = jdbcDataSource.getConnection().createStatement();
  Statement statement = druidDataSource.getConnection().createStatement();
  String sql = "select * from " + tableName;
  logger.info("Running: " + sql);
  ResultSet res = statement.executeQuery(sql);
  List list = new ArrayList();
  int count = res.getMetaData().getColumnCount();
  String str = null;
  while (res.next()) {
   str = "";
   for (int i = 1; i < count; i++) {
    str += res.getString(i) + " ";
   }
   str += res.getString(count);
   logger.info(str);
   list.add(str);
  }
  return list;
 }

}


使用 JdbcTemplate 操作 Hive


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用 JdbcTemplate 操作 Hive
 */
@RestController
@RequestMapping("/hive2")
public class HiveJdbcTemplateController {

 private static final Logger logger = LoggerFactory.getLogger(HiveJdbcTemplateController.class);

 @Autowired
 @Qualifier("hiveDruidTemplate")
 private JdbcTemplate hiveDruidTemplate;

 @Autowired
 @Qualifier("hiveJdbcTemplate")
 private JdbcTemplate hiveJdbcTemplate;

 /**
  * 示例:创建新表
  */
 @RequestMapping("/table/create")
 public String createTable() {
  StringBuffer sql = new StringBuffer("CREATE TABLE IF NOT EXISTS ");
  sql.append("user_sample");
  sql.append("(user_num BIGINT, user_name STRING, user_gender STRING, user_age INT)");
  sql.append("ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' "); // 定义分隔符
  sql.append("STORED AS TEXTFILE"); // 作为文本存储

  logger.info("Running: " + sql);
  String result = "Create table successfully...";
  try {
   // hiveJdbcTemplate.execute(sql.toString());
   hiveDruidTemplate.execute(sql.toString());
  } catch (DataAccessException dae) {
   result = "Create table encounter an error: " + dae.getMessage();
   logger.error(result);
  }
  return result;

 }

 /**
  * 示例:将Hive服务器本地文档中的数据加载到Hive表中
  */
 @RequestMapping("/table/load")
 public String loadIntoTable() {
  String filepath = "/home/hadoop/user_sample.txt";
  String sql = "load data local inpath '" + filepath + "' into table user_sample";
  String result = "Load data into table successfully...";
  try {
   // hiveJdbcTemplate.execute(sql);
   hiveDruidTemplate.execute(sql);
  } catch (DataAccessException dae) {
   result = "Load data into table encounter an error: " + dae.getMessage();
   logger.error(result);
  }
  return result;
 }

 /**
  * 示例:向Hive表中添加数据
  */
 @RequestMapping("/table/insert")
 public String insertIntoTable() {
  String sql = "INSERT INTO TABLE  user_sample(user_num,user_name,user_gender,user_age) VALUES(888,'Plum','M',32)";
  String result = "Insert into table successfully...";
  try {
   // hiveJdbcTemplate.execute(sql);
   hiveDruidTemplate.execute(sql);
  } catch (DataAccessException dae) {
   result = "Insert into table encounter an error: " + dae.getMessage();
   logger.error(result);
  }
  return result;
 }

 /**
  * 示例:删除表
  */
 @RequestMapping("/table/delete")
 public String delete(String tableName) {
  String sql = "DROP TABLE IF EXISTS "+tableName;
  String result = "Drop table successfully...";
  logger.info("Running: " + sql);
  try {
   // hiveJdbcTemplate.execute(sql);
   hiveDruidTemplate.execute(sql);
  } catch (DataAccessException dae) {
   result = "Drop table encounter an error: " + dae.getMessage();
   logger.error(result);
  }
  return result;
 }
}


启动测试


通过运行HiveApplication类的main方法启动项目,接下来对每个示例方法进行测试。


待项目启动后,在浏览器中访问 //localhost:8080/hive2/table/create来创建一张 user_sample 测试表:


user_sample 表的创建 sql 如下:


    create table user_sample
    ( 
        user_num bigint, 
        user_name string, 
        user_gender string, 
        user_age int
    ) row format delimited fields terminated by ',';


测试表创建完成后,通过访问 //localhost:8080/hive/table/list来查看hive库中的数据表都有哪些?


返回如下内容:



在Hive客户端中使用 show tables 命令查看,与浏览器中看到的数据表相同,内容如下:



访问 //localhost:8080/hive/table/describetableName=user_sample来查看user_sample表的字段信息:


返回如下内容:



在Hive客户端中使用 describe user_sample命令进行查看,与浏览器中看到的数据表字段相同。



接下来进行数据导入测试,先在Hive服务器的/home/hadoop/ 目录下新建一个user_sample.txt 文件,内容如下:


622,Lee,M,25
633,Andy,F,27
644,Chow,M,25
655,Grace,F,24
666,Lily,F,29
677,Angle,F,23


然后在浏览器中访问以下地址,将/home/hadoop/user_sample.txt 文件中的内容加载到user_sample数据表中。


//localhost:8080/hive2/table/load


数据导入成功之后,访问//localhost:8080/hive/table/selecttableName=user_sample,返回如下内容:



在Hive客户端中使用 select * form user_sample命令进行查看,与浏览器中看到的内容相同。



再访问 //localhost:8080/hive2/table/insert来测试向 user_sample 表中插入一条数据。


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved