Spring boot with Apache Hive
5.29.1. Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.0</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.20</version>
</dependency>
5.29.2. application.properties
hive 数据源配置项
hive.url=jdbc:hive2://172.16.0.10:10000/default
hive.driver-class-name=org.apache.hive.jdbc.HiveDriver
hive.username=hadoop
hive.password=
用户名是需要具有 hdfs 写入权限,密码可以不用写
如果使用 yaml 格式 application.yml 配置如下
hive:
url: jdbc:hive2://172.16.0.10:10000/default
driver-class-name: org.apache.hive.jdbc.HiveDriver
type: com.alibaba.druid.pool.DruidDataSource
username: hive
password: hive
5.29.3. Configuration
package cn.netkiller.config; 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 HiveConfig {
private static final Logger logger = LoggerFactory.getLogger(HiveConfig.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.username"));
dataSource.setPassword(env.getProperty("hive.password"));
logger.debug("Hive DataSource");
return dataSource;
} @Bean(name = "hiveJdbcTemplate")
public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveJdbcDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
你也可以使用 DruidDataSource
package cn.netkiller.api.config; @Configuration
public class HiveDataSource { @Autowired
private Environment env; @Bean(name = "hiveJdbcDataSource")
@Qualifier("hiveJdbcDataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("hive.url"));
dataSource.setDriverClassName(env.getProperty("hive.driver-class-name"));
dataSource.setUsername(env.getProperty("hive.username"));
dataSource.setPassword(env.getProperty("hive.password"));
return dataSource;
} @Bean(name = "hiveJdbcTemplate")
public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveJdbcDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
} }
5.29.4. CURD 操作实例
Hive 数据库的增删插改操作与其他数据库没有什么不同。
package cn.netkiller.web; import java.util.Iterator;
import java.util.List;
import java.util.Map; 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.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/hive")
public class HiveController {
private static final Logger logger = LoggerFactory.getLogger(HiveController.class); @Autowired
@Qualifier("hiveJdbcTemplate")
private JdbcTemplate hiveJdbcTemplate; @RequestMapping("/create")
public ModelAndView create() { StringBuffer sql = new StringBuffer("create table IF NOT EXISTS ");
sql.append("HIVE_TEST");
sql.append("(KEY INT, VALUE STRING)");
sql.append("PARTITIONED BY (CTIME DATE)"); // 分区存储
sql.append("ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' "); // 定义分隔符
sql.append("STORED AS TEXTFILE"); // 作为文本存储 logger.info(sql.toString());
hiveJdbcTemplate.execute(sql.toString()); return new ModelAndView("index"); } @RequestMapping("/insert")
public String insert() {
hiveJdbcTemplate.execute("insert into hive_test(key, value) values('Neo','Chen')");
return "Done";
} @RequestMapping("/select")
public String select() {
String sql = "select * from HIVE_TEST";
List<Map<String, Object>> rows = hiveJdbcTemplate.queryForList(sql);
Iterator<Map<String, Object>> it = rows.iterator();
while (it.hasNext()) {
Map<String, Object> row = it.next();
System.out.println(String.format("%s\t%s", row.get("key"), row.get("value")));
}
return "Done";
} @RequestMapping("/delete")
public String delete() {
StringBuffer sql = new StringBuffer("DROP TABLE IF EXISTS ");
sql.append("HIVE_TEST");
logger.info(sql.toString());
hiveJdbcTemplate.execute(sql.toString());
return "Done";
}
}
Spring boot with Apache Hive的更多相关文章
- Building Microservices with Spring Boot and Apache Thrift. Part 1 with servlet
https://dzone.com/articles/building-microservices-spring In the modern world of microservices it's i ...
- Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services
http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html In previous article I showed y ...
- Spring Boot 整合 Apache Dubbo
Apache Dubbo是一款高性能.轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. 注意,是 Apache Dubb ...
- Spring Boot 整合 Apache Ignite
关于Ignite的介绍,这边推荐三个链接进行学习了解. https://ignite.apache.org/,首选还是官网,不过是英文版,如果阅读比较吃力可以选择下方两个链接. https://www ...
- spring boot 集成 Apache CXF 调用 .NET 服务端 WebService
1. pom.xml加入 cxf 的依赖 <dependency> <groupId>org.apache.cxf</groupId> <artifactId ...
- spring boot面试问题集锦
译文作者:david 原文链接:https://www.javainuse.com/spring/SpringBootInterviewQuestions Q: 什么是spring boot? A: ...
- (转)Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理
http://www.ityouknow.com/springboot/2017/06/26/spring-boot-shiro.html 这篇文章我们来学习如何使用 Spring Boot 集成 A ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- Spring Boot 针对 Java 开发人员的安装指南
Spring Boot 可以使用经典的开发工具或者使用安装的命令行工具.不管使用何种方式,你都需要确定你的 Java 版本为 Java SDK v1.8 或者更高的版本.在你开始安装之前,你需要确定你 ...
随机推荐
- 配置github的SSH key及GitHub项目上传方式一——使用终端命令行
GitHub是一个开源的大仓库,我们经常从github上下载项目进行学习和研究,下面是一个完整的步骤——往GitHub上传一个新项目. 一.注册GitHub账号 1.注册GitHub账号,地址:htt ...
- 雷林鹏分享:Ruby 数组(Array)
Ruby 数组(Array) Ruby 数组是任何对象的有序的.整数索引的集合.数组中的每个元素都与一个索引相关,并可通过索引进行获取. 数组的索引从 0 开始,这与 C 或 Java 中一样.一个负 ...
- poj3436网络流之最大流拆点
这题看了半天看不懂题意...还是看的网上题意写的 加一个源点一个汇点,把每个点拆成两个,这两个点的流量是v,其他联通的边都设为无穷大 输入没有1的点就与源点连接,输出只有1的点就与汇点连接 还有这个输 ...
- SCWS中文分词PHP扩展详细安装说明
因最近写的一段代码,需要用到中文分词,在网上找了一下,发现了scws这个不错的插件,故根据文档安装使用,下面记录下安装的全过程 系统:centos 安装scws wget http://www.xun ...
- Netty自定义Encoder/Decoder进行对象传递
转载:http://blog.csdn.net/top_code/article/details/50901623 在上一篇文章中,我们使用Netty4本身自带的ObjectDecoder,Objec ...
- npm设置仓库
如果国外官方的npm仓库下载速度很慢的话,可以考虑更换npm仓库,加快下载包的速度. 1.通过config命令 npm config set registry https://registry.npm ...
- Java第七次作业--图形用户界面
Deadline: 2017-5-11 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 了解GUI开发的相关原理和技巧 熟悉Swint组件的使用 理解事件处理模型 二.作业要求 发布 ...
- [QT][SQLITE]学习记录一 querry 查询
使用 QSqlQuery query ; query("SELECT id FROM TABLE1 WHERE id = '2017'); 的到的结果集就是query本身,此时需要使用 qu ...
- 华为荣耀7i手动更改DNS,提高网页加载速度
为什么在同样的Wi-Fi网络下,别人的手机可以秒开网页,但自己的手机却总会慢个半拍或是经常打不开,简直龟速.有时还会加载网页失败.我想大部分人都遇到过吧. 今天本人给大家介绍一种方法,可以加快打开网页 ...
- 6-18 Two Stacks In One Array(20 分)
Write routines to implement two stacks using only one array. Your stack routines should not declare ...