加载中…
个人资料
小亮
小亮
  • 博客等级:
  • 博客积分:0
  • 博客访问:2,262
  • 关注人气:2
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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 bytes100兆,mapred.map.tasks默认值为2,那么分片大小就是50兆;如果我们把mapred.map.tasks改成1,那分片大小就变成了64兆。

计算好了分片大小之后接下来计算map数。Map数的计算是以文件为单位的,针对每一个文件做一个循环:

1.   文件大小/splitsize>1.1,创建一个split,这个split的大小=splitsize,文件剩余大小=文件大小-splitsize

2.   文件剩余大小/splitsize<1.1,剩余的部分作为一个split

举几个例子:

1.   input只有一个文件,大小为100M,splitsize=blocksize,map数为2,第一个map处理的分片为64M,第二个为36M

2.   input只有一个文件,大小为65M,splitsize=blocksize,则map数为1,处理的分片大小为65M (因为65/64<1.1

3.   input只有一个文件,大小为129M,splitsize=blocksize,则map数为2,第一个map处理的分片为64M,第二个为65M

4.   input有两个文件,大小为100M20M,splitsize=blocksize,map数为3,第一个文件分为两个map,第一个map处理的分片为64M,第二个为36M,第二个文件分为一个map,处理的分片大小为20M

5.   input10个文件,每个大小10Msplitsize=blocksize,则map数为10,每个map处理的分片大小为10M

再看2个更特殊的例子:

1.   输入文件有2个,分别为40M20Mdfs.block.size = 64M mapred.map.tasks采用默认值2。那么splitSize = 30M map数实际为3,第一个文件分为2map,第一个map处理的分片大小为30M,第二个map10M;第二个文件分为1map,大小为20M

2.   输入文件有2个,分别为40M20Mdfs.block.size = 64M mapred.map.tasks手工设置为1

那么splitSize = 60M map数实际为2,第一个文件分为1map,处理的分片大小为40M;第二个文件分为1map,大小为20M

通过这2个特殊的例子可以看到mapred.map.tasks并不是设置的越大,JOB执行的效率就越高。同时,Hadoop在处理小文件时效率也会变差。

根据分片与map数的计算方法可以得出结论,一个map处理的分片最大不超过dfs.block.size * 1.1 ,默认情况下是70.4兆。但是有2个特例:

1.   Hive中合并小文件的map only JOB,此JOB只会有一个或很少的几个map

2.   输入文件格式为压缩的Text File,因为压缩的文本格式不知道如何拆分,所以也只能用一个map


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.getBucketedColsByDirectory().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之类的。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有