Hive DML&分區(qū)表

1.INSERT

官網(wǎng)說明:

INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2...)[IF NOT EXISTS]] select_statement1 FROM from_statement;

INSERT INTO TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2...)]select_statement1 FROM from_statement;

hive> create table ruozedata_emp4 like ruozedata_emp;

hive> INSERT OVERWRITE TABLE ruozedata_emp4 select * FROM ruozedata_emp;

hive> select * from ruozedata_emp4;

hive> INSERT INTO TABLE ruozedata_emp4 select * FROM ruozedata_emp;

hive> select * from ruozedata_emp4;

數(shù)據(jù)被追加進(jìn)去

上邊兩個命令都是select * 侠讯,兩表都是一一對應(yīng)的挖藏,如果insert的表的列數(shù)與原表不等或者列的順序與原表不同會怎樣呢?

hive> INSERT OVERWRITE TABLE ruozedata_emp4

? ? > SELECT empno,ename from ruozedata_emp;

列的數(shù)量不同無法插入

hive> INSERT INTO TABLE ruozedata_emp4

? ? > select empno,job, ename,mgr, hiredate, salary, comm, deptno from ruozedata_emp;

(列的順序與原表不同)

hive> select * from ruozedata_emp4;

數(shù)據(jù)混亂

2.Writing data into the filesystem from queries

官網(wǎng)說明:

INSERT OVERWRITE [LOCAL] DIRECTORY directory1

[ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive0.11.0)

??SELECT ... FROM ...

3.where語句

hive> select * from ruozedata_emp where deptno=10;

hive> select * from ruozedata_emp where empno>=8000;

hive> select * from ruozedata_emp where salary between 800 and 1500;(左閉右閉)

hive> select * from ruozedata_emp limit 5;

hive> select * from ruozedata_emp where ename in ("SMITH","KING");

hive> select * from ruozedata_emp where ename not in ("SMITH","KING");

hive> select * from ruozedata_emp where comm is null;

4.聚合函數(shù)

max/min/count/sum/avg? ?特點:多進(jìn)一出

hive> select count(1) from ruozedata_emp where deptno=10;

hive>select max(salary),?min(salary),?avg(salary),?sum(salary) from ruozedata_emp;

5.分組函數(shù) group by

1)

hive> select deptno,avg(salary) from ruozedata_emp group by deptno;(求部門平均工資)

hive> select ename,deptno,avg(salary) from ruozedata_emp group by deptno;

FAILED: SemanticException [Error 10025]: Line 1:7 Expression not in GROUP BY key 'ename'

報錯原因:select中出現(xiàn)的字段厢漩,如果沒有出現(xiàn)在組函數(shù)/聚合函數(shù)中膜眠,必須出現(xiàn)在group by里面

2)求每個部門(deptno)、工作崗位(job)的最高工資(salary)

hive> select deptno,job,max(salary) from ruozedata_emp group by deptno,job;

3)求平均工資大于2000的部門

hive> select deptno,avg(salary) from ruozedata_emp group by deptno having avg(salary)>2000;

不能用where溜嗜,對于分組之后的結(jié)果進(jìn)行篩選只能用having宵膨;where是用來對單條數(shù)據(jù)進(jìn)行篩選的,而且where需要寫在group by之前的

4)case when then

select ename,salary,

case

when salary>1 and salary<=1000 then 'lower'

when salary>1000 and salary<=2000 then 'middle'

when salary>2000 and salary<=4000 then 'high'

else 'highest'

end

from ruozedata_emp;

5)join

hive> select * from a join b;

笛卡爾積炸宵,非常耗性能柄驻,一定要規(guī)避

inner join=join

outer join:left join,right join焙压,full join

hive> select a.id,a.name,b.age from a join b on a.id=b.id;

hive> select a.id,a.name,b.age from a left join b on a.id=b.id;

hive> select a.id,a.name,b.age from a full join b on a.id=b.id;

6.分區(qū)表? ?partition

1)靜態(tài)分區(qū)

hive> create table order_partition(

? ? > ordernumber string,

? ? > eventtime string

? ? > )

? ? > partitioned by (event_month string)

? ? > row format delimited fields terminated by '\t';

hive> LOAD DATA LOCAL INPATH '/home/hadoop/data/order.txt'

? ? > OVERWRITE INTO TABLE order_partition

? ? > PARTITION(event_month='2014-05');

hive> select * from order_partition;

OK

10703007267488? 2014-05-01? ? ? 2014-05

10101043505096? 2014-05-01? ? ? 2014-05

10103043509747? 2014-05-01? ? ? 2014-05

10103043501575? 2014-05-01? ? ? 2014-05

10104043514061? 2014-05-01? ? ? 2014-05

hive> desc formatted order_partition;

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://192.168.137.141:9000/user/hive/warehouse/ruozedata.db/order_partition

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://192.168.137.141:9000/user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-05

hive> select * from order_partition where event_month='2014-05';

分區(qū)查詢的時候務(wù)必要記得帶上分區(qū)

添加分區(qū)的方法

a)以上是通過建表的時候直接分區(qū)鸿脓,hdfs里顯示相應(yīng)的分區(qū)會有相應(yīng)的文件夾/order_partition/event_month=2014-05,那么可不可以通過直接在hdfs里新建一個分區(qū)文件夾來進(jìn)行分區(qū)呢涯曲?

[hadoop@hadoop001 data]$ hdfs dfs -mkdir /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-06

[hadoop@hadoop001 data]$ hdfs dfs -put order.txt /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-06

新建了/event_month=2014-06文件夾野哭,并將order.txt放入了該文件夾內(nèi)

[hadoop@hadoop001 data]$ hdfs dfs -ls hdfs://192.168.137.141:9000/user/hive/warehouse/ruozedata.db/order_partition

hive> select * from order_partition where event_month='2014-06';

在hive里查詢一下分區(qū)event_month=2014-06下的數(shù)據(jù),卻發(fā)現(xiàn)是空的:

進(jìn)入mysql查看一下元數(shù)據(jù)

mysql> show databases;

mysql> use ruozedata_basic03;

mysql> show tables;

mysql> select * from partitions;

mysql> select * from partition_keys;

mysql> select * from partition_key_vals;

發(fā)現(xiàn)元數(shù)據(jù)里只有event_month=2014-05這一個分區(qū)幻件,為什么呢匆浙?

查詢官網(wǎng)的說明:

metastore沒有感知到产舞,需使用msck命令修復(fù)MySQL表的元數(shù)據(jù)信息,Hive上才能查到到相應(yīng)的數(shù)據(jù)結(jié)果

hive> MSCK REPAIR TABLE order_partition;

OK

Partitions not in metastore:? ? order_partition:event_month=2014-06

Repair: Added partition to metastore order_partition:event_month=2014-06

再次查看mysql里的元數(shù)據(jù),分區(qū)信息都進(jìn)來了

hive> select * from order_partition where event_month='2014-06';

但是幔崖,這個命令會刷新所有的分區(qū)信息,過于簡單粗暴呜舒,不建議使用

b)應(yīng)該用Add Partitions

ALTER TABLE table_name ADD [IF NOT EXISTS] PARTITION partition_spec [LOCATION?'location'][, PARTITION partition_spec [LOCATION?'location'],?...];

partition_spec:

??: (partition_column = partition_col_value, partition_column = partition_col_value,?...)

再來試驗一下

[hadoop@hadoop001 data]$ hdfs dfs -mkdir /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-07

[hadoop@hadoop001 data]$ hdfs dfs -put order.txt /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-07

[hadoop@hadoop001 data]$ hdfs dfs -ls /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-07

Found 1 items

-rw-r--r--? 1 hadoop supergroup? ? ? ? 217 2018-06-17 21:11 /user/hive/warehouse/ruozedata.db/order_partition/event_month=2014-07/order.txt

hive> select * from order_partition where event_month='2014-07';

OK(沒有數(shù)據(jù))

使用Add Partitions命令

hive> ALTER TABLE order_partition ADD IF NOT EXISTS

? ? > PARTITION (event_month='2014-07') ;

hive> select * from order_partition where event_month='2014-07';

c)還有一種方法反璃,如下:

hive> create table order_4_partition(

? ? > ordernumber string,

? ? > eventtime string

? ? > )

? ? > row format delimited fields terminated by '\t';

hive> load data local inpath '/home/hadoop/data/order.txt' overwrite into table order_4_partition;

hive> insert overwrite table order_partition

? ? > partition(event_month='2014-08')

? ? > select * from order_4_partition;

hive> select * from order_partition where event_month='2014-08';

在hive里查看當(dāng)前有幾個分區(qū)的方法:

hive> show partitions order_partition;

靜態(tài)分區(qū)是最簡單的分區(qū),單級分區(qū)秃臣,分區(qū)與表中的字段內(nèi)容沒有關(guān)系涧衙,而且出現(xiàn)在分區(qū)內(nèi)的字段內(nèi)容是不能出現(xiàn)在表中的

d)多級分區(qū)

hive> create table order_mulit_partition(

? ? > ordernumber string,

? ? > eventtime string

? ? > )

? ? > partitioned by (event_month string,event_day string)

? ? > row format delimited fields terminated by '\t';

hive> desc formatted order_mulit_partition;

hive> LOAD DATA LOCAL INPATH '/home/hadoop/data/order.txt'

? ? > OVERWRITE INTO TABLE order_mulit_partition

? ? > PARTITION(event_month='2014-05', event_day='01');

hive> select * from order_mulit_partition where event_month='2014-05' and event_day='01';

查看一下在hdfs下的目錄結(jié)構(gòu)

2)動態(tài)分區(qū)

hive> create table ruozedata_static_emp

? ? > (empno int, ename string, job string, mgr int, hiredate string, salary double, comm double,deptno string)

? ? > PARTITIONED by(deptno string)

? ? > ROW FORMAT DELIMITED

? ? > FIELDS TERMINATED BY '\t';

FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns(報錯,分區(qū)字段不能出現(xiàn)在表中)

hive> create table ruozedata_static_emp

? ? > (empno int, ename string, job string, mgr int, hiredate string, salary double, comm double)

? ? > PARTITIONED by(deptno string)

? ? > ROW FORMAT DELIMITED

? ? > FIELDS TERMINATED BY '\t';

hive> select * from ruozedata_static_emp;

OK(里面沒有數(shù)據(jù))

將表?ruozedata_emp中deptno=10的數(shù)據(jù)寫入?ruozedata_static_emp的deptno='10'的分區(qū)內(nèi)

hive> insert into table ruozedata_static_emp partition(deptno='10')

? ? > select empno,ename,job,mgr,hiredate,salary,comm from ruozedata_emp

? ? > where deptno=10;

hive> select * from ruozedata_static_emp;

以上還是靜態(tài)分區(qū)的方法奥此,如果需要分區(qū)很多(比如1000個分區(qū))弧哎,這種方式太耗時間精力

所以需要動態(tài)分區(qū)

hive> create table ruozedata_dynamic_emp

? ? > (empno int, ename string, job string, mgr int, hiredate string, salary double, comm double)

? ? > PARTITIONED by(deptno string)

? ? > ROW FORMAT DELIMITED

? ? > FIELDS TERMINATED BY '\t' ;

動態(tài)分區(qū)明確要求:分區(qū)字段寫在select的最后面

hive> insert into table ruozedata_dynamic_emp partition(deptno)

? ? > select empno,ename,job,mgr,hiredate,salary,comm,deptno from ruozedata_emp ;

hive> set hive.exec.dynamic.partition.mode=nonstrict;(只是臨時設(shè)置為 nonstrict ,重新進(jìn)入hive又會自動變?yōu)閟trict模式)

【set hive.exec.dynamic.partition.mode=nonstrict;

這是hive中常用的設(shè)置key=value的方式

語法格式:

set key=value; 設(shè)置

set key;? ? ? ? ? ? ?取值? 】

hive> insert into table ruozedata_dynamic_emp partition(deptno)

? ? > select empno,ename,job,mgr,hiredate,salary,comm,deptno from ruozedata_emp ;

hive> show partitions ruozedata_dynamic_emp;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末稚虎,一起剝皮案震驚了整個濱河市撤嫩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蠢终,老刑警劉巖序攘,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鸭限,死亡現(xiàn)場離奇詭異,居然都是意外死亡两踏,警方通過查閱死者的電腦和手機(jī)败京,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來梦染,“玉大人赡麦,你說我怎么就攤上這事∨潦叮” “怎么了泛粹?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長肮疗。 經(jīng)常有香客問我晶姊,道長,這世上最難降的妖魔是什么伪货? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任们衙,我火速辦了婚禮,結(jié)果婚禮上碱呼,老公的妹妹穿的比我還像新娘蒙挑。我一直安慰自己,他們只是感情好愚臀,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布忆蚀。 她就那樣靜靜地躺著,像睡著了一般姑裂。 火紅的嫁衣襯著肌膚如雪馋袜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天舶斧,我揣著相機(jī)與錄音欣鳖,去河邊找鬼。 笑死捧毛,一個胖子當(dāng)著我的面吹牛观堂,可吹牛的內(nèi)容都是我干的让网。 我是一名探鬼主播呀忧,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼溃睹!你這毒婦竟也來了而账?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤因篇,失蹤者是張志新(化名)和其女友劉穎泞辐,沒想到半個月后笔横,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡咐吼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年吹缔,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锯茄。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡厢塘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出肌幽,到底是詐尸還是另有隱情晚碾,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布喂急,位于F島的核電站格嘁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏廊移。R本人自食惡果不足惜糕簿,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望狡孔。 院中可真熱鬧冶伞,春花似錦、人聲如沸步氏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽荚醒。三九已至芋类,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間界阁,已是汗流浹背侯繁。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留泡躯,地道東北人贮竟。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像较剃,于是被迫代替她去往敵國和親咕别。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 主流關(guān)系型數(shù)據(jù)庫 關(guān)系型數(shù)據(jù)庫存儲數(shù)據(jù)的特點 結(jié)構(gòu)化查詢語言: 數(shù)據(jù)類型: select * from emp; ...
    陳先森mansplain閱讀 690評論 0 0
  • mysql數(shù)據(jù)庫中 :database : 文件夾table : 數(shù)據(jù)表(數(shù)據(jù)文件) 進(jìn)入mysqlmysql -...
    賦閑閱讀 567評論 0 0
  • 5.多表查詢 多表查詢 目的:從多張表獲取數(shù)據(jù) 前提:進(jìn)行連接的多張表中有共同的列 等連接 通過兩個表具有相同意義...
    喬震閱讀 1,244評論 0 0
  • 第九章 他們聽了老村長的話写穴,繼續(xù)往前走惰拱,走著走著,又一個奇怪的東西啊送,跑到了他們的面前偿短,這次...
    齊異初閱讀 157評論 0 0
  • 端正如如萬物生欣孤,午夜半酣賞華燈。 安然記取今朝事昔逗,康樂可期好夢成降传。 【2015年6月20日】
    d03e056874dc閱讀 219評論 0 0