Hive是構(gòu)建在Hadoop HDFS上得一個(gè)數(shù)據(jù)倉(cāng)庫(kù)
數(shù)據(jù)倉(cāng)庫(kù)是一個(gè)面向主題的右蹦、集成的瘤旨、不可更新的、隨時(shí)間不變化的數(shù)據(jù)集合富弦,它用于支持企業(yè)或組織的決策分析處理
搭建數(shù)據(jù)倉(cāng)庫(kù)的時(shí)候最基本的兩個(gè)模型:星型模型和雪花模型(雪花是在星型的基礎(chǔ)上發(fā)展起來(lái)的)
OLTP應(yīng)用(比如銀行轉(zhuǎn)賬)灵嫌、OLAP應(yīng)用(比如商品推薦系統(tǒng))
Hive是建立在Hadoop HDFS上的數(shù)據(jù)倉(cāng)庫(kù)
Hive可以用來(lái)進(jìn)行數(shù)據(jù)抽取轉(zhuǎn)換加載(ETL)
Hive定義了簡(jiǎn)單的類似SQL的查詢語(yǔ)言壹罚,稱為HQL,它允許熟悉SQL的用戶查詢數(shù)據(jù)
Hive允許熟悉MapReduce開(kāi)發(fā)者開(kāi)發(fā)自定義的mapper和reducer來(lái)處理內(nèi)建的maprper和reducer無(wú)法完成的復(fù)雜的分析工作
Hive是SQL(其實(shí)是HQL)解析引擎寿羞,他將SQL語(yǔ)句轉(zhuǎn)移成M/R Job猖凛,然后在Hadoop執(zhí)行
Hive的表其實(shí)就是HDFS的目錄/文件
-
Hive的元數(shù)據(jù)
Hive將元數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫(kù)中(metastore),支持mysql/derby等數(shù)據(jù)庫(kù)
Hive中的元數(shù)據(jù)包括表的名字、表的列和分區(qū)及其屬性绪穆,表的屬性(是否為外部表等)辨泳,表的數(shù)據(jù)所在目錄等
-
HQL的執(zhí)行過(guò)程:
解釋器、編譯器玖院、優(yōu)化器完成HQL查詢語(yǔ)句從詞法分析菠红、語(yǔ)法分析、編譯难菌、優(yōu)化以及查詢計(jì)劃(Plan)的生成途乃。生成的查詢計(jì)劃存儲(chǔ)在HDFS中,并在隨后又MapReduce調(diào)用執(zhí)行
下載apache已發(fā)布版(release)安裝包網(wǎng)站:http://archive.apache.org/
-
Hive的安裝模式:
- 嵌入模式:
元數(shù)據(jù)信息被存儲(chǔ)在Hive自帶的Derby數(shù)據(jù)庫(kù)中
只允許創(chuàng)建一個(gè)鏈接
多用于Demo - 本地模式:
元數(shù)據(jù)信息被存儲(chǔ)在MySql數(shù)據(jù)庫(kù)中
MySql數(shù)據(jù)庫(kù)與Hive允許在同一臺(tái)物理機(jī)器上
多用于開(kāi)發(fā)和測(cè)試 - 遠(yuǎn)程模式:
Hive和MySql運(yùn)行在不同機(jī)器(操作系統(tǒng))上
- 嵌入模式:
-
Hive的啟動(dòng)方式:
CLI(命令行)方式:
<HIVE_HOME>/bin/hive或者h(yuǎn)vie --service cli
常用CLI命令:
Ctrl+L或者!clear 清屏
show tables; //show tables --查看表列表扔傅;
show functions;
desc 表名;
dfs -ls 目錄 //查看hdfs上得文件
!命令 //執(zhí)行l(wèi)inux操作系統(tǒng)命令
select *** from *** //執(zhí)行hql語(yǔ)句
select tname from test1;//會(huì)轉(zhuǎn)換為mapreduce作業(yè)
source SQL文件 //執(zhí)行sql腳本
注:
hive -S表示靜默模式耍共,不會(huì)打印mapreduce作業(yè)等的日志信息
hive -e可以不進(jìn)入命令行,直接在操作系統(tǒng)提示符下執(zhí)行hive語(yǔ)句猎塞,比如:hive -S -e 'show tables';-
Web界面方式
端口號(hào):9999
啟動(dòng)方式:hive --service hwi
通過(guò)瀏覽器訪問(wèn):http://ip:9999/hwi
解壓源碼包hive-1.1.0-cdh5.7.0-src.tar.gz進(jìn)入到hive-1.1.0-cdh5.7.0-src\hive-1.1.0-cdh5.7.0\hwi中使用
jar cvfM0 hive-hwi-1.1.0.war -C web/ . 命令打包得到hive-hwi-1.1.0.war
把war包拷貝到hive的lib目錄下
修改hive-site.xml添加如下配置:<property> <name>hive.hwi.listen.host</name> <value>0.0.0.0</value> <description>This is the host address the Hive Web Interface will listen on</description> </property> <property> <name>hive.hwi.listen.port</name> <value>9999</value> <description>This is the port the Hive Web Interface will listen on</description> </property> <property> <name>hive.hwi.war.file</name> <value>lib/hive-hwi-<version>.war</value> <description>This is the WAR file with the jsp content for Hive Web Interface</description> </property>
拷貝jdk的tools.jar拷貝到hive的lib目錄下
啟動(dòng) hive --service hwi 遠(yuǎn)程服務(wù)啟動(dòng)方式
端口號(hào):10000
啟動(dòng)方式:hive --service hiveserver
(以JDBC或ODBC的程序登錄到hive中操作數(shù)據(jù)時(shí)试读,必須選用遠(yuǎn)程服務(wù)啟動(dòng)方式)
-
Hive的數(shù)據(jù)類型:
- 基本數(shù)據(jù)類型:
tinyint/smallint/int/bigint:整數(shù)類型
float/double:浮點(diǎn)數(shù)類型
boolean:布爾類型
string:字符串類型
create table person(pid int,pname string,married boolean,salary double);
desc person;
create table test1(vname varchar(20),cname char(20));
desc test1; - 復(fù)雜數(shù)據(jù)類型:
Array:數(shù)組類型,由一系列想同數(shù)據(jù)類型的元素組成
Map:集合類型荠耽,包含key-value鍵值對(duì)钩骇,可以通過(guò)key來(lái)訪問(wèn)元素
Struct:結(jié)構(gòu)類型,可以包含不同數(shù)據(jù)類型的元素。這些元素可以通過(guò)“點(diǎn)語(yǔ)法”的方式來(lái)得到所需要的元素
create table student(sid int,sname string,grade array<float>);
插入數(shù)據(jù)時(shí)應(yīng)該是這樣的數(shù)據(jù):{1,Tom,[80,90,75]}
desc student;
create table student2(sid int,sname string,grade map<string,float>);
{1,'Tom',<"語(yǔ)文",88>}
desc student2;
create table student3(sid int,sname string,grades array<map<string,float>>);
desc student3;
{1,'Tom',[<'語(yǔ)文',99>,<'數(shù)學(xué)',88>]}
create table student4(sid int,info struct<name:string,age:int,sex:string>);
{1,{'Tom',10,'男'}} - 時(shí)間類型:
Date:從Hive0.12.0開(kāi)始支持
Timestamp:從Hive0.8.0開(kāi)始支持
使用select unix_timestamp();查詢當(dāng)前系統(tǒng)時(shí)間的時(shí)間戳(偏移量)
- 基本數(shù)據(jù)類型:
-
Hive的數(shù)據(jù)存儲(chǔ):
基于HDFS(默認(rèn)對(duì)應(yīng)于/user/hive/warehouse/下的文件)
沒(méi)有專門的數(shù)據(jù)存儲(chǔ)格式(可以是csv/txt等等)
存儲(chǔ)結(jié)構(gòu)主要包括:數(shù)據(jù)庫(kù)倘屹、文件银亲、表、視圖
可以直接加載文本文件(.txt文件等)
創(chuàng)建表時(shí)纽匙,指定Hive數(shù)據(jù)的列分隔符于行分隔符
其中:表包括Table(內(nèi)部表)务蝠、Partition(分區(qū)表)、External Table(外部表)烛缔、Bucket Table(桶表)內(nèi)部表:
與數(shù)據(jù)庫(kù)中的Table在概念上類似
每個(gè)Table在Hive中都有一個(gè)相應(yīng)的目錄存儲(chǔ)數(shù)據(jù)
所有Table數(shù)據(jù)(不包括External Table)都保存在這個(gè)目錄中
刪除表時(shí)馏段,元數(shù)據(jù)與數(shù)據(jù)都會(huì)被刪除
create table t1(tid int,tname string,age int);//默認(rèn)會(huì)放在/user/hive/warehouse/下邊
create table t2(tid int,tname string,age int) location '/mytable/hive/t2';//手動(dòng)指定位置到/mytable/hive下邊
create table t3(tid int ,tname string,age int) row format delimited fields terminated by ',';//列與列的分隔符用逗號(hào),就可以導(dǎo)入csv文件了
create table t4 as select * from sample_data;
create table t5 row format delimited fields terminated by ',' as select * from sample_data;
alter table t1 add colums(english int);
drop table t1;-
分區(qū)表(Partition)
Partition 對(duì)應(yīng)于數(shù)據(jù)庫(kù)的Partition列的密集索引
在Hive中践瓷,表中的一個(gè)Partition對(duì)應(yīng)于表下的一個(gè)目錄院喜,所有的Partition的數(shù)據(jù)都存儲(chǔ)在對(duì)應(yīng)的目錄中.
創(chuàng)建一張基于性別的分區(qū)表:
create table partition_table (sid int,sname string) partitioned by (gender string) row format delimited fields terminated by ',';
insert into table partition_table partition(gender='M') select sid,sname from sample_data where gender='M';
insert into table partition_table partition(gender='F') select sid,sname from sample_data where gender='F';
有了分區(qū)表,可以對(duì)別沒(méi)有分區(qū)的表晕翠,執(zhí)行計(jì)劃的不同:
explain select * from sample_data where gender='M';
explain select * from partition_table where gender='M'; -
外部表(External Table):
指向已經(jīng)在HDFS中存在的數(shù)據(jù)喷舀,可以創(chuàng)建Partition
它和內(nèi)部表在元數(shù)據(jù)的組織上是相同的,而實(shí)際數(shù)據(jù)的存儲(chǔ)則有較大的差異
外部表只有一個(gè)過(guò)程淋肾,加載數(shù)據(jù)和創(chuàng)建表同時(shí)完成元咙,并不會(huì)移動(dòng)到數(shù)據(jù)倉(cāng)庫(kù)目錄中,只是與外部數(shù)據(jù)建立一個(gè)鏈接巫员。當(dāng)刪除一個(gè)外部表時(shí),僅刪除該鏈接
create external table external_student
(sid int,sname string,age int)
row format delimited fields terminated by ','
location '/input'; 桶表(Bucket Table)
桶表是對(duì)數(shù)據(jù)進(jìn)行哈希取值甲棍,然后放到不同文件中存儲(chǔ)
create table bucket_table
(sid int,sname string,age int)
clustered by (sname) into 5 buckets;視圖(View)
視圖是一種虛表简识,是一個(gè)邏輯概念,可以跨越多張表感猛;
視圖建立在已有表的基礎(chǔ)上七扰,視圖賴以建立的這些表成為基表;
視圖可以簡(jiǎn)化復(fù)雜的查詢
create view empinfo
as
select e.empno,e.ename,e.sal,e.sal*12 annlsal,d.dname
from emp e,dept d
where e.deptno=d.deptno;
select * from empinfo;
-
Hive數(shù)據(jù)導(dǎo)入
-
load
上邊創(chuàng)建的t2和t3表結(jié)構(gòu)是一樣的陪白,只是t2采用的分隔符是默認(rèn)的制表符颈走,t3采用的是逗號(hào)分隔,現(xiàn)有如下數(shù)據(jù):
student01.txt:
1,Tom,23
2,Mary,20
student02.txt:
3,Mike,25
student03.txt
4,Scott,21
5,King,20
將student01.txt數(shù)據(jù)導(dǎo)入t2
load data local inpath '/home/hadoop/data/hive/student01.txt' into table t2; //失敗咱士,因?yàn)榉指舴灰恢?/p>將student01.txt數(shù)據(jù)導(dǎo)入t3
load data local inpath '/home/hadoop/data/hive/student01.txt' into table t3; //成功導(dǎo)入將/home/hadoop/data/hive/下的所有數(shù)據(jù)文件導(dǎo)入t3表中立由,并且覆蓋原來(lái)的數(shù)據(jù):
load data local inpath '/home/hadoop/data/hive/' overwrite into table t3;將HDFS中/input/student01.txt 導(dǎo)入到t3
load data inpath '/input/student01.txt' overwrite into table t3;上邊有一張分區(qū)表叫做partition_table,創(chuàng)建語(yǔ)句是這樣的:
create table partition_table (sid int,sname string) partitioned by (gender string) row format delimited fields terminated by ',';
現(xiàn)有如下數(shù)據(jù):
data1.txt:
1,Tom,M
3,Mike,M
data2.txt:
2,Mary,F
load data local inpath '/home/hadoop/data/hive/data1.txt' into table partition_table partition (gender='M');
load data local inpath '/home/hadoop/data/hive/data2.txt' into table partition_table partition (gender='F'); -
Sqoop(是apache社區(qū)的一個(gè)開(kāi)源框架)
下載sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz并解壓
設(shè)置環(huán)境變量:
export HADOOP_COMMON_HOME=/home/hadoop/app/hadoop-2.6.0-cdh5.7.0
export HADOOP_MAPRED_HOME=/home/hadoop/app/hadoop-2.6.0-cdh5.7.0
具體使用可以查看官網(wǎng)
-
-
Hive的數(shù)據(jù)查詢
--查詢所有員工的所有信息
select * from emp;
--查詢員工信息:?jiǎn)T工號(hào) 姓名 月薪
select empno,ename,sal from emp;
--查詢員工信息:?jiǎn)T工號(hào) 姓名 月薪 年薪
select empno,ename,sal,sal12 from emp;
查詢員工信息:?jiǎn)T工號(hào) 姓名 月薪 年薪 獎(jiǎng)金 年收入
select empno,ename,sal,sal12,comm,sal*12+nvl(com,0) from emp;
--查詢獎(jiǎng)金為null的員工
select * from emp where comm is null;
--使用distinct 來(lái)去掉重復(fù)記錄
select distinct deptno,job from emp; 簡(jiǎn)單查詢的Fetch Task功能序厉,配置后簡(jiǎn)單查詢就不會(huì)生成mapreduce作業(yè)锐膜,從Hive 0.10.0版本開(kāi)始支持
配置方式:
set hive.fetch.task.conversion=more;
hive --hiveconf hive.fetch.task.conversion=more
-
修改hive-site.xml文件
<property>
<name>hive.fetch.task.conversion</name>
<value>more</value>
</property>過(guò)濾查詢:
--查詢10部門的員工
select * from emp where deptno=10;
--查詢名叫KING的員工
select * from emp where ename='KING';
--查詢部門號(hào)是10,薪水小于2000的員工
select * from emp where deptno=10 and sal<2000;
--模糊查詢:查詢名字以S打頭的員工
select empno,ename,sal from emp where ename like 'S%';
--模糊查詢:查詢名字含有下劃線的員工
select empno,ename,sal from emp where ename like '%\\_%';
排序:
-- 查詢員工信息:?jiǎn)T工號(hào) 姓名 月薪 按照月薪排序
select empno,ename,sal from emp order by sal desc;
--order by 后面可以跟:列 弛房,表達(dá)式道盏,別名,序號(hào)
select empno,ename,sal,sal12 annsal from emp order by annsal;
select empno,ename,sal,sal12 annsal from emp order by 4;//需要set hive.groupby.orderby.position.alias=true
--查詢員工信息,按照獎(jiǎng)金排序(null排序:升序在最前面荷逞,降序在最后面)
select empno,ename,sal,comm from emp order by comm desc;
-
hive函數(shù)
包括內(nèi)置函數(shù)和自定義函數(shù)
-
數(shù)學(xué)函數(shù):
舉幾個(gè)例子
round //四舍五入 select round(45,926,2),round(45,926,1),round(45,926,0),round(45,926,-1),round(45,926,-2),
ceil //向上取整 ceil(45.9)
floor //floor(45.9)- 字符函數(shù):
舉幾個(gè)例子
lower // select lower('Hello World'),upper('Hello World');
upper
length //字符數(shù) select length('Hello World'),length('你好');
concat //拼加字符串 select concat('Hello','World');
substr //求子串 select substr('Hello World',3); select substr('Hello World',3,4);
trim //去掉首位空格
lpad //左填充select lpad('abcd',10,''),rpad('abcd',10,'');
rpad //右填充 - 收集函數(shù)和轉(zhuǎn)換函數(shù)
收集函數(shù):
size(map(<key,value>,<key,value>))
select size(map(1,'Tom',2,'Mary'));
轉(zhuǎn)換函數(shù):
cast
select cast(1 as bigint);
select cast(1 as float);
select cast('2015-04-10' as date); - 日期函數(shù)
to_date //select to_date('2015-04-23 11:23:11');
year //select year('2015-04-23 11:23:11'),month('2015-04-23 11:23:11'),day('2015-04-23 11:23:11');
month
day
weekofyear //select weekofyear('2015-04-23 11:23:11');
datediff //select datediff('2015-04-23 11:23:11','2014-04-23 11:23:11')
date_add //select date_add('2015-04-23 11:23:11',2),date_sub('2015-04-23 11:23:11',2)
date_sub - 條件函數(shù)
coalesce:從左到右返回第一個(gè)不為null的值 //select comm,sal,coalesce(comm,sal) from emp;
case...when...:條件表達(dá)式
``` select ename,job,sal, case job when 'PRESIDENT' then sal+1000 when 'MANAGER' then sal+800 else sal+400 end from emp; ``` * 聚合函數(shù) count //select count(*),sum(sal),max(sal),min(sal),avg(sal) from emp; sum min max avg * 表生成函數(shù) explode select explode(map(1,'Tom',2,'Mary',3,'Mike'));
- 字符函數(shù):
Hive的表連接
等值連接媒咳、不等值連接、外連接种远、自連接
select e.empno,e.ename,e.sal,d.dname from emp e,dept d where e.deptno=de.deptno;//等值
select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;//不等值
select d.deptno,d.dname,count(e.empno) from emp e,dept d where e.deptno=d.deptno group by d.deptno,d.dname;
通過(guò)外連接可以將對(duì)于連接條件不成立的記錄仍然包含在最后的結(jié)果中
左外連接涩澡,右外連接
select d.deptno,d.dname,count(e.empno) from emp e right outer join dept d on (e.deptno=d.deptno) group by d.deptno,d.dname;
自連接
select e.ename,b.ename from emp e,emp b where e.mgr=b.empno;Hive子查詢
hive只支持:from和where子句中的子查詢
select e.ename from emp e where e.deptno in (select d.deptno from dept d where d.dname='SALES' or d.dname='ACCOUNTING');
select * from emp e where e.empno not in (select e1.mgr from emp e1 where e1.mgr is not null);Hive的客戶端操作
需要通過(guò)hive --service hiveserver啟動(dòng)Hive遠(yuǎn)程服務(wù)
2種方式:JDBC和Thrift Client