1甚脉、背景表結(jié)構(gòu)
在講解中我們需要貫串一個(gè) 例子,所以需要設(shè)計(jì)一個(gè)情景铆农,對應(yīng) 還要有一個(gè)表結(jié)構(gòu)和填充數(shù)據(jù)牺氨。如下:有3個(gè)字段,分別為personId標(biāo)識某一個(gè)人墩剖,company標(biāo)識一家公司名稱猴凹,money標(biāo)識該公司每年盈利收入(單位:萬元人民幣)
personId | company | money |
---|---|---|
p1 | 公司1 | 100 |
p2 | 公司2 | 200 |
p1 | 公司3 | 150 |
p3 | 公司4 | 300 |
company_info.txt
p1 公司1 100
p2 公司2 200
p1 公司3 150
p3 公司4 300
創(chuàng)建表
hive> create table company_info(
personId string,
company string,
money float
)row format delimited fields terminated by "\t"
導(dǎo)入數(shù)據(jù)
hive> load data local inpath “company_info.txt” into table company_info;
2、order by
hive中的order by語句會對查詢結(jié)果做一次全局排序岭皂,即郊霎,所有的mapper產(chǎn)生的結(jié)果都會交給一個(gè)reducer去處理,無論數(shù)據(jù)量大小爷绘,job任務(wù)只會啟動一個(gè)reducer书劝,如果數(shù)據(jù)量巨大,則會耗費(fèi)大量的時(shí)間土至。
尖叫提示:如果在嚴(yán)格模式下购对,order by需要指定limit數(shù)據(jù)條數(shù),不然數(shù)據(jù)量巨大的情況下會造成崩潰無輸出結(jié)果陶因。涉及屬性:set hive.mapred.mode=nonstrict/strict
例如:按照money排序的例子
hive> select * from company_info order by money desc;
3骡苞、sort by
hive中的sort by語句會對每一塊局部數(shù)據(jù)進(jìn)行局部排序,即,每一個(gè)reducer處理的數(shù)據(jù)都是有序的解幽,但是不能保證全局有序贴见。
4、distribute by
hive中(distribute by + “表中字段”)關(guān)鍵字控制map輸出結(jié)果的分發(fā),相同字段的map輸出會發(fā)到一個(gè)reduce節(jié)點(diǎn)去處理亚铁。sort by為每一個(gè)reducer產(chǎn)生一個(gè)排序文件蝇刀,他倆一般情況下會結(jié)合使用。
hive中的distribute by一般要和sort by一起使用徘溢,即將某一塊數(shù)據(jù)歸給(distribute by)某一個(gè)reducer處理吞琐,然后在指定的reducer中進(jìn)行sort by排序。
尖叫提示:distribute by必須寫在sort by之前然爆,涉及屬性mapreduce.job.reduces站粟,hive.exec.reducers.bytes.per.reducer
例如:不同的人(personId)分為不同的組,每組按照money排序曾雕。
hive> select * from company_info distribute by personId sort by personId, money desc;
5奴烙、cluster by
hive中的cluster by在distribute by和sort by排序字段一致的情況下是等價(jià)的。同時(shí)剖张,cluster by指定的列只能是降序切诀,即默認(rèn)的descend,而不能是ascend搔弄。
簡單說:cluster by 相當(dāng)于 distribute by 和sort by 的結(jié)合幅虑,默認(rèn)只能是升序
例如:寫一個(gè)等價(jià)于distribute by 與sort by的例子
hive> select * from company_info distribute by personId sort by personId;
等價(jià)于
hive> select * from compnay_info cluster by personId;