Hive优化策略
hive優化目標
在有限的資源下,運行效率高。常見問題
數據傾斜、Map數設置、Reduce數設置等
hive運行
查看運行計劃
explain [extended] hql
例子
explain select no,count(*) from testudf group by no; explain extended select no,count(*) from testudf group by no;運行階段
STAGE DEPENDENC1ES:
Stage-1 is a root stage
Stage-0 is a root stage
Map階段
Map Operator Tree:TableScanalias: testudfStatistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats: NONESelect Operatorexpressions: no (type: string)outputColumnNames: noStatistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats : NONEGroup By Operatoraggregations: count()keys: no (type: string)mode: hashoutputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column sta ts: NONEReduce Output Operatorkey expressions: _col0 (type: string)sort order: +Map-reduce partition columns: _col0 (type: string)Statistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column s tats: NONEvalue expressions: _col1 (type: bigint)reduce階段
Reduce Operator Tree:Group By Operatoraggregations: count(VALUE._col0)keys: KEY._col0 (type: string)mode: mergepartialoutputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONESelect Operatorexpressions: _col0 (type: string), _col1 (type: bigint)outputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONEFile Output Operatorcompressed: falseStatistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NO NEtable:input format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutput Formatserde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDehive (liguodong)> explain extended select no,count(*) from testudf group by no; OK Explain ABSTRACT SYNTAX TREE:TOK_QUERYTOK_FROMTOK_TABREFTOK_TABNAMEtestudfTOK_INSERTTOK_DESTINATIONTOK_DIRTOK_TMP_FILETOK_SELECTTOK_SELEXPRTOK_TABLE_OR_COLnoTOK_SELEXPRTOK_FUNCTIONSTARcountTOK_GROUPBYTOK_TABLE_OR_COLnoSTAGE DEPENDENCIES:Stage-1 is a root stageStage-0 is a root stageSTAGE PLANS:Stage: Stage-1Map ReduceMap Operator Tree:TableScanalias: testudfStatistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats: NONEGatherStats: falseSelect Operatorexpressions: no (type: string)outputColumnNames: noStatistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats: NONEGroup By Operatoraggregations: count()keys: no (type: string)mode: hashoutputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats: NONEReduce Output Operatorkey expressions: _col0 (type: string)sort order: +Map-reduce partition columns: _col0 (type: string)Statistics: Num rows: 0 Data size: 30 Basic stats: PARTIAL Column stats: NONEtag: -1value expressions: _col1 (type: bigint)Path -> Alias:hdfs://nameservice1/user/hive/warehouse/liguodong.db/testudf [testudf]Path -> Partition:hdfs://nameservice1/user/hive/warehouse/liguodong.db/testudfPartitionbase file name: testudfinput format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatproperties:COLUMN_STATS_ACCURATE truebucket_count -1columns no,numcolumns.commentscolumns.types string:stringfield.delimfile.inputformat org.apache.hadoop.mapred.TextInputFormatfile.outputformat org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatline.delimlocation hdfs://nameservice1/user/hive/warehouse/liguodong.db/testudfname liguodong.testudfnumFiles 1numRows 0rawDataSize 0serialization.ddl struct testudf { string no, string num}serialization.formatserialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDetotalSize 30transient_lastDdlTime 1437374988serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDeinput format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatproperties:COLUMN_STATS_ACCURATE truebucket_count -1columns no,numcolumns.commentscolumns.types string:stringfield.delimfile.inputformat org.apache.hadoop.mapred.TextInputFormatfile.outputformat org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatline.delimlocation hdfs://nameservice1/user/hive/warehouse/liguodong.db/testudfname liguodong.testudfnumFiles 1numRows 0rawDataSize 0serialization.ddl struct testudf { string no, string num}serialization.formatserialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDetotalSize 30transient_lastDdlTime 1437374988serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDename: liguodong.testudfname: liguodong.testudfTruncated Path -> Alias:/liguodong.db/testudf [testudf]Needs Tagging: falseReduce Operator Tree:Group By Operatoraggregations: count(VALUE._col0)keys: KEY._col0 (type: string)mode: mergepartialoutputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONESelect Operatorexpressions: _col0 (type: string), _col1 (type: bigint)outputColumnNames: _col0, _col1Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONEFile Output Operatorcompressed: falseGlobalTableId: 0directory: hdfs://nameservice1/tmp/hive-root/hive_2015-07-21_09-51-37_330_7990199479532530033-1/-mr-10000/.hive-staging_hive_2015-07-21_09-51-37_330_7990199479532530033-1/-ext-10001NumFilesPerFileSink: 1Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONEStats Publishing Key Prefix: hdfs://nameservice1/tmp/hive-root/hive_2015-07-21_09-51-37_330_7990199479532530033-1/-mr-10000/.hive-staging_hive_2015-07-21_09-51-37_330_7990199479532530033-1/-ext-10001/table:input format: org.apache.hadoop.mapred.TextInputFormatoutput format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatproperties:columns _col0,_col1columns.types string:bigintescape.delim \hive.serialization.extend.nesting.levels trueserialization.format 1serialization.lib org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDeserde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDeTotalFiles: 1GatherStats: falseMultiFileSpray: falseStage: Stage-0Fetch Operatorlimit: -1
HIVE運行過程
hive表優化
分區
靜態分區
動態分區
分桶
set hive.enforce.bucketing=true; set hive.enforce.sorting=true;表優化數據目標:同樣數據盡量聚集在一起
Hive job優化
并行化運行
每一個查詢被hive轉化成多個階段,有些階段關聯性不大,則能夠并行化運行,降低運行時問。
set hive.exec.parallel=true; set hive.exec.parallel.thread.number=8;eg:
select num from (select count(city) as num from city union all select count(province) as num from province )tmp;本地化運行
set hive.exec.mode.local.auto=true;當一個job滿足例如以下條件才干真正使用本地模式:
1.job的輸入數據大小必須小于參數:
hive.exec.mode.local.inputbytes.max(默認128MB)
2.job的map數必須小于參數:
hive.exec.mode.local.auto.tasks.max(默認4)
3.job的reduce數必須為0或者1
job合并輸入小文件
set hive.input.format= org.apache.hadoop.hive.ql.io.CombineHiveInputFormat合并文件數由mapred.max.split.size限制的大小決定。
job合并輸出小文件
set hive.merge.smallfiles.avgsize=256000000;當輸出文件平均大小小于該值。啟動新job合并文件
set hive.merge.size.per.task=64000000;合并之后的文件大小
JVM重利用
set mapred.job.reuse.jvm.num.tasks=20;
JVM重利用能夠是job長時間保留slot,直到作業結束,這在對于有較多任務和較多小文件的任務是很有意義的,降低運行時間。當然這個值不能設置過大,由于有些作業會有reduce任務,假設reduce任務沒有完畢,則map任務占用的slot不能釋放。其它的作業可能就須要等待。
壓縮數據
中間壓縮就是處理hive查詢的多個job之間的數據。對中間壓縮,
最好選擇一個節省CPU耗時的壓縮方式。
終于的輸出也能夠壓縮,選擇一個壓縮效果比較好的,節省了磁盤空間,可是cpu比較耗時。
set hive.exec.compress.output=true; set mapred.output.compression.codec= org.apache.hadoop.io.compress.GzipCodec; set mapred.output.compression.type=BLOCK:Hive SQL語句優化
join優化
hive.optimize.skewjoin=true; 假設是join過程出現傾斜應該設置為true
set hive.skewjoin.key=100000; 這個是join的鍵相應的記錄條數超過這個值則會進行優化。
mapjoin
自己主動運行 set hive.auto.convert.join=true; hive.mapjoin.smalltable.filesize默認值是25mb 手動運行 select /*+mapjoin(A)*/ f.a,f.b from A t join B f on(f.a==t.a)簡單總結一下,mapjoin的使用場景:
1、關聯操作中有一張表很小
2、(不等值)的鏈接操作時
注:小表盡量設置小一點或用手動方式。
bucket join
兩個表以同樣方式劃分捅。
兩個表的桶個數是倍數關系。
改動where的位置進行優化
join優化前 select m.cid, u.id from order m join customer u on m.cid=u.id where m.dt='2013-12-12join優化后 select m.cid, u.id from (select cid from order where dt='2013-12-12') m join customer u on m.cid=u.id; 這樣就能降低join連接的數據量。group by優化
hive.groupby.skewindata=true;
假設是group by過程出現傾斜應該設置為true。
set hive.groupby.mapaggr.checkinterval=100000;
這個是group的鍵相應的記錄條數超過這個值則會進行優化。
count distinct優化
優化前(啟動一個job,數據量大時,一個reduce負載過重)
select count(distinct id) from tablename;
優化后(啟動兩個job)
select count(1) from (select distinct id from tablename)tmp; select count(1) from (select id from tablename group by id)tmp;union all優化
優化前 select a,sum(b),count(distinct c),count(distinct d) from test group by a;優化后 select a, sum(b) as b,count(c) as c, count(d) as d from( select a, 0 as b, c, null as d from test group by a,c union all select a, 0 as b, null as c, d from test group by a,d union all select a,b,null as c,null as d from test )tmpl group by a;Hive Map/Reduce優化
Map優化
改動map個數進行優化
直接設置mapred.map.tasks無效
set mapred.map.tasks=10。
map個數的計算過程
(1)默認map個數
default_num=total_size/block_size;
(2)期望大小
goal_num=mapred.map.tasks;
(3)設置處理的文件大小
split_size=max(mapred.min.split.size,b1ock_size); split_num=total_size/split_size;(4)計算的map個數
compute_map_num=min(split_num,max(default_num,goal_num))
經過以上的分析。在設置map個數的時候,能夠簡單的總結為下面幾點:
1)假設想添加map個數,則設置mapred.map.tasks為一個較大的值。
2)假設想減小map個數。則設置mapred.min.split.size為一個較大的值。有例如以下兩種情況:
情況1:輸入文件size巨大。但不是小文件增大mapred.min.split.size的值。
情況2:輸入文件數量巨大,且都是小文件,就是單個文件的size小于blockSize。
這樣的情況通過增大mapred.min.spllt.size不可行,
須要使用CombineFileInputFormat將多個input path合并成一個
InputSplit送給mapper處理,從而降低mapper的數量。
map端聚合
map階段進行combiner
set hive.map.aggr=true:
猜測運行
啟動多個同樣的map,誰先運行完。用誰的。
set mapred.map.tasks.speculative.execution=true
shuffle優化
依據須要配置相應參數。
Map端
io.sort.mb
io.sort.spill.percent
min.num.spill.for.combine
io.sort.factor
io.sort.record.percent
Reduce端
mapred.reduce.parallel.copies
mapred.reduce.copy.backoff
io.sort.factor
mapred.job.shuffle.input.buffer.percent
mapred.job.reduce.input.buffer.percent
Reduce優化
須要reduce操作的查詢
聚合函數sum,count,distinct
高級查詢group by,join,distribute by,cluster by…
order by比較特殊,僅僅須要一個reduce,設置reduce個數無效。
判斷運行
設置mapred.reduce.tasks.speculative.execution或者hive.mapred.reduce.tasks.speculative.execution效果都一樣。
設置Reduce
set mapred.reduce.tasks=10; 直接設置
hive.exec.reducers.max 默認:999
hive.exec.reducers.bytes.per.reducer 默認:1G
計算公式
maxReducers=hive.exec.reducers.max
perReducer=hive.exec.reducers.bytes.per.reducer
numRTasks=min[maxReducers,input.size/perReducer]
轉載于:https://www.cnblogs.com/claireyuancy/p/7224529.html
總結
- 上一篇: Discrete Logging POJ
- 下一篇: android踩坑日记1