上一篇:097-BigData-25Hive函數(shù)壓縮存儲(chǔ)格式
九、企業(yè)級(jí)調(diào)優(yōu)
9.1 Fetch抓取
Fetch抓取是指,Hive中對(duì)某些情況的查詢(xún)可以不必使用MapReduce計(jì)算。例如:SELECT * FROM employees;在這種情況下,Hive可以簡(jiǎn)單地讀取employee對(duì)應(yīng)的存儲(chǔ)目錄下的文件,然后輸出查詢(xún)結(jié)果到控制臺(tái)。
在hive-default.xml.template文件中hive.fetch.task.conversion默認(rèn)是more,老版本hive默認(rèn)是minimal,該屬性修改為more以后,在全局查找、字段查找、limit查找等都不走mapreduce。
<property>
<name>hive.fetch.task.conversion</name>
<value>more</value>
<description>
Expects one of [none, minimal, more].
Some select queries can be converted to single FETCH task minimizing latency.
Currently the query should be single sourced not having any subquery and should not have
any aggregations or distincts (which incurs RS), lateral views and joins.
0. none : disable hive.fetch.task.conversion
1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
2. more : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)
</description>
</property>
案例實(shí)操:
1)把hive.fetch.task.conversion設(shè)置成none,然后執(zhí)行查詢(xún)語(yǔ)句,都會(huì)執(zhí)行mapreduce程序。
hive (default)> set hive.fetch.task.conversion=none;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;
2)把hive.fetch.task.conversion設(shè)置成more,然后執(zhí)行查詢(xún)語(yǔ)句,如下查詢(xún)方式都不會(huì)執(zhí)行mapreduce程序。
hive (default)> set hive.fetch.task.conversion=more;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;
9.2 本地模式
大多數(shù)的Hadoop Job是需要Hadoop提供的完整的可擴(kuò)展性來(lái)處理大數(shù)據(jù)集的。不過(guò),有時(shí)Hive的輸入數(shù)據(jù)量是非常小的。在這種情況下,為查詢(xún)觸發(fā)執(zhí)行任務(wù)消耗的時(shí)間可能會(huì)比實(shí)際job的執(zhí)行時(shí)間要多的多。對(duì)于大多數(shù)這種情況,Hive可以通過(guò)本地模式在單臺(tái)機(jī)器上處理所有的任務(wù)。對(duì)于小數(shù)據(jù)集,執(zhí)行時(shí)間可以明顯被縮短。
用戶(hù)可以通過(guò)設(shè)置hive.exec.mode.local.auto的值為true,來(lái)讓Hive在適當(dāng)?shù)臅r(shí)候自動(dòng)啟動(dòng)這個(gè)優(yōu)化。
set hive.exec.mode.local.auto=true; //開(kāi)啟本地mr
//設(shè)置local mr的最大輸入數(shù)據(jù)量,當(dāng)輸入數(shù)據(jù)量小于這個(gè)值時(shí)采用local mr的方式,默認(rèn)為134217728,即128M
set hive.exec.mode.local.auto.inputbytes.max=50000000;
//設(shè)置local mr的最大輸入文件個(gè)數(shù),當(dāng)輸入文件個(gè)數(shù)小于這個(gè)值時(shí)采用local mr的方式,默認(rèn)為4
set hive.exec.mode.local.auto.input.files.max=10;
案例實(shí)操:
1)開(kāi)啟本地模式,并執(zhí)行查詢(xún)語(yǔ)句(注意重啟Hive)
hive (default)> set hive.exec.mode.local.auto=true;
hive (default)> select * from emp cluster by deptno;
Time taken: 1.328 seconds, Fetched: 14 row(s)
2)關(guān)閉本地模式,并執(zhí)行查詢(xún)語(yǔ)句
hive (default)> set hive.exec.mode.local.auto=false;
hive (default)> select * from emp cluster by deptno;
Time taken: 20.09 seconds, Fetched: 14 row(s)
9.3 表的優(yōu)化
9.3.1 小表、大表Join
將key相對(duì)分散,并且數(shù)據(jù)量小的表放在join的左邊,這樣可以有效減少內(nèi)存溢出錯(cuò)誤發(fā)生的幾率;再進(jìn)一步,可以使用Group變小的維度表(1000條以下的記錄條數(shù))先進(jìn)內(nèi)存。在map端完成reduce(預(yù)聚合)。
實(shí)際測(cè)試發(fā)現(xiàn):新版的hive已經(jīng)對(duì)小表JOIN大表和大表JOIN小表進(jìn)行了優(yōu)化。小表放在左邊和右邊已經(jīng)沒(méi)有明顯區(qū)別。
案例實(shí)操
(0)需求:測(cè)試大表JOIN小表和小表JOIN大表的效率
(1)建大表、小表和JOIN后表的語(yǔ)句
// 創(chuàng)建大表
create table bigtable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
// 創(chuàng)建小表
create table smalltable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
// 創(chuàng)建join后表的語(yǔ)句
create table jointable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
(2)分別向大表和小表中導(dǎo)入數(shù)據(jù)
hive (default)> load data local inpath '/opt/module/datas/bigtable' into table bigtable;
hive (default)>load data local inpath '/opt/module/datas/smalltable' into table smalltable;
(3)關(guān)閉mapjoin功能(默認(rèn)是打開(kāi)的)
set hive.auto.convert.join = false;
(4)執(zhí)行小表JOIN大表語(yǔ)句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
left join bigtable b
on b.id = s.id;
Time taken: 35.921 seconds
(5)執(zhí)行大表JOIN小表語(yǔ)句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable b
left join smalltable s
on s.id = b.id;
Time taken: 34.196 seconds
9.3.2 大表Join大表
1)空KEY過(guò)濾
有時(shí)join超時(shí)是因?yàn)槟承﹌ey對(duì)應(yīng)的數(shù)據(jù)太多,而相同key對(duì)應(yīng)的數(shù)據(jù)都會(huì)發(fā)送到相同的reducer上,從而導(dǎo)致內(nèi)存不夠。此時(shí)我們應(yīng)該仔細(xì)分析這些異常的key,很多情況下,這些key對(duì)應(yīng)的數(shù)據(jù)是異常數(shù)據(jù),我們需要在SQL語(yǔ)句中進(jìn)行過(guò)濾。例如key對(duì)應(yīng)的字段為空,操作如下:
案例實(shí)操
(1)配置歷史服務(wù)器
配置mapred-site.xml
<property>
<name>mapreduce.jobhistory.address</name>
<value>bigdata111:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>bigdata111:19888</value>
</property>
啟動(dòng)歷史服務(wù)器
sbin/mr-jobhistory-daemon.sh start historyserver
查看jobhistory
http://192.168.1.102:19888/jobhistory
(2)創(chuàng)建原始數(shù)據(jù)表、空id表、合并后數(shù)據(jù)表
// 創(chuàng)建原始表
create table ori(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
// 創(chuàng)建空id表
create table nullidtable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
// 創(chuàng)建join后表的語(yǔ)句
create table jointable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
(3)分別加載原始數(shù)據(jù)和空id數(shù)據(jù)到對(duì)應(yīng)表中
hive (default)> load data local inpath '/opt/module/datas/ori' into table ori;
hive (default)> load data local inpath '/opt/module/datas/nullid' into table nullidtable;
(4)測(cè)試不過(guò)濾空id
hive (default)> insert overwrite table jointable
select n.* from nullidtable n left join ori o on n.id = o.id;
Time taken: 42.038 seconds
Time taken: 37.284 seconds
Time taken: 97.281 seconds
(5)測(cè)試過(guò)濾空id
hive (default)> insert overwrite table jointable
select n.* from (select * from nullidtable where id is not null ) n left join ori o on n.id = o.id;
Time taken: 31.725 seconds
Time taken: 28.876 seconds
2)空key轉(zhuǎn)換
有時(shí)雖然某個(gè)key為空對(duì)應(yīng)的數(shù)據(jù)很多,但是相應(yīng)的數(shù)據(jù)不是異常數(shù)據(jù),必須要包含在join的結(jié)果中,此時(shí)我們可以表a中key為空的字段賦一個(gè)隨機(jī)的值,使得數(shù)據(jù)隨機(jī)均勻地分不到不同的reducer上。例如:
案例實(shí)操:
不隨機(jī)分布空null值:
(1)設(shè)置5個(gè)reduce個(gè)數(shù)
set mapreduce.job.reduces = 5;
(2)JOIN兩張表
insert overwrite table jointable
select n.* from nullidtable n left join ori b on n.id = b.id;
結(jié)果:可以看出來(lái),出現(xiàn)了數(shù)據(jù)傾斜,某些reducer的資源消耗遠(yuǎn)大于其他reducer。
隨機(jī)分布空null值
(1)設(shè)置5個(gè)reduce個(gè)數(shù)
set mapreduce.job.reduces = 5;
(2)JOIN兩張表
insert overwrite table jointable
select n.* from nullidtable n full join ori o on
case when n.id is null then concat('hive', rand()) else n.id end = o.id;
結(jié)果:可以看出來(lái),消除了數(shù)據(jù)傾斜,負(fù)載均衡reducer的資源消耗
9.3.3 MapJoin
如果不指定MapJoin或者不符合MapJoin的條件,那么Hive解析器會(huì)將Join操作轉(zhuǎn)換成Common Join,即:在Reduce階段完成join。容易發(fā)生數(shù)據(jù)傾斜。可以用MapJoin把小表全部加載到內(nèi)存在map端進(jìn)行join,避免reducer處理。
1)開(kāi)啟MapJoin參數(shù)設(shè)置:
(1)設(shè)置自動(dòng)選擇Mapjoin
set hive.auto.convert.join = true; 默認(rèn)為true
(2)大表小表的閾值設(shè)置(默認(rèn)25M一下認(rèn)為是小表):
set hive.mapjoin.smalltable.filesize=25000000;
2)MapJoin工作機(jī)制
案例實(shí)操:
(1)開(kāi)啟Mapjoin功能
set hive.auto.convert.join = true; 默認(rèn)為true
(2)執(zhí)行小表JOIN大表語(yǔ)句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
join bigtable b
on s.id = b.id;
Time taken: 24.594 seconds
(3)執(zhí)行大表JOIN小表語(yǔ)句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable b
join smalltable s
on s.id = b.id;
Time taken: 24.315 seconds
9.3.4 Group By
默認(rèn)情況下,Map階段同一Key數(shù)據(jù)分發(fā)給一個(gè)reduce,當(dāng)一個(gè)key數(shù)據(jù)過(guò)大時(shí)就傾斜了。
并不是所有的聚合操作都需要在Reduce端完成,很多聚合操作都可以先在Map端進(jìn)行部分聚合,最后在Reduce端得出最終結(jié)果。
1)開(kāi)啟Map端聚合參數(shù)設(shè)置
(1)是否在Map端進(jìn)行聚合,默認(rèn)為T(mén)rue
set hive.map.aggr = true
(2)在Map端進(jìn)行聚合操作的條目數(shù)目
set hive.groupby.mapaggr.checkinterval = 100000
(3)有數(shù)據(jù)傾斜的時(shí)候進(jìn)行負(fù)載均衡(默認(rèn)是false)
set hive.groupby.skewindata = true
當(dāng)選項(xiàng)設(shè)定為 true,生成的查詢(xún)計(jì)劃會(huì)有兩個(gè)MR Job。第一個(gè)MR Job中,Map的輸出結(jié)果會(huì)隨機(jī)分布到Reduce中,每個(gè)Reduce做部分聚合操作,并輸出結(jié)果,這樣處理的結(jié)果是相同的Group By Key有可能被分發(fā)到不同的Reduce中,從而達(dá)到負(fù)載均衡的目的;第二個(gè)MR Job再根據(jù)預(yù)處理的數(shù)據(jù)結(jié)果按照Group By Key分布到Reduce中(這個(gè)過(guò)程可以保證相同的Group By Key被分布到同一個(gè)Reduce中),最后完成最終的聚合操作。
9.3.5 Count(Distinct) 去重統(tǒng)計(jì)
數(shù)據(jù)量小的時(shí)候無(wú)所謂,數(shù)據(jù)量大的情況下,由于COUNT DISTINCT操作需要用一個(gè)Reduce Task來(lái)完成,這一個(gè)Reduce需要處理的數(shù)據(jù)量太大,就會(huì)導(dǎo)致整個(gè)Job很難完成,一般COUNT DISTINCT使用先GROUP BY再COUNT的方式替換:
案例實(shí)操
(1)創(chuàng)建一張大表
hive (default)> create table bigtable(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) row format delimited fields terminated by '\t';
(2)加載數(shù)據(jù)
hive (default)> load data local inpath '/opt/module/datas/bigtable' into table bigtable;
(3)設(shè)置5個(gè)reduce個(gè)數(shù)
set mapreduce.job.reduces = 5;
(4)執(zhí)行去重id查詢(xún)
hive (default)> select count(distinct id) from bigtable;
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 7.12 sec HDFS Read: 120741990 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 7 seconds 120 msec
OK
c0
100001
Time taken: 23.607 seconds, Fetched: 1 row(s)
(5)采用GROUP by去重id(推薦)
hive (default)> select count(id) from (select id from bigtable group by id) a;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 17.53 sec HDFS Read: 120752703 HDFS Write: 580 SUCCESS
Stage-Stage-2: Map: 1 Reduce: 1 Cumulative CPU: 4.29 sec HDFS Read: 9409 HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 21 seconds 820 msec
OK
_c0
100001
Time taken: 50.795 seconds, Fetched: 1 row(s)
雖然會(huì)多用一個(gè)Job來(lái)完成,但在數(shù)據(jù)量大的情況下,這個(gè)絕對(duì)是值得的。
9.3.6 笛卡爾積
盡量避免笛卡爾積,join的時(shí)候不加on條件,或者無(wú)效的on條件,Hive只能使用1個(gè)reducer來(lái)完成笛卡爾積。
9.3.7 行列過(guò)濾
列處理:在SELECT中,只拿需要的列,如果有,盡量使用分區(qū)過(guò)濾,少用SELECT *。
行處理:在分區(qū)剪裁中,當(dāng)使用外關(guān)聯(lián)時(shí),如果將副表的過(guò)濾條件寫(xiě)在Where后面,那么就會(huì)先全表關(guān)聯(lián),之后再過(guò)濾,總而言之,就是先where還是先join的執(zhí)行順序的問(wèn)題,以下兩種,經(jīng)過(guò)SQL優(yōu)化器,執(zhí)行效果大體一樣。比如:
案例實(shí)操:
(1)測(cè)試先關(guān)聯(lián)兩張表,再用where條件過(guò)濾
hive (default)> select o.id from bigtable b
join ori o on o.id = b.id
where o.id <= 10;
Time taken: 34.406 seconds, Fetched: 100 row(s)
(2)通過(guò)子查詢(xún)后,再關(guān)聯(lián)表
hive (default)> select b.id from bigtable b
join (select id from ori where id <= 10 ) o on b.id = o.id;
Time taken: 30.058 seconds, Fetched: 100 row(s)
9.3.8 動(dòng)態(tài)分區(qū)調(diào)整
關(guān)系型數(shù)據(jù)庫(kù)中,對(duì)分區(qū)表Insert數(shù)據(jù)時(shí)候,數(shù)據(jù)庫(kù)自動(dòng)會(huì)根據(jù)分區(qū)字段的值,將數(shù)據(jù)插入到相應(yīng)的分區(qū)中,Hive中也提供了類(lèi)似的機(jī)制,即動(dòng)態(tài)分區(qū)(Dynamic Partition),只不過(guò),使用Hive的動(dòng)態(tài)分區(qū),需要進(jìn)行相應(yīng)的配置。
1)開(kāi)啟動(dòng)態(tài)分區(qū)參數(shù)設(shè)置
(1)開(kāi)啟動(dòng)態(tài)分區(qū)功能(默認(rèn)true,開(kāi)啟)
set hive.exec.dynamic.partition=true
(2)設(shè)置為非嚴(yán)格模式(動(dòng)態(tài)分區(qū)的模式,默認(rèn)strict,表示必須指定至少一個(gè)分區(qū)為靜態(tài)分區(qū),nonstrict模式表示允許所有的分區(qū)字段都可以使用動(dòng)態(tài)分區(qū)。)
set hive.exec.dynamic.partition.mode=nonstrict
(3)在所有執(zhí)行MR的節(jié)點(diǎn)上,最大一共可以創(chuàng)建多少個(gè)動(dòng)態(tài)分區(qū)。(默認(rèn)1000)
set hive.exec.max.dynamic.partitions=1000
(4)在每個(gè)執(zhí)行MR的節(jié)點(diǎn)上,最大可以創(chuàng)建多少個(gè)動(dòng)態(tài)分區(qū)。該參數(shù)需要根據(jù)實(shí)際的數(shù)據(jù)來(lái)設(shè)定。比如:源數(shù)據(jù)中包含了一年的數(shù)據(jù),即day字段有365個(gè)值,那么該參數(shù)就需要設(shè)置成大于365,如果使用默認(rèn)值100,則會(huì)報(bào)錯(cuò)。
set hive.exec.max.dynamic.partitions.pernode=100
(5)整個(gè)MR Job中,最大可以創(chuàng)建多少個(gè)HDFS文件。(默認(rèn)值100000)
set hive.exec.max.created.files=100000
(6)當(dāng)有空分區(qū)生成時(shí),是否拋出異常。一般不需要設(shè)置。(默認(rèn)false)
set hive.error.on.empty.partition=false
2)案例實(shí)操
需求:將ori中的數(shù)據(jù)按照時(shí)間(如:20111230000008),插入到目標(biāo)表ori_partitioned_target的相應(yīng)分區(qū)中。
(1)創(chuàng)建分區(qū)表
create table ori_partitioned(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string)
partitioned by (p_time bigint)
row format delimited fields terminated by '\t';
(2)加載數(shù)據(jù)到分區(qū)表中
hive (default)> load data local inpath '/opt/module/datas/ds1' into table ori_partitioned partition(p_time='20111230000010') ;
hive (default)> load data local inpath '/opt/module/datas/ds2' into table ori_partitioned partition(p_time='20111230000011') ;
(3)創(chuàng)建目標(biāo)分區(qū)表
create table ori_partitioned_target(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) PARTITIONED BY (p_time STRING) row format delimited fields terminated by '\t';
(4)設(shè)置動(dòng)態(tài)分區(qū)
set hive.exec.dynamic.partition = true;(默認(rèn)true)
set hive.exec.dynamic.partition.mode = nonstrict;(默認(rèn)strict)
set hive.exec.max.dynamic.partitions = 1000;(默認(rèn)1000)
set hive.exec.max.dynamic.partitions.pernode = 100;(默認(rèn)100)
set hive.exec.max.created.files = 100000;(默認(rèn)值100000)
set hive.error.on.empty.partition = false;(默認(rèn)值false)
hive (default)> insert overwrite table ori_partitioned_target partition (p_time)
select id, time, uid, keyword, url_rank, click_num, click_url, p_time from ori_partitioned;
(5)查看目標(biāo)分區(qū)表的分區(qū)情況
hive (default)> show partitions ori_partitioned_target;
(6)如果不設(shè)置非嚴(yán)格模式,報(bào)錯(cuò)如下
FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict
9.3.9 分桶
詳見(jiàn)6.6章。
9.3.10 分區(qū)
詳見(jiàn)4.6章。
9.4 數(shù)據(jù)傾斜
9.4.1 合理設(shè)置Map數(shù)
1)通常情況下,作業(yè)會(huì)通過(guò)input的目錄產(chǎn)生一個(gè)或者多個(gè)map任務(wù)。
主要的決定因素有:input的文件總個(gè)數(shù),input的文件大小,集群設(shè)置的文件塊大小。
2)是不是map數(shù)越多越好?
答案是否定的。如果一個(gè)任務(wù)有很多小文件(遠(yuǎn)遠(yuǎn)小于塊大小128m),則每個(gè)小文件也會(huì)被當(dāng)做一個(gè)塊,用一個(gè)map任務(wù)來(lái)完成,而一個(gè)map任務(wù)啟動(dòng)和初始化的時(shí)間遠(yuǎn)遠(yuǎn)大于邏輯處理的時(shí)間,就會(huì)造成很大的資源浪費(fèi)。而且,同時(shí)可執(zhí)行的map數(shù)是受限的。
3)是不是保證每個(gè)map處理接近128m的文件塊,就高枕無(wú)憂(yōu)了?
答案也是不一定。比如有一個(gè)127m的文件,正常會(huì)用一個(gè)map去完成,但這個(gè)文件只有一個(gè)或者兩個(gè)小字段,卻有幾千萬(wàn)的記錄,如果map處理的邏輯比較復(fù)雜,用一個(gè)map任務(wù)去做,肯定也比較耗時(shí)。
針對(duì)上面的問(wèn)題2和3,我們需要采取兩種方式來(lái)解決:即減少map數(shù)和增加map數(shù);
9.4.2 小文件進(jìn)行合并
在map執(zhí)行前合并小文件,減少map數(shù):CombineHiveInputFormat具有對(duì)小文件進(jìn)行合并的功能(系統(tǒng)默認(rèn)的格式)。HiveInputFormat沒(méi)有對(duì)小文件合并功能。
set hive.input.format= org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
9.4.3 復(fù)雜文件增加Map數(shù)
當(dāng)input的文件都很大,任務(wù)邏輯復(fù)雜,map執(zhí)行非常慢的時(shí)候,可以考慮增加Map數(shù),來(lái)使得每個(gè)map處理的數(shù)據(jù)量減少,從而提高任務(wù)的執(zhí)行效率。
增加map的方法為:根據(jù)computeSliteSize(Math.max(minSize,Math.min(maxSize,blocksize)))=blocksize=128M公式,調(diào)整maxSize最大值。讓maxSize最大值低于blocksize就可以增加map的個(gè)數(shù)。
案例實(shí)操:
(1)執(zhí)行查詢(xún)
hive (default)> select count() from emp;
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
(2)設(shè)置最大切片值為100個(gè)字節(jié)
hive (default)> set mapreduce.input.fileinputformat.split.maxsize=100;
hive (default)> select count() from emp;
Hadoop job information for Stage-1: number of mappers: 6; number of reducers: 1
9.4.4 合理設(shè)置Reduce數(shù)
1)調(diào)整reduce個(gè)數(shù)方法一
(1)每個(gè)Reduce處理的數(shù)據(jù)量默認(rèn)是256MB
set hive.exec.reducers.bytes.per.reducer=256000000
(2)每個(gè)任務(wù)最大的reduce數(shù),默認(rèn)為1009
set hive.exec.reducers.max=1009
(3)計(jì)算reducer數(shù)的公式
N=min(參數(shù)2=1009,總輸入數(shù)據(jù)量/參數(shù)1=?)
2)調(diào)整reduce個(gè)數(shù)方法二
在hadoop的mapred-default.xml文件中修改
設(shè)置每個(gè)job的Reduce個(gè)數(shù)
set mapreduce.job.reduces = 5;
3)reduce個(gè)數(shù)并不是越多越好
1)過(guò)多的啟動(dòng)和初始化reduce也會(huì)消耗時(shí)間和資源;
2)另外,有多少個(gè)reduce,就會(huì)有多少個(gè)輸出文件,如果生成了很多個(gè)小文件,那么如果這些小文件作為下一個(gè)任務(wù)的輸入,則也會(huì)出現(xiàn)小文件過(guò)多的問(wèn)題;
在設(shè)置reduce個(gè)數(shù)的時(shí)候也需要考慮這兩個(gè)原則:處理大數(shù)據(jù)量利用合適的reduce數(shù);使單個(gè)reduce任務(wù)處理數(shù)據(jù)量大小要合適;
9.5 并行執(zhí)行
Hive會(huì)將一個(gè)查詢(xún)轉(zhuǎn)化成一個(gè)或者多個(gè)階段。這樣的階段可以是MapReduce階段、抽樣階段、合并階段、limit階段。或者Hive執(zhí)行過(guò)程中可能需要的其他階段。默認(rèn)情況下,Hive一次只會(huì)執(zhí)行一個(gè)階段。不過(guò),某個(gè)特定的job可能包含眾多的階段,而這些階段可能并非完全互相依賴(lài)的,也就是說(shuō)有些階段是可以并行執(zhí)行的,這樣可能使得整個(gè)job的執(zhí)行時(shí)間縮短。不過(guò),如果有更多的階段可以并行執(zhí)行,那么job可能就越快完成。
通過(guò)設(shè)置參數(shù)hive.exec.parallel值為true,就可以開(kāi)啟并發(fā)執(zhí)行。不過(guò),在共享集群中,需要注意下,如果job中并行階段增多,那么集群利用率就會(huì)增加。
set hive.exec.parallel=true; //打開(kāi)任務(wù)并行執(zhí)行,默認(rèn)false
set hive.exec.parallel.thread.number=16; //同一個(gè)sql允許最大并行度,默認(rèn)為8。
當(dāng)然,得是在系統(tǒng)資源比較空閑的時(shí)候才有優(yōu)勢(shì),否則,沒(méi)資源,并行也起不來(lái)。
9.6 嚴(yán)格模式
Hive提供了一個(gè)嚴(yán)格模式,可以防止用戶(hù)執(zhí)行那些可能意向不到的不好的影響的查詢(xún)。
通過(guò)設(shè)置屬性hive.mapred.mode值為默認(rèn)是非嚴(yán)格模式nonstrict 。開(kāi)啟嚴(yán)格模式需要修改hive.mapred.mode值為strict,開(kāi)啟嚴(yán)格模式可以禁止3種類(lèi)型的查詢(xún)。
<property>
<name>hive.mapred.mode</name>
<value>strict</value>
<description>
The mode in which the Hive operations are being performed.
In strict mode, some risky queries are not allowed to run. They include:
Cartesian Product.
No partition being picked up for a query.
Comparing bigints and strings.
Comparing bigints and doubles.
Orderby without limit.
</description>
</property>
1)對(duì)于分區(qū)表,除非where語(yǔ)句中含有分區(qū)字段過(guò)濾條件來(lái)限制范圍,否則不允許執(zhí)行。換句話(huà)說(shuō),就是用戶(hù)不允許掃描所有分區(qū)。進(jìn)行這個(gè)限制的原因是,通常分區(qū)表都擁有非常大的數(shù)據(jù)集,而且數(shù)據(jù)增加迅速。沒(méi)有進(jìn)行分區(qū)限制的查詢(xún)可能會(huì)消耗令人不可接受的巨大資源來(lái)處理這個(gè)表。
2)對(duì)于使用了order by語(yǔ)句的查詢(xún),要求必須使用limit語(yǔ)句。因?yàn)閛rder by為了執(zhí)行排序過(guò)程會(huì)將所有的結(jié)果數(shù)據(jù)分發(fā)到同一個(gè)Reducer中進(jìn)行處理,強(qiáng)制要求用戶(hù)增加這個(gè)LIMIT語(yǔ)句可以防止Reducer額外執(zhí)行很長(zhǎng)一段時(shí)間。
3)限制笛卡爾積的查詢(xún)。對(duì)關(guān)系型數(shù)據(jù)庫(kù)非常了解的用戶(hù)可能期望在執(zhí)行JOIN查詢(xún)的時(shí)候不使用ON語(yǔ)句而是使用where語(yǔ)句,這樣關(guān)系數(shù)據(jù)庫(kù)的執(zhí)行優(yōu)化器就可以高效地將WHERE語(yǔ)句轉(zhuǎn)化成那個(gè)ON語(yǔ)句。不幸的是,Hive并不會(huì)執(zhí)行這種優(yōu)化,因此,如果表足夠大,那么這個(gè)查詢(xún)就會(huì)出現(xiàn)不可控的情況。
9.7 JVM重用
JVM重用是Hadoop調(diào)優(yōu)參數(shù)的內(nèi)容,其對(duì)Hive的性能具有非常大的影響,特別是對(duì)于很難避免小文件的場(chǎng)景或task特別多的場(chǎng)景,這類(lèi)場(chǎng)景大多數(shù)執(zhí)行時(shí)間都很短。
Hadoop的默認(rèn)配置通常是使用派生JVM來(lái)執(zhí)行map和Reduce任務(wù)的。這時(shí)JVM的啟動(dòng)過(guò)程可能會(huì)造成相當(dāng)大的開(kāi)銷(xiāo),尤其是執(zhí)行的job包含有成百上千task任務(wù)的情況。JVM重用可以使得JVM實(shí)例在同一個(gè)job中重新使用N次。N的值可以在Hadoop的mapred-site.xml文件中進(jìn)行配置。通常在10-20之間,具體多少需要根據(jù)具體業(yè)務(wù)場(chǎng)景測(cè)試得出。
<property>
<name>mapreduce.job.jvm.numtasks</name>
<value>10</value>
<description>How many tasks to run per jvm. If set to -1, there is
no limit.
</description>
</property>
這個(gè)功能的缺點(diǎn)是,開(kāi)啟JVM重用將一直占用使用到的task插槽,以便進(jìn)行重用,直到任務(wù)完成后才能釋放。如果某個(gè)“不平衡的”job中有某幾個(gè)reduce task執(zhí)行的時(shí)間要比其他Reduce task消耗的時(shí)間多的多的話(huà),那么保留的插槽就會(huì)一直空閑著卻無(wú)法被其他的job使用,直到所有的task都結(jié)束了才會(huì)釋放。
9.8 推測(cè)執(zhí)行
在分布式集群環(huán)境下,因?yàn)槌绦駼ug(包括Hadoop本身的bug),負(fù)載不均衡或者資源分布不均等原因,會(huì)造成同一個(gè)作業(yè)的多個(gè)任務(wù)之間運(yùn)行速度不一致,有些任務(wù)的運(yùn)行速度可能明顯慢于其他任務(wù)(比如一個(gè)作業(yè)的某個(gè)任務(wù)進(jìn)度只有50%,而其他所有任務(wù)已經(jīng)運(yùn)行完畢),則這些任務(wù)會(huì)拖慢作業(yè)的整體執(zhí)行進(jìn)度。為了避免這種情況發(fā)生,Hadoop采用了推測(cè)執(zhí)行(Speculative Execution)機(jī)制,它根據(jù)一定的法則推測(cè)出“拖后腿”的任務(wù),并為這樣的任務(wù)啟動(dòng)一個(gè)備份任務(wù),讓該任務(wù)與原始任務(wù)同時(shí)處理同一份數(shù)據(jù),并最終選用最先成功運(yùn)行完成任務(wù)的計(jì)算結(jié)果作為最終結(jié)果。
設(shè)置開(kāi)啟推測(cè)執(zhí)行參數(shù):Hadoop的mapred-site.xml文件中進(jìn)行配置
<property>
<name>mapreduce.map.speculative</name>
<value>true</value>
<description>If true, then multiple instances of some map tasks
may be executed in parallel.</description>
</property>
<property>
<name>mapreduce.reduce.speculative</name>
<value>true</value>
<description>If true, then multiple instances of some reduce tasks
may be executed in parallel.</description>
</property>
不過(guò)hive本身也提供了配置項(xiàng)來(lái)控制reduce-side的推測(cè)執(zhí)行:
<property>
<name>hive.mapred.reduce.tasks.speculative.execution</name>
<value>true</value>
<description>Whether speculative execution for reducers should be turned on. </description>
</property>
關(guān)于調(diào)優(yōu)這些推測(cè)執(zhí)行變量,還很難給一個(gè)具體的建議。如果用戶(hù)對(duì)于運(yùn)行時(shí)的偏差非常敏感的話(huà),那么可以將這些功能關(guān)閉掉。如果用戶(hù)因?yàn)檩斎霐?shù)據(jù)量很大而需要執(zhí)行長(zhǎng)時(shí)間的map或者Reduce task的話(huà),那么啟動(dòng)推測(cè)執(zhí)行造成的浪費(fèi)是非常巨大大。
9.9 壓縮
詳見(jiàn)第8章。
9.10 執(zhí)行計(jì)劃(Explain)
1)基本語(yǔ)法
EXPLAIN [EXTENDED | DEPENDENCY | AUTHORIZATION] query
2)案例實(shí)操
(1)查看下面這條語(yǔ)句的執(zhí)行計(jì)劃
hive (default)> explain select * from emp;
hive (default)> explain select deptno, avg(sal) avg_sal from emp group by deptno;
(2)查看詳細(xì)執(zhí)行計(jì)劃
hive (default)> explain extended select * from emp;
hive (default)> explain extended select deptno, avg(sal) avg_sal from emp group by deptno;
EXPLAIN字段:
Table:顯示這一行的數(shù)據(jù)是關(guān)于哪張表的
possible_keys:顯示可能應(yīng)用在這張表中的索引。如果為空,沒(méi)有可能的索引。可以為相關(guān)的域從WHERE語(yǔ)句中選擇一個(gè)合適的語(yǔ)句
key:實(shí)際使用的索引。如果為NULL,則沒(méi)有使用索引。MYSQL很少會(huì)選擇優(yōu)化不足的索引,此時(shí)可以在SELECT語(yǔ)句中使用USE INDEX(index)來(lái)強(qiáng)制使用一個(gè)索引或者用IGNORE INDEX(index)來(lái)強(qiáng)制忽略索引
key_len:使用的索引的長(zhǎng)度。在不損失精確性的情況下,長(zhǎng)度越短越好
ref:顯示索引的哪一列被使用了,如果可能的話(huà),是一個(gè)常數(shù)
rows:MySQL認(rèn)為必須檢索的用來(lái)返回請(qǐng)求數(shù)據(jù)的行數(shù)
type:這是最重要的字段之一,顯示查詢(xún)使用了何種類(lèi)型。從最好到最差的連接類(lèi)型為system、const、eq_reg、ref、range、index和ALL
system、const:可以將查詢(xún)的變量轉(zhuǎn)為常量. 如id=1; id為 主鍵或唯一鍵.
eq_ref:訪問(wèn)索引,返回某單一行的數(shù)據(jù).(通常在聯(lián)接時(shí)出現(xiàn),查詢(xún)使用的索引為主鍵或惟一鍵)
ref:訪問(wèn)索引,返回某個(gè)值的數(shù)據(jù).(可以返回多行) 通常使用=時(shí)發(fā)生
range:這個(gè)連接類(lèi)型使用索引返回一個(gè)范圍中的行,比如使用>或<查找東西,并且該字段上建有索引時(shí)發(fā)生的情況(注:不一定好于index)
index:以索引的順序進(jìn)行全表掃描,優(yōu)點(diǎn)是不用排序,缺點(diǎn)是還要全表掃描
ALL:全表掃描,應(yīng)該盡量避免
Extra:關(guān)于MYSQL如何解析查詢(xún)的額外信息,主要有以下幾種
using index:只用到索引,可以避免訪問(wèn)表.
using where:使用到where來(lái)過(guò)慮數(shù)據(jù). 不是所有的where clause都要顯示using where. 如以=方式訪問(wèn)索引.
using tmporary:用到臨時(shí)表
using filesort:用到額外的排序. (當(dāng)使用order by v1,而沒(méi)用到索引時(shí),就會(huì)使用額外的排序)
range checked for eache record(index map:N):沒(méi)有好的索引.