Hadoop2.X的ChainMapper和ChainReducer

本文详细介绍了Hadoop2.x中ChainMapper和ChainReducer的使用,通过官方API文档解析和实例演示,展示了如何添加Mapper和Reducer,以及它们的执行顺序。实验部分探讨了是否需要额外设置输入输出类型,以及ChainReducer在设置Reducer前是否能添加Mapper,得出结论在ChainReducer中必须先设置Reducer再添加Mapper。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程过程中,有时候需要多个map才能完成任务,虽然知道ChainMapper和ChainReducer可以做到这一点,但是不是很清楚,从网上到的一些讲解也不是很清楚,所以自己看API文档,总结了一下。
这个是ChainMapper和ChainReducer的官方API文档。
http://hadoop.apache.org/docs/r2.7.3/api/index.html

ChainMapper

先看官方文档对ChainMapper的解释,我英语不好,英语好的自己看英文文档把,看不懂的看我的翻译把,其实也并不是很重要。

## 原文 + 翻译
The ChainMapper class allows to use multiple Mapper classes within a single Map task.
ChainMapper类允许使用多个map子类在一个map任务中,

The Mapper classes are invoked in a chained (or piped) fashion, the output of the first becomes the input of the second, and so on until the last Mapper, the output of the last Mapper will be written to the task's output.
这些map子类的执行与liunx的管道命令十分相似,第一个map的输出会成为第二个map的输入,第二个map的输出也会变成第三个map的输入,以此类推,知道最后一个map的输出会变成整个mapTask的输出,如果有reducer的话,就会传输给reducer。

The key functionality of this feature is that the Mappers in the chain do not need to be aware that they are executed in a chain. This enables having reusable specialized Mappers that can be combined to perform composite operations within a single task.
这个特性的关键功能在于,这些在管道中的map不需要知道,他们正在一个管道里执行。(我估计这句话的意思是,除了关心和上一个map的输出类型相匹配,这些map的写法与一个mapTask中仅有一个map的写法一模一样。不知道如果加上combine会怎么样,等会试试)这些可复用的专业的map可以联合起来执行一个复杂的操作在一个task任务中,


Special care has to be taken when creating chains that the key/values output by a Mapper are valid for the following Mapper in the chain. It is assumed all Mappers and the Reduce in the chain use matching output and input key and value classes as no conversion is done by the chaining code.
特别要注意,创建管道过程中要注意上一个的输出要和下一个的输入相匹配。下一句不知道怎么翻译,但是,大概意思,上一个的输出和下一个的输入要匹配。

Using the ChainMapper and the ChainReducer classes is possible to compose Map/Reduce jobs that look like [MAP+ / REDUCE MAP*]. And immediate benefit of this pattern is a dramatic reduction in disk IO.

使用ChainMapper和ChainReducer的执行流程是:至少一个map  + 一个reducer +任意个map(可以为0),这个模式可以大大减少磁盘的IO。


IMPORTANT: There is no need to specify the output key/value classes for the ChainMapper, this is done by the addMapper for the last mapper in the chain.

最重要的是:mapper管道不需要特殊的输出类型,即,你原来怎么写map,现在怎么写就行,就是这要注意这一个输出和下一个输入的匹配。并且,确保最后一个map是使用addMapper函数增加进入chain中的。

官方给出的范例是这样的,现在先不说我们的例子,因为我打算,看完ChainReducer之后,一起结合成一个例子。你只要记住使用 ChainMapper.addMapper(…)方法设置map,map执行的顺序就是 ChainMapper.addMapper(…)设置的顺序。需要注意的是,这些map都是在reducer之前执行的,最后一个map的输出就是reducer的输入。至于reducer之后要执行的map,那是使用ChainReducer类设置的。

...
 Job = new Job(conf);

 Configuration mapAConf = new Configuration(false);
 ...
 ChainMapper.addMapper(job, AMap.class, LongWritable.class, Text.class,
   Text.class, Text.class, true, mapAConf);

 Configuration mapBConf = new Configuration(false);
 ...
 ChainMapper.addMapper(job, BMap.class, Text.class, Text.class,
   LongWritable.class, Text.class, false, mapBConf);

 ...

 job.waitForComplettion(true);
 ...

addMapper的参数解释

static void addMapper(Job job, Class<? extends Mapper> klass, Class<?> inputKeyClass, Class<?> inputValueClass, Class<?> outputKeyClass, Class<?> outputValueClass, Configuration mapperConf)
## 参数的含义如下
# 1. job
# 2. 此map的class
# 3. 此map的输入的key类型
# 4. 此map的输入的value类型
# 5. 此map的输出的key类型
# 6. 此map的输出的value类型
# 7. 此map的配置文件类conf

ChainReducer

官方说明

The ChainReducer class allows to chain multiple Mapper classes after a Reducer within the Reducer task.
For each record output by the Reducer, the Mapper classes are invoked in a chained (or piped) fashion. The output of the reducer becomes the inpu
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值