简介
基于HDFS上的数据分析引擎
支持SQL子集
架构
安装
嵌入模式
不需要MySQL,使用Hive自带的Derby数据库存储元信息(Derby较小,嵌入到Hive中)
* 只支持一个连接,用于开发和测试
本地模式
远程模式
文件存储
分类
Hive
性能分析
数据模型
1 create table emp_part
2 (empno int,
3 ename string,
4 job string,
5 mgr int,
6 hiredate string,
7 sal int,
8 comm int
9 )partitioned by (deptno int)
10 row format delimited fields terminated by ',';
执行
JDBC查询
启动 hive thrift 服务
将hive lib下的jar包下载到本地,添加依赖
JDBCUtils.java
1 package jdbc;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 /*
10 * 工具类:
11 * 1、获取数据库Hive链接
12 * 2、释放资源
13 */
14 public class JDBCUtils {
15
16 //Hive数据库的驱动
17 private static String driver = "org.apache.hive.jdbc.HiveDriver";
18
19 //Hive位置
20 private static String url = "jdbc:hive2://192.168.174.111:7777/default";
21
22 //注册驱动
23 static{
24 try {
25 Class.forName(driver);
26 } catch (ClassNotFoundException e) {
27 e.printStackTrace();
28 throw new ExceptionInInitializerError(e);
29 }
30 }
31
32 //获取链接
33 public static Connection getConnection(){
34 try {
35 return DriverManager.getConnection(url);
36 } catch (SQLException e) {
37 e.printStackTrace();
38 }
39 return null;
40 }
41
42 public static void release(Connection conn,Statement st,ResultSet rs){
43 if(rs != null){
44 try {
45 rs.close();
46 } catch (SQLException e) {
47 e.printStackTrace();
48 }finally{
49 rs = null;
50 }
51 }
52
53 if(st != null){
54 try {
55 st.close();
56 } catch (SQLException e) {
57 e.printStackTrace();
58 }finally{
59 st = null;
60 }
61 }
62
63 if(conn != null){
64 try {
65 conn.close();
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }finally{
69 conn = null;
70 }
71 }
72 }
73 }
TestHive.java
1 package jdbc;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.Statement;
6
7 public class TestHive {
8
9 public static void main(String[] args) {
10 //查询: select * from emp1;
11 Connection conn = null;
12 Statement st = null;
13 ResultSet rs = null;
14 try{
15 conn = JDBCUtils.getConnection();
16 st = conn.createStatement();
17
18 //执行SQL
19 rs = st.executeQuery("select * from emp1");
20 while(rs.next()){
21 String ename = rs.getString("ename");
22 System.out.println(ename);
23 }
24 }catch(Exception ex){
25 ex.printStackTrace();
26 }finally{
27 //释放资源
28 JDBCUtils.release(conn, st, rs);
29 }
30 }
31 }
自定义函数
MyConcatString
1 package demo.udf;
2
3 import org.apache.hadoop.hive.ql.exec.UDF;
4
5 public class MyConcatString extends UDF{
6 public String evaluate(String a,String b) {
7 return a+"**************"+b;
8 }
9 }
CheckSalaryGrade
1 package demo.udf;
2
3 import org.apache.hadoop.hive.ql.exec.UDF;
4
5 public class CheckSalaryGrade extends UDF{
6 public String evaluate(String salary) {
7 int sal = Integer.parseInt(salary);
8 if(sal < 1000) return "Grade A";
9 else if(sal>=1000 && sal<3000) return "Grade B";
10 else return "Grade C";
11 }
12 }
把package打包为jar包,上传到/root/tmp
部署jar包
把jar包加入Hive的Classpath
为自定义函数创建别名
命令
参考
hive报错
https://blog.csdn.net/qq_40048866/article/details/90041277
https://www.cnblogs.com/lijinze-tsinghua/p/8563054.html
https://blog.csdn.net/SunnyYoona/article/details/51648871
beeline
https://www.cnblogs.com/lenmom/p/11218807.html
https://blog.csdn.net/leanaoo/article/details/83351240
Hive set
https://meihuakaile.github.io/2018/10/19/hive-set%E8%AE%BE%E7%BD%AE/
存储格式
https://www.cnblogs.com/wuxiaolong4/p/11809291.html
https://www.cnblogs.com/wujin/p/6208734.html
应用场景
https://zhuanlan.zhihu.com/p/297769662
Hadoop和数据仓库关系
https://blog.csdn.net/qq_42580464/article/details/80953447
http://www.kokojia.com/article/27501.html
https://www.cnblogs.com/zzjhn/p/10563148.html
手机扫一扫
移动阅读更方便
你可能感兴趣的文章