1Hive簡介
Hive對我來說就是一個基於HDFS的數據倉庫,它提供了一個種類SQL語言(和SQL標准基本一樣又有一些特殊的地方不一樣),能讓不精通Java語言而熟悉SQL語言的工程師,快速的對HDFS或其他存儲文件系統如Amazon,S3,上的數據進行數據分析,是Hadoop生態系統中非常重要的一個工具。對於大數據分析師而言,HiveQL則是必須要掌握的一個工具。
2.Hive常用語句
2.1菜鳥建表法
1.直接建表,指定分隔符,默認存儲為text,也可以指定存儲格式!
create table if not exists employees( nam STRING, salary FLOAT comment "income", subordinates ARRAY<STRING>, deductions MAP<STRING, FLOAT>, address STRUCT<street:STRING, city:STRING, state:STRING,zip:INT> ) row format delimited fields terminated by "," collection items terminated by "-" map keys terminated by "_" lines terminated by "\n" stored as textfile; --comment注釋,collection items以"-"分割,即此建表語句中的ARRAY,map keys以"_"分割,行以"\n"分割,存儲為text格式,也可以選則sequence 等,text是默認值。 --查看表結構: hive> desc employees; OK nam string salary float income subordinates array<string> deductions map<string,float> address struct<street:string,city:string,state:string,zip:int> Time taken: 0.153 seconds, Fetched: 5 row(s) --也可以用desc extended employees;或者desc formatted employees;來顯示更加詳盡冗余的表信息。
2從查詢語句建表
--原表 hive> select * from tb151; OK 0 馬文楷 29 1 王俊朋 28 2 李玉林 28 3 陳鵬輝 28 4 李春廷 28 5 白衛東 27
--通過查詢語句建表
create table tb151_sample as select * from tb151 tablesample(5 rows);
hive> select * from tb151_sample;
OK
0 馬文楷 29
1 王俊朋 28
2 李玉林 28
3 陳鵬輝 28
4 李春廷 28
--注:tablesample是表抽樣函數,5 rows代表抽取前5行,也可以是百分比(50 percent)或按照內存抽樣(10M):
3根據已知表結構創建表
create table test_tb like tb151; hive> desc tb151; OK id string name string age int Time taken: 0.173 seconds, Fetched: 3 row(s) hive> desc test_like; OK id string name string age int Time taken: 0.164 seconds, Fetched: 3 row(s)
2.2菜鳥導入導出Hive數據的幾種方法
1.裝載數據
--1直接從本地導入表 load data local inpath "/path/to/data" overwrite into table tbname; --注:local指定本地目錄,不加的話默認是HDFS目錄,overwrite關鍵字直接覆蓋原表數據 --2將查詢語句結果導入表 insert overwrite table tbname partition(partname="ptname") select * from tbname2 where....; --注:overwrite關鍵字會覆蓋元數據,使用into則追加數據
2.導出數據
--1如果數據文件恰好是需要的格式則直接從HDFS目錄下載即可 hadoop fs -get /path --2通過查詢語句 insert overwrite local directory "/path/to/data" select ... from tbname where...; --3通過shell命令行 /path/to/hive -e "select ...from tbname where...." >> /path/to/data.txt
2.3關於Join
一般情況下按一下規則進行join:
1.從左到右表由小到大
2或者顯示的標記大表
select /*+STREAMTABLE(t)*/t.a,t1.b from t join t1 where……; --注:t表示大表
3小表join大表可以使用mapjoin將小表放到內存中
select /*+MAPJOIN(d)*/s.ymd,s.tmd from s join d on s.ymd=d.ymd; --注:d指的是小表
3.Hive集合之交並差
先看原始表:
hive> select * from tb151; OK 0 馬文楷 29 1 王俊朋 28 2 李玉林 28 3 陳鵬輝 28 4 李春廷 28 5 白衛東 27
1.Hive並集可以用union實現
hive> select age from tb151 union > select age from tb151; Total MapReduce CPU Time Spent: 3 seconds 520 msec OK 27 28 29 Time taken: 36.328 seconds, Fetched: 3 row(s)
2.Hive的union all則是合並兩個集合不去重
> select age from tb151 union all > select age from tb151; Total MapReduce CPU Time Spent: 1 seconds 290 msec OK 29 29 28 28 28 28 28 28 28 28 27 27 Time taken: 23.941 seconds, Fetched: 12 row(s)
3.Hive交集可以通過先A左關聯B,然后得到的結果將右列不為NULL的元素去重即可
4差集左表A-右表B,先用Aleft joinB,然后得到的結果右列為NULL的左列元素去重即可
注:3,4可以先把做關聯的結果存儲為一個表,然后根據要求進行選擇去重即可,咱們看一下關聯結果就OK
hive> select * from tb151; OK 0 馬文楷 29 1 王俊朋 28 2 李玉林 28 3 陳鵬輝 28 4 李春廷 28 5 白衛東 27 Time taken: 0.141 seconds, Fetched: 6 row(s) hive> select * from tb151_sample; OK 0 馬文楷 29 1 王俊朋 28 2 李玉林 28 3 陳鵬輝 28 4 李春廷 28 hive> select t.age,t1.age from tb151 t left join tb151_sample t1 > on t.age=t1.age; Total MapReduce CPU Time Spent: 1 seconds 550 msec OK 29 29 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 27 NULL --注:兩個表的結果交集很明顯右列不為空的元素去重,差集左表-右表則是右列為空的元素去重即可
4.Hive的幾種執行方式
--1執行單條語句 hive -e "select * from tbname" --2執行sql文件 hive -f /path/to/sqlfil --3可以在hive命令行通過dfs執行Hadoop命令 hive> dfs -ls /; Found 3 items drwxr-xr-x - root supergroup 0 2019-02-15 19:18 /test drwx-wx-wx - root supergroup 0 2019-02-12 18:23 /tmp drwxr-xr-x - root supergroup 0 2019-01-27 09:19 /user --4也可以在hive命令行通過!+命令執行shell的一些簡單命令 hive> ! ls /wjp; data javacode pycode readDoc.py sparkcode udf