我们每次执行hive的hql时,shell里都会提示一段话:
- ...
- number of reduce tasks not specified. estimated from input data size: 500
- in order to change the average load for a reducer (in bytes):
- set hive.exec.reducers.bytes.per.reducer=
- in order to limit the maximum number of reducers:
- set hive.exec.reducers.max=
- in order to set a constant number of reducers:
- set mapred.reduce.tasks=
- ...
这个是调优的经常手段,主要有一下三个属性来决定
hive.exec.reducers.bytes.per.reducer 这个参数控制一个job会有多少个reducer来处理,依据的是输入文件的总大小。默认1gb。
this controls how many reducers a map-reduce job should have, depending on the total size of input files to the job. default is 1gb
hive.exec.reducers.max 这个参数控制最大的reducer的数量, 如果 input / bytes per reduce > max 则会启动这个参数所指定的reduce个数。 这个并不会影响mapre.reduce.tasks参数的设置。默认的max是999。this controls the maximum number of reducers a map-reduce job can have. if input_file_size divided by "hive.exec.bytes.per.reducer" is greater than this value, the map-reduce job will have this value as the number reducers. note this does not affect the number of reducers directly specified by the user through "mapred.reduce.tasks" and query hints
mapred.reduce.tasks 这个参数如果指定了,hive就不会用它的estimation函数来自动计算reduce的个数,而是用这个参数来启动reducer。默认是-1.this overrides the hadoop configuration to make sure we enable the estimation of the number of reducers by the size of the input files. if this value is non-negative, then hive will pass this number directly to map-reduce jobs instead of doing the estimation.
reduce的个数设置其实对执行效率有很大的影响:
1、如果reduce太少: 如果数据量很大,会导致这个reduce异常的慢,从而导致这个任务不能结束,也有可能会oom
2、如果reduce太多: 产生的小文件太多,合并起来代价太高,namenode的内存占用也会增大。
如果我们不指定mapred.reduce.tasks, hive会自动计算需要多少个reducer。
计算的公式: reduce个数 = inputfilesize / bytes per reducer
这个数个粗略的公式,详细的公式在:
common/src/java/org/apache/hadoop/hive/conf/hiveconf.java
我们先看下:
1、计算输入文件大小的方法:其实很简单,遍历每个路径获取length,累加。
- * calculate the total size of input files.
- * @param job the hadoop job conf.
- * @return the total size in bytes.
- * @throws ioexception
- */
- public static long gettotalinputfilesize(jobconf job, mapredwork work) throws ioexception {
- long r = 0;
- filesystem fs = filesystem.get(job);
- // for each input path, calculate the total size.
- for (string path: work.getpathtoaliases().keyset()) {
- contentsummary cs = fs.getcontentsummary(new path(path));
- r = cs.getlength();
- }
- return r;
- }
2、估算reducer的个数,及计算公式:
注意最重要的一句话: int reducers = (int)((totalinputfilesize bytesperreducer - 1) / bytesperreducer);
- /**
- * estimate the number of reducers needed for this job, based on job input,
- * and configuration parameters.
- * @return the number of reducers.
- */
- public int estimatenumberofreducers(hiveconf hive, jobconf job, mapredwork work) throws ioexception {
- long bytesperreducer = hive.getlongvar(hiveconf.confvars.bytesperreducer);
- int maxreducers = hive.getintvar(hiveconf.confvars.maxreducers);
- long totalinputfilesize = gettotalinputfilesize(job, work);
-
- 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);
- return reducers;
- }
3、真正的计算流程代码:- /**
- * set the number of reducers for the mapred work.
- */
- protected void setnumberofreducers() throws ioexception {
- // this is a temporary hack to fix things that are not fixed in the compiler
- integer numreducersfromwork = work.getnumreducetasks();
-
- if (numreducersfromwork != null && numreducersfromwork >= 0) {
- log.info("number of reduce tasks determined at compile: " work.getnumreducetasks());
- } else if(work.getreducer() == null) {
- log.info("number of reduce tasks not specified. defaulting to 0 since there's no reduce operator");
- work.setnumreducetasks(integer.valueof(0));
- } else {
- int reducers = estimatenumberofreducers(conf, job, work);
- work.setnumreducetasks(reducers);
- log.info("number of reduce tasks not specified. estimated from input data size: " reducers);
- }
- }
这就是reduce个数计算的原理。
by the way :
今天中午在群里看到一位朋友问到:
当前hive的reduce个数的设定是依据map阶段输入的数据量大小来除以每一个reduce能够处理的数据量来决定有多少个的,但是考虑到经过map阶段处理的数据很可能可输入数据相差很大,这样子的话,当初设定的reduce个数感觉不太合理。。。请问hive当前能支持依据map阶段输出数据量的大小决定reduce个数么?(但是,reduce任务的开启是在有某些map任务完成就会开始的,所以要等到所有map全部执行完成再统计数据量来决定reduce个数感觉也不太合理) 有没有什么好方法?谢谢
这个问题的大意是,reducer个数是根据输入文件的大小来估算出来的,但是实际情况下,map的输出文件才是真正要到reduce上计算的数据量,如何依据map的阶段输出数据流觉得reduce的个数,才是实际的问题。
我给出的思路是:
1、hack下源码,计算下每个map输出的大小×map个数不就估算出map总共输出的数据量了吗?不用等它结束,因为每个map的处理量是一定的。
2、你把源码的 总输入量 / 每个reduce处理量 改成 总输出量 / 每个reduce处理量不就行了?(总输出=每个map输出文件的大小×map个数)
ps:最后朋友提到:
建议不错,虽然有一定误差。 谢谢。 不过,如果filter push down的话,每一个map的输出大小差别可能比较大。。。而且filter push down 现在应该是hive默认支持的了
大意是,还是会有一些误差,谓词下推可能会影响map的输出大小。
本文权且当作回顾加备忘,如有不对之处,请高手指正。
—eof——
posted on 2018-03-07 11:21
xzc 阅读(1361)
评论(1) 编辑 收藏 所属分类:
hadoop