hive是如何判断设置map、reduce个数的
(2014-01-23 08:46:34)
标签:
it |
分类: HIVE |
hive map个数判断
mapred.map.tasks(无效)直接设置任务个数无效
影响因子
input_file_num:文件数量
input_file_size:文件大小
mapred.max.split.size(default 265M)
mapred.min.split.size(default 1B)
切割算法(TextInputFormat切分)
splitSize=max[minSize,min(maxSize,blockSize)]
dfs.block.size
压缩方式
map参数影响
map个数并不是越大越好,或者越小越好。
因为map启动、初始化大概需要1~5秒时间
当然map数也不是越小越好,一般mapred.max.split.size默认256M就比较好。
如果涉及到cube、rollup、cpu密集型作业,可适当降低mapred.max.split.size的大小,增大map数
Hadoop中在计算一个JOB需要的map数之前首先要计算分片的大小。计算分片大小的公式是:
goalSize = totalSize / mapred.map.tasks
minSize = max {mapred.min.split.size, minSplitSize}
splitSize = max (minSize, min(goalSize, dfs.block.size))
totalSize是一个JOB的所有map总的输入大小,即Map input bytes。参数mapred.map.tasks的默认值是2,我们可以更改这个参数的值。计算好了goalSize之后还要确定上限和下限。
下限是max {mapred.min.split.size, minSplitSize} 。参数mapred.min.split.size的默认值为1个字节,minSplitSize随着File Format的不同而不同。
上限是dfs.block.size,它的默认值是64兆。
举几个例子,例如Map input bytes是100兆,mapred.map.tasks默认值为2,那么分片大小就是50兆;如果我们把mapred.map.tasks改成1,那分片大小就变成了64兆。
计算好了分片大小之后接下来计算map数。Map数的计算是以文件为单位的,针对每一个文件做一个循环:
1.
2.
举几个例子:
1.
2.
3.
4.
5.
再看2个更特殊的例子:
1.
2.
那么splitSize = 60M ,map数实际为2,第一个文件分为1个map,处理的分片大小为40M;第二个文件分为1个map,大小为20M
通过这2个特殊的例子可以看到mapred.map.tasks并不是设置的越大,JOB执行的效率就越高。同时,Hadoop在处理小文件时效率也会变差。
根据分片与map数的计算方法可以得出结论,一个map处理的分片最大不超过dfs.block.size * 1.1 ,默认情况下是70.4兆。但是有2个特例:
1.
2.
hive reduce个数判断
mapred.reduce.tasks=#number(可手工设置)
影响因子
hive.exec.reducers.max (hive设置的最大可用reducer)
hive.exec.reducers.bytes.per.reducer (hive默认每1G文件分配一个reduce)
分配算法
num_reduce_tasks=min[maxReucers,input.size/perReducer]
reduce最大参数设置建议计算公式
(集群总Reduce个槽位个数*1.5)/(执行中的查询平均数)
hive是按照输入数据量大小确定reduce个数
默认hive.exec.reducers.bytes.per.reducer=1000,000,000
1G(一个reduce处理1G~10G之间数据量,比较合适)
也就是说你如果有20G的数据,将会启动20个reduce,同时会与1、设置的最大maxReducers做比较
if (totalInputFileSize != inputSummary.getLength()) {LOG.info("BytesPerReducer=" + bytesPerReducer + " maxReducers=" + maxReducers + " estimated totalInputFileSize=" + totalInputFileSize); } else { LOG.info("BytesPerReducer=" + bytesPerReducer + " maxReducers=" + maxReducers + " totalInputFileSize=" + totalInputFileSize); } int reducers = (int) ((totalInputFileSize + bytesPerReducer - 1) / bytesPerReducer); reducers = Math.max(1, reducers); reducers = Math.min(maxReducers, reducers);
另外如果你开启了hive.exec.infer.bucket.sort.num.buckets.power.two参数,hive会设置reduce个数为2的N次方,
比如原本27个reduece,hive就会开启32个。有利有弊,具体优缺点可以参考hive/conf/hive-default.xml.template
// If this map reduce job writes final data to a table and bucketing is being inferred, // and the user has configured Hive to do this, make sure the number of reducers is a // power of two if (conf.getBoolVar(HiveConf.ConfVars.HIVE_INFER_BUCKET_SORT_NUM_BUCKETS_POWER_TWO) && work.isFinalMapRed() && !work.getBucketedColsByDirecto ry().isEmpty()) { int reducersLog = (int)(Math.log(reducers) / Math.log(2)) + 1; int reducersPowerTwo = (int)Math.pow(2, reducersLog); if (reducersPowerTwo / 2 == reducers) { return reducers; } else if (reducersPowerTwo > maxReducers) { reducers = reducersPowerTwo / 2; } else { // Otherwise use the smallest power of two greater than the original number of reducers reducers = reducersPowerTwo; } }
reduce参数影响
不过有些情况下,查询的map阶段会产生比实际输入数据量要多得多的数据。如果map阶段产生的数据量非常多,那么根据输入的数据量大小来确定的reduce个数就显得有些少了,
比如数据魔方cube、rollup之类。同样地,map阶段也可能过滤掉输入数据集中的很大一部分数据,这个时候就只需要少量的reduce就满足计算,比如wordcount、pv之类的。