多个Mapper和Reducer的Job
多个Mapper和Reducer的Job
@(Hadoop)
对于复杂的mr任务来说,只有一个map和reduce往往是不能够满足任务需求的,有可能是需要n个map之后进行reduce,reduce之后又要进行m个map。
在hadoop的mr编程中可以使用ChainMapper和ChainReducer来实现链式的Map-Reduce任务。
ChainMapper
以下为官方API文档翻译:
ChainMapper类允许在单一的Map任务中使用多个Mapper来执行任务,这些Mapper将会以链式或者管道的形式来调用。
第一个Mapper的输出即为第二个Mapper的输入,以此类推,直到最后一个Mapper则为任务的输出。
这个特性的关键功能在于,在链中的Mappers不必知道他们是否已经被执行,这可以在一个单一的任务中让一些Mapper进行重用,组合在一起完成复杂的操作。
使用的时候需要注意,每个Mapper的输出都会在下一个Mapper的输入中进行验证,这里假设所有的Mapper和Reduce都使用相匹配的key和value作为输入和输出,因为在链式执行的代码中并没有对其进行转换。
使用ChainMapper和ChainReducer可以将Map-Reduce任务组合成[MAP+ / REDUCE MAP*]的形式,这个模式最直接的好处就是可以大大减少磁盘的IO开销。
注意:没有必要为ChainMapper指定输出的key和value的类型,使用addMapper方法添加最后一个Mapper的时候回自动完成。
使用的格式:
Job = new Job(conf);
//mapA的配置,如果不是特殊配置可传入null或者共用一个conf
Configuration mapAConf = new Configuration(false);
//将Mapper加入执行链中
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函数的定义如下:
public static void addMapper(Job job,
Class<? extends Mapper> klass,
Class<?> inputKeyClass,
Class<?> inputValueClass,
Class<?> outputKeyClass,
Class<?> outputValueClass,
Configuration mapperConf)
throws IOException
ChainReducer
基本描述同ChainMapper。
对于每条reduce输出的数据,Mappers将会以链或者管道的形式调用。 ?
ChainReducer有两个基本函数可以调用,使用格式:
Job = new Job(conf);
Configuration reduceConf = new Configuration(false);
//这里是在setReducer之后才调用addMapper
ChainReducer.setReducer(job, XReduce.class, LongWritable.class, Text.class,
Text.class, Text.class, true, reduceConf);
ChainReducer.addMapper(job, CMap.class, Text.class, Text.class,
LongWritable.class, Text.class, false, null);
ChainReducer.addMapper(job, DMap.class, LongWritable.class, Text.class,
LongWritable.class, LongWritable.class, true, null);
job.waitForCompletion(true);
setReducer定义:
public static void setReducer(Job job,
Class<? extends Reducer> klass,
Class<?> inputKeyClass,
Class<?> inputValueClass,
Class<?> outputKeyClass,
Class<?> outputValueClass,
Configuration reducerConf)
addMapper定义同ChainMapper。
实际的测试过程
在demo程序测试中观察结果得到两条比较有用的结论:
- 对于reduce之后添加的Mapper,每条reduce的输出都会马上调用一次该map函数,而不是等待reduce全部完成之后再调用map,如果是有多个map,应该是一样的道理。
- reduce之后的Mapper只执行map过程,并不会有一个完整map阶段(如,map之后的排序,分组,分区等等都没有了)。
**另注:**reduce之前设置多个Mapper使用ChainMapper的addMapper,reduce之后设置多个Mapper使用ChainReducer的addMapper。
多个job连续运行
有时候链式的设置多个Mapper仍然无法满足需求,例如,有时候我们需要多个reduce过程,或者map之后的分组排序等,这就需要多个job协同进行工作。
使用的方法很简单,直接在第一个job.waitForCompletion之后再次实例化一个Job对象,按照八股文的格式进行设置即可,注意输入和输出的路径信息。
example:
Job newJob = Job.getInstance(conf, jobName + "-sort");
newJob.setJarByClass(jarClass);
FileInputFormat.setInputPaths(newJob, new Path(outPath + "/part-*"));
newJob.setInputFormatClass(TextInputFormat.class);
newJob.setMapperClass(SortMapper.class);
newJob.setMapOutputKeyClass(SortKey.class);
newJob.setMapOutputValueClass(NullWritable.class);
FileOutputFormat.setOutputPath(newJob, new Path(outPath + "/sort"));
newJob.setOutputFormatClass(TextOutputFormat.class);
newJob.waitForCompletion(true);
作者:@小黑
多个Mapper和Reducer的Job的更多相关文章
- 关于Mapper、Reducer的个人总结(转)
Mapper的处理过程: 1.1. InputFormat 产生 InputSplit,并且调用RecordReader将这些逻辑单元(InputSplit)转化为map task的输入.其中Inpu ...
- Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类
前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...
- Mapper 与 Reducer 解析
1 . 旧版 API 的 Mapper/Reducer 解析 Mapper/Reducer 中封装了应用程序的数据处理逻辑.为了简化接口,MapReduce 要求所有存储在底层分布式文件系统上的数据均 ...
- MapReduce之Mapper类,Reducer类中的函数(转载)
Mapper类4个函数的解析 Mapper有setup(),map(),cleanup()和run()四个方法.其中setup()一般是用来进行一些map()前的准备工作,map()则一般承担主要的处 ...
- Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍
在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...
- 027_编写MapReduce的模板类Mapper、Reducer和Driver
模板类编写好后写MapReduce程序,的模板类编写好以后只需要改参数就行了,代码如下: package org.dragon.hadoop.mr.module; import java.io.IOE ...
- [hadoop入门]mapper与reducer(word_count计数demo)
1.mapper #!/usr/bin/env python import sys for line in sys.stdin: line = line.strip() words = line.sp ...
- hadoop2.7之Mapper/reducer源码分析
一切从示例程序开始: 示例程序 Hadoop2.7 提供的示例程序WordCount.java package org.apache.hadoop.examples; import java.io.I ...
- 【Hadoop】Combiner的本质是迷你的reducer,不能随意使用
问题提出: 众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出 ...
随机推荐
- openwrt 控制gpio口的方法
利用i2c来控制gpio口 1.编译驱动或者安装驱动 # opkg update # opkg install kmod-i2c-gpio-custom kmod-i2c-core# 加载驱动 # i ...
- spring源码分析 contextConfigLocation属性的位置
<context-param> <param-name>contextConfigLocation</param-name> <param-value> ...
- Bzoj1202/洛谷P2294 [HNOI2005]狡猾的商人(带权并查集/差分约束系统)
题面 Bzoj 洛谷 题解 考虑带权并查集,设\(f[i]\)表示\(i\)的父亲(\(\forall f[i]<i\)),\(sum[i]\)表示\(\sum\limits_{j=fa[i]} ...
- RxSwift 系列(八)
前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...
- 【bzoj1875】【JZYZOJ1354】[SDOI2009]HH去散步 矩阵快速幂 点边转换
http://172.20.6.3/Problem_Show.asp?id=1354 http://www.lydsy.com/JudgeOnline/problem.php?id=1875 题意: ...
- bzoj 1026
很久以前做过的一道数位DP,现在用一种新的解决数位DP的比较一般的方法. 数位DP裸题是:求[L,R]有多少个数. 先转化成求[0,R]有多少个数,然后区间相减即可. 对于[0,R]中的所有数,用0补 ...
- 10.十进制转m进制
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 将十进制数n转换成m进制数 m<=16 n<=1 ...
- hihocoder1322 树结构判定(161周)
hihocoder1322 : 树结构判定(161周) 题目链接 思路: 无向图中判断是不是一棵树. 并查集判断.判断是不是只有一个连通分量.并且该联通分量中没有环.没有环的判定很简单就是看边的数目和 ...
- csv文件导入到mysql
如何将csv文件导入到mysql数据库呢,方法有很多但最简单粗暴的方法还是用sql语句啦,像下面这样. LOAD DATA LOCAL INFILE 'csv文件路径' INTO TABLE 数据表名 ...
- HDU 4496 D-City (并查集,水题)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...