一、Hive的概述
1、Hive的定義
Hive是基於Hadoop的一個數據倉庫工具,可以將結構化的數據文件映射為一張數據庫表,並提供類SQL進行數據讀取、寫入和管理。
2、Hive的架構圖
hive的各個組成部分介紹:
- 用戶接口:包括 CLI、JDBC/ODBC、WebGUI。
- 元數據存儲:通常是存儲在關系數據庫如 mysql , derby中。
- 用戶接口主要由三個:CLI、JDBC/ODBC和WebGUI。其中,CLI為shell命令行;JDBC/ODBC是Hive的JAVA實現,與傳統數據庫JDBC類似;WebGUI是通過瀏覽器訪問Hive。
- 元數據存儲:Hive 將元數據存儲在數據庫中。Hive 中的元數據包括表的名字,表所屬的數據庫,表的列和分區及其屬性(是否為外部表),表的數據所在目錄等。
- 解釋器、編譯器、優化器完成 HQL 查詢語句從詞法分析、語法分析、編譯、優化以及查詢計划的生成。生成的查詢計划存儲在 HDFS 中,並在隨后有 MapReduce 調用執行。
3、Hive與Hadoop的關系
a、Hive的數據是存放在Hadoop之上的;
b、Hive的數據分為兩個部分:數據+元數據;元數據是記錄數據的數據,這里記錄着表名、所屬的數據庫、列(名/類型/index)、表類、表數據和分區及其在hadoop的目錄。
c、Hive將SQL進行解析,然后開啟MR任務在Hadoop上運行。
4、Hive的執行引擎
hive的執行引擎包括:MapReduce、Tez和Spark;只要通過一個參數就能夠切換底層的執行引擎。
5、Hive的參數配置
1)元數據庫連接配置
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://XXX:3306/databaseName?createDatabaseIfNotExist=true</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
2)顯示當前使用數據庫和表結構名稱配置
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
3)也可以使用set hive.cli.print.current.db = true的方式,使之在當前界面生效。
6、hive和RDBMS的關系
- 相同點:都是使用sql,都支持事務(hive用的很少)和支持insert/update/delete。
- 不同點:1)體量不同/集群規模 2)延遲/時性
二、Hive的常用命令
1、創建數據庫
a、Create database if not exists test;
自己創建的數據庫,默認存放在:/user/hive/warehouse/test.db目錄中
b、創建數據庫,同時制定目錄
create database if not exists test location ‘/test/directory’;
c、創建數據庫,同時添加描述信息
create database if not exists test comment ‘this is a database’ With DBPROPERTIES('creator'='me','date'='9012-12-17')
2、Hive常用的數據類型
數值類型:int bigint float double decimal
字符串類型:Stirng 90%
布爾類型: boolean:true false
日期類型:date timestamp
3、Hive的表格類型
Hive的表格分為內部表和外部表。
內部表刪除時:HDFS的數據和Meta都會被刪除;
外部表刪除時:HDFS的數據不會被刪除,只是Meta上的數據會被刪除,安全起見,最好創建外部表。
a、創建外部表的格式
create external table emp_external (empno int,ename string,job string,mgr int,hiredate string,sal double, comm double,deptno int) row format DELIMITED FIELDS TERMINATED BY '\t' Location '/test/externaltable/emp';
加載數據到外部表
Load data Local inpath '/home/hadoop/data/emp.txt' into table emp;
b、創建內部表
- 只是創建表結構,不包含數據: create table emp2 like emp;
- 既創建表結構,也包含數據:create table emp3 like emp;
4、Hive的表的分區和分桶
- 分區列並不是一個“真正的”表字段,其實是HDFS上表對應的文件下的一個文件夾
create table order_partition(order_no string,event_time string) partitioned by(event_month string)ROW Format Delimited Fields terminated By '\t';
Load DATA Local inpath '/home/hadoop/data/order_created.txt' Into table order_partition PARTITION(event_month='2015-05');
- 分區表加載數據的時候一定要指定分區字段;對於分區表操作,如果你的數據是寫入到HDFS中,默認SQL是查詢不到的,因為元數據中沒有;如果想用SQL查詢,需要添加表分區
Alter table order_partition ADD if not exists Partition(event_month='2015-07');
- 多級分區:為表格指定多個分區
create table order_mulit_partition(order_no String,event_time string) Partition by(event_month String,step String) Row format Delimited Fields Terminated By '\t';
Load Data Local Inpath '/home/hadoop/data/order_mulit_partition Into Table order_mulit_partition Partition(event_month='2014-06',step='2');
- 使用分區表時,加載數據一定要指定我們的所有分區字段
Create table emp_parititon(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double) Partition By(deptno int) Row Format Delimited Fields Terminated By '\t';
Insert Overwrite Table emp_partition Partition(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm form emp where deptno=10;
- 動態分區
Create table emp_dynamic_partition(empno int,ename string,job string,mgr int,hiredate string, sal double,comm double) Partition by(deptno int) Row Format Delimited Fields Terminated By '\t';
Insert Overwrite Table emp_dynamic_partition Partition(deptno) select empno,ename,job,mgr,hiredate,sal comm form emp;
5、幾個關鍵字介紹
Load Data Local Inpath 'home/hadoop/data/order_created.txt' [Overwrite] into table order_mulit_parititon Partition (event_month='2014-05',step='1')
- Load Data:加載數據
- INPATH:指定路徑
- Overwrite:數據覆蓋,沒有的話就是追加。
6、數據導入的另外兩種方式
- CTAS:表不能實現存在(create table .. as select ..)
- Insert方式:表必須事先存在,表格字段必須順序書寫,不能亂 ,否則查詢時會出錯,找不到該字段
Insert overwrite table emp4 select empo,job,ename,mgr hiredate,sal,comm,deptno from emp;
from emp insert into table emp4 select *;
7、分組查詢時注意的事項
- where只是對單項進行限制,分組限制要使用hiving
- select 中出現的字段,如果沒有出現在group by 中,則必須出現在聚合函數中
select deptno,avg(sal) avg_sal from emp group by deptno having avg_sal >2000;
8、Hive中MR的使用設置
通過設置hive中的參數可以部分限制MR的使用級別,默認Select * from table;是不用走mapReduce的;涉及到多對一的聚合函數:多進一出,則必然使用MapReduce,比如:max,min,count,sum,avg。