HiveQL詳解


HiveQL是一種類似SQL的語言, 它與大部分的SQL語法兼容, 但是並不完全支持SQL標准, 如HiveQL不支持更新操作, 也不支持索引和事務, 它的子查詢和join操作也很局限, 這是因其底層依賴於Hadoop雲平台這一特性決定的, 但其有些特點是SQL所無法企及的。例如多表查詢、支持create table as select和集成MapReduce腳本等, 本節主要介紹Hive的數據類型和常用的HiveQL操作。

1.hive client命令
a.hive命令參數

-e: 命令行sql語句
-f: SQL文件
-h, --help: 幫助
--hiveconf: 指定配置文件
-i: 初始化文件
-S, --silent: 靜態模式(不將錯誤輸出)
-v,--verbose: 詳細模式

b.交互模式

hive> show tables; #查看所有表名
hive> show tables  'ad*'  #查看以'ad'開頭的表名
hive> set 命令 #設置變量與查看變量;
hive> set -v #查看所有的變量
hive> set hive.stats.atomic #查看hive.stats.atomic變量
hive> set hive.stats.atomic=false #設置hive.stats.atomic變量
hive> dfs  -ls #查看hadoop所有文件路徑
hive> dfs  -ls /user/hive/warehouse/ #查看hive所有文件
hive> dfs  -ls /user/hive/warehouse/ptest #查看ptest文件
hive> source file <filepath> #在client里執行一個hive腳本文件
hive> quit #退出交互式shell
hive> exit #退出交互式shell
hive> reset #重置配置為默認值
hive> !ls #從Hive shell執行一個shell命令

2.操作及函數

查看函數:
hive> show  functions;   
正則查看函數名:
show  functions  'xpath.*';  
查看具體函數內容:
describe function xpath; | desc function  xpath;

3.字段類型
Hive支持基本數據類型和復雜類型, 基本數據類型主要有數值類型(INT、FLOAT、DOUBLE)、布爾型和字符串, 復雜類型有三種:ARRAY、MAP 和 STRUCT。
a.基本數據類型
TINYINT: 1個字節
SMALLINT: 2個字節
INT: 4個字節   
BIGINT: 8個字節
BOOLEAN: TRUE/FALSE  
FLOAT: 4個字節,單精度浮點型
DOUBLE: 8個字節,雙精度浮點型STRING       字符串

b.復雜數據類型
ARRAY: 有序字段
MAP: 無序字段
STRUCT: 一組命名的字段

4.表類型
hive表大致分為普通表、外部表、分區表三種。
a.普通表

創建表
hive> create table tb_person(id int, name string);

創建表並創建分區字段ds
hive> create table tb_stu(id int, name string) partitioned by(ds string);

查看分區
hive> show  partitions tb_stu;

顯示所有表
hive> show tables;

按正則表達式顯示表,
hive> show tables 'tb_*';

表添加一列
hive> alter table tb_person add columns (new_col int);

添加一列並增加列字段注釋
hive> alter table tb_stu add columns (new_col2 int comment 'a comment');

更改表名
hive> alter table tb_stu rename to tb_stu;

刪除表(hive只能刪分區,不能刪記錄或列 )
hive> drop table tb_stu;

對於托管表, drop 操作會把元數據和數據文件刪除掉, 對於外部表, 只是刪除元數據。如果只要刪除表中的數據, 保留表名可以在 HDFS 上刪除數據文件:
hive> dfs –rmr /user/hive/warehouse/mutill1/*

將本地/home/hadoop/ziliao/stu.txt文件中的數據加載到表中, stu.txt文件數據如下:
1 zhangsan
2 lisi
3 wangwu

將文件中的數據加載到表中

hive> load data local inpath '/home/hadoop/ziliao/stu.txt' overwrite into table tb_person;

加載本地數據,同時給定分區信息

hive> load data local inpath '/home/hadoop/ziliao/stu.txt' overwrite into table tb_stu partition (ds='2008-08-15');

備注:如果導入的數據在 HDFS 上,則不需要 local 關鍵字。托管表導入的數據文件可在數據倉庫目錄“user/hive/warehouse/<tablename>”中看到。

查看數據

hive> dfs -ls /user/hive/warehouse/tb_stu
hive> dfs -ls /user/hive/warehouse/tb_person

b.外部表
external關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際數據的路徑(location),hive創建內部表時,會將數據移動到數據倉庫指向的路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據。
eg. 創建外部表:

create external table tb_record(col1 string, col2 string) row format delimited fields terminated by '\t' location '/user/hadoop/input';

這樣表tb_record的數據就是hdfs://user/hadoop/input/* 的數據了。

c.分區表
分區是表的部分列的集合, 可以為頻繁使用的數據建立分區, 這樣查找分區中的數據時就不需要掃描全表, 這對於提高查找效率很有幫助。
創建分區:create table log(ts bigint,line string) partitioned by(name string);
插入分區:insert overwrite table log partition(name='xiapi') select id from userinfo where name='xiapi';
查看分區:show  partitions log;
刪除分區: alter table ptest drop partition (name='xiapi')
備注:通常情況下需要先預先創建好分區,然后才能使用該分區。還有分區列的值要轉化為文件夾的存儲路徑,所以如果分區列的值中包含特殊值,如 '%', ':', '/', '#',它將會被使用%加上 2 字節的 ASCII 碼進行轉義。

5. sql操作及桶
1). 創建表
首先建立三張測試表:
userinfo表中有兩列,以tab鍵分割,分別存儲用戶的id和名字name;
classinfo表中有兩列,以tab鍵分割,分別存儲課程老師teacher和課程名classname;
choice表中有兩列,以tab鍵分割,分別存儲用戶的userid和選課名稱classname(類似中間表)。

創建測試表:

hive> create table userinfo(id int,name string) row format delimited fields terminated by '\t';
hive> create table classinfo(teacher string,classname string) row format delimited fields terminated by '\t';
hive> create table choice(userid int,classname string) row format delimited fields terminated by '\t';

注意:'\t'相當於一個tab鍵盤。
顯示剛才創建的數據表:
hive> show tables;

2). 導入數據
建表后,可以從本地文件系統或 HDFS 中導入數據文件,導入數據樣例如下:
userinfo.txt內容如下(數據之間用tab鍵隔開):
1    xiapi
2    xiaoxue
3    qingqing

classinfo.txt內容如下(數據之間用tab鍵隔開):
jack    math
sam    china
lucy    english

choice.txt內容如下(數據之間用tab鍵隔開):
1    math
1    china
1    english
2    china
2    english
3    english
首先在本地“/home/hadoop/ziliao”下按照上面建立三個文件, 並添加如上的內容信息。

3. 按照下面導入數據。

hive> load data local inpath '/home/hadoop/ziliao/userinfo.txt' overwrite into table userinfo;
hive> load data local inpath '/home/hadoop/ziliao/classinfo.txt' overwrite into table classinfo;
hive> load data local inpath '/home/hadoop/ziliao/choice.txt' overwrite into table choice;

查詢表數據

hive> select * from userinfo;
hive> select * from classinfo;
hive> select * from choice;

4. 分區

a.創建分區
hive> create table ptest(userid int) partitioned by (name string) row format delimited fields terminated by '\t';
b.准備導入數據
xiapi.txt內容如下(數據之間用tab鍵隔開):
1    
c.導入數據
hive> load data local inpath '/home/hadoop/ziliao/xiapi.txt' overwrite into table ptest partition (name='xiapi');
d.查看分區
hive> dfs -ls /user/hive/warehouse/ptest/name=xiapi;
e.查詢分區
hive> select * from ptest where name='xiapi';
f.顯示分區
hive> show partitions ptest;
g.對分區插入數據(每次都會覆蓋掉原來的數據):
hive> insert overwrite table ptest partition(name='xiapi') select id from userinfo where name='xiapi';
h.刪除分區
hive> alter table ptest drop partition (name='xiapi')

5.桶
可以把表或分區組織成桶, 桶是按行分開組織特定字段, 每個桶對應一個 reduce 操作。在建立桶之前, 需要設置“hive.enforce.bucketing”屬性為 true, 使 Hive 能夠識別桶。在表中分桶的操作如下:

hive> set hive.enforce.bucketing=true;
hive> set hive.enforce.bucketing;
hive.enforce.bucketing=true;
hive> create table btest2(id int, name string) clustered by(id) into 3 buckets row format delimited fields terminated by '\t';

向桶中插入數據, 這里按照用戶 id 分了三個桶, 在插入數據時對應三個 reduce 操作,輸出三個文件。
hive> insert overwrite table btest2 select * from userinfo;

查看數據倉庫下的桶目錄,三個桶對應三個目錄。
hive> dfs -ls /user/hive/warehouse/btest2;

Hive 使用對分桶所用的值進行 hash,並用 hash 結果除以桶的個數做取余運算的方式來分桶,保證了每個桶中都有數據,但每個桶中的數據條數不一定相等,如下所示。

hive>dfs -cat /user/hive/warehouse/btest2/*0_0;
hive>dfs -cat /user/hive/warehouse/btest2/*1_0;
hive>dfs -cat /user/hive/warehouse/btest2/*2_0;

分桶可以獲得比分區更高的查詢效率,同時分桶也便於對全部數據進行采樣處理。下面是對桶取樣的操作。
hive>select * from btest2 tablesample(bucket 1 out of 3 on id);

6. 多表插入
多表插入指的是在同一條語句中, 把讀取的同一份元數據插入到不同的表中。只需要掃描一遍元數據即可完成所有表的插入操作, 效率很高。多表操作示例如下。

hive> create table mutill as select id,name from userinfo; #有數據
hive> create table mutil2 like mutill; #無數據,只有表結構
hive> from userinfo insert overwrite table mutill
      select id,name insert overwrite table mutil2 select count(distinct id),name group by name;

7.  連接
連接是將兩個表中在共同數據項上相互匹配的那些行合並起來, HiveQL 的連接分為內連接、左向外連接、右向外連接、全外連接和半連接 5 種。

a. 內連接(等值連接)
內連接使用比較運算符根據每個表共有的列的值匹配兩個表中的行。
例如, 檢索userinfo和choice表中標識號相同的所有行。

hive> select userinfo.*, choice.* from userinfo join choice on(userinfo.id=choice.userid);

b. 左連接
左連接的結果集包括“LEFT OUTER”子句中指定的左表的所有行, 而不僅僅是連接列所匹配的行。如果左表的某行在右表中沒有匹配行, 則在相關聯的結果集中右表的所有選擇列均為空值。

hive> select userinfo.*, choice.* from userinfo left outer join choice on(userinfo.id=choice.userid);

c. 右連接
右連接是左向外連接的反向連接,將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。

hive> select userinfo.*, choice.* from userinfo right outer join choice on(userinfo.id=choice.userid);

d. 全連接
全連接返回左表和右表中的所有行。當某行在另一表中沒有匹配行時,則另一個表的選擇列表包含空值。如果表之間有匹配行,則整個結果集包含基表的數據值。

hive> select userinfo.*, choice.* from userinfo full outer join choice on(userinfo.id=choice.userid);

e. 半連接
半連接是 Hive 所特有的, Hive 不支持 IN 操作,但是擁有替代的方案; left semi join, 稱為半連接, 需要注意的是連接的表不能在查詢的列中,只能出現在 on 子句中。

hive> select userinfo.* from userinfo left semi join choice on (userinfo.id=choice.userid);

8. 子查詢
標准 SQL 的子查詢支持嵌套的 select 子句,HiveQL 對子查詢的支持很有限,只能在from 引導的子句中出現子查詢。如下語句在 from 子句中嵌套了一個子查詢(實現了對教課最多的老師的查詢)。

hive>select teacher,MAX(class_num)
         from (select teacher, count(classname) as class_num from classinfo group by teacher)  subq
         group by teacher;

9.  視圖操作
目前,只有 Hive0.6 之后的版本才支持視圖。
Hive 只支持邏輯視圖, 並不支持物理視圖, 建立視圖可以在 MySQL 元數據庫中看到創建的視圖表, 但是在 Hive 的數據倉庫目錄下沒有相應的視圖表目錄。
當一個查詢引用一個視圖時, 可以評估視圖的定義並為下一步查詢提供記錄集合。這是一種概念的描述, 實際上, 作為查詢優化的一部分, Hive 可以將視圖的定義與查詢的定義結合起來,例如從查詢到視圖所使用的過濾器。
在視圖創建的同時確定視圖的架構,如果隨后再改變基本表(如添加一列)將不會在視圖的架構中體現。如果基本表被刪除或以不兼容的方式被修改,則該視圖的查詢將被無效。
視圖是只讀的,不能用於 LOAD/INSERT/ALTER。
視圖可能包含 ORDER BY 和 LIMIT 子句,如果一個引用了視圖的查詢也包含這些子句,那么在執行這些子句時首先要查看視圖語句,然后返回結果按照視圖中的語句執行。
以下是創建視圖的例子:

hive> create view teacher_classsum as select teacher, count(classname)  from classinfo group by teacher;

刪除視圖:

hive>drop view teacher_classnum;

10. 函數
創建函數

hive> create temporary function function_name as class_name

該語句創建一個由類名實現的函數。在 Hive 中用戶可以使用 Hive 類路徑中的任何類,用戶通過執行 add files 語句將函數類添加到類路徑,並且可持續使用該函數進行操作。
刪除函數
注銷用戶定義函數的格式如下:

hive> drop temporary function function_name;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM