类似于Linux管道重定向机制,前一个Map的输出直接作为下一个Map的输入,形成一个流水线。设想这样一个场景:在Map阶段,数据经过mapper01和mapper02处理;在Reduce阶段,数据经过sort和shuffle后,交给对应的reducer处理。reducer处理后并没有直接写入到Hdfs, 而是交给了另一个mapper03处理,它产生的最终结果写到hdfs输出目录中。

注意:对任意MR作业,Map和Reduce阶段可以有无限个Mapper,但reduer只能有一个。

package chain;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Chain { /**
* 手机 5000 * 需求:
* 电脑 2000 * 在第一个Mapper1里面过滤大于10000的数据
* 衣服 300   * 第二个Mapper2里面过滤掉大于100-10000的数据
* 鞋子 1200 * Reduce里面进行分类汇总并输出
* 裙子 434 * Reduce后的Mapper3里过滤掉商品名长度大于3的数据
* 手套 12 *
* 图书 12510 *
* 小商品 5   * 结果:
* 小商品 3 * 手套 12
* 订餐 2 * 订餐 2
*/ public static void main(String[] args) throws Exception {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(Chain.class); /**
* 配置mapper1
* 注意此处带参数的构造函数:new Configuration(false)
*/
Configuration map1Conf = new Configuration(false);
ChainMapper.addMapper(job, //主作业
Mapper1.class, //待加入的map class
LongWritable.class, //待加入map class的输入key类型
Text.class, //待加入map class的输入value类型
Text.class, //待加入map class的输出key类型
VLongWritable.class, //待加入map class的输出value类型
map1Conf); //待加入map class的配置信息 //配置mapper2
ChainMapper.addMapper(job, Mapper2.class, Text.class, VLongWritable.class, Text.class, VLongWritable.class, new Configuration(false)); /**
* 配置Reducer
* 注意此处使用的是setReducer()方法
*/
ChainReducer.setReducer(job, Reducer_Only.class, Text.class, VLongWritable.class, Text.class, VLongWritable.class, new Configuration(false)); //配置mapper3
ChainReducer.addMapper(job, Mapper3.class, Text.class, VLongWritable.class, Text.class, VLongWritable.class, new Configuration(false)); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
} //Mapper1
public static class Mapper1 extends Mapper<LongWritable, Text, Text, VLongWritable>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { /**
* Hadoop中默认的输入格式 TextOutputFormat 只支持UTF-8格式
* 所以解决GBK中文输出乱码问题的方法是:
* 1. 先将输入的Text类型的value转换为字节数组
* 2. 然后使用String的构造器String(byte[] bytes, int offset, int length, Charset charset)
* 3. 通过使用指定的charset解码指定的byte子数组,构造一个新的String
*/
String line=new String(value.getBytes(),0,value.getLength(),"GBK");
String[] splited = line.split(" "); //过滤大于10000的数据
if(Integer.parseInt(splited[1])<10000L){
context.write(new Text(splited[0]), new VLongWritable(Long.parseLong(splited[1])));
}
}
} //Mapper2
public static class Mapper2 extends Mapper<Text, VLongWritable, Text, VLongWritable>{
@Override
protected void map(Text key, VLongWritable value, Context context)
throws IOException, InterruptedException { //过滤100-10000间的数据
if(value.get()<100L){
context.write(key, value);
}
}
} //Reducer
public static class Reducer_Only extends Reducer<Text, VLongWritable, Text, VLongWritable>{
@Override
protected void reduce(Text key, Iterable<VLongWritable> v2s, Context context)
throws IOException, InterruptedException { long sumLong=0L; for(VLongWritable vLongWritable : v2s){
sumLong += vLongWritable.get(); context.write(key, new VLongWritable(sumLong));
}
}
} //Mapper3
public static class Mapper3 extends Mapper<Text, VLongWritable, Text, VLongWritable>{
@Override
protected void map(Text key, VLongWritable value, Context context)
throws IOException, InterruptedException { String line=new String(key.getBytes(),0,key.getLength(),"GBK"); //过滤商品名称长度大于3
if(line.length()<3){
context.write(key, value);
}
}
}
}

MR案例:链式ChainMapper的更多相关文章

  1. 组合式+迭代式+链式 MapReduce

    1.迭代式mapreduce 一些复杂的任务难以用一次mapreduce处理完成,需要多次mapreduce才能完成任务,例如Pagrank,Kmeans算法都需要多次的迭代,关于mapreduce迭 ...

  2. Hadoop的ChainMapper和ChainReducer使用案例(链式处理)(四)

    不多说,直接上干货!      Hadoop的MR作业支持链式处理,类似在一个生产牛奶的流水线上,每一个阶段都有特定的任务要处理,比如提供牛奶盒,装入牛奶,封盒,打印出厂日期,等等,通过这样进一步的分 ...

  3. javascript链式运动框架案例

    javascript链式运动框架 任务描述: 当鼠标移入红色矩形时,该矩形宽度逐渐增加至400px,之后高度逐渐增加至400px; 当鼠标移出红色矩形时,该矩形高度逐渐减小至200px,之后宽度逐渐减 ...

  4. jQuery编程基础精华01(jQuery简介,顶级对象$,jQuery对象、Dom对象,链式编程,选择器)

    jQuery简介 什么是jQuery? jQuery就是一个JavaScript函数库,没什么特别的.(开源)联想SQLHelper类 jQuery能做什么?jQuery是做什么的? jQuery本身 ...

  5. jQuery链式编程

    链式编程 多行代码合并成一行代码,前提要认清此行代码返回的是不是对象.是对象才能进行链式编程 .html(‘val’).text(‘val’).css()链式编程,隐式迭代 链式编程注意:$(‘div ...

  6. 从零开始学 Web 之 jQuery(七)事件冒泡,事件参数对象,链式编程原理

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  7. Hadoop基础-Map端链式编程之MapReduce统计TopN示例

    Hadoop基础-Map端链式编程之MapReduce统计TopN示例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.项目需求 对“temp.txt”中的数据进行分析,统计出各 ...

  8. 模仿Masonry链式编程思想

    使用masonry 也将近一年多了,它的链式编程方式一直是很吸引我的. 之前一直没空好好思考它是如何实现,直到现在正好自己有空,因此写下链式编程的基本思路. 链式基本的编程形式如 a.property ...

  9. 链式mapreduce

    在hadoop 中一个Job中可以按顺序运行多个mapper对数据进行前期的处理,再进行reduce,经reduce后的结果可经个经多个按顺序执行的mapper进行后期的处理,这样的Job是不会保存中 ...

随机推荐

  1. Node REPL

    Node 自带了交互式解释器,可以执行以下任务: 读取 - 读取用户输入,解析输入了Javascript 数据结构并存储在内存中. 执行 - 执行输入的数据结构 打印 - 输出结果 循环 - 循环操作 ...

  2. JSTL-标准标签库

    JSTL1.2中的标签库可以分成5类区域      核心:uri="http://java.sun.com/jsp/jstl/core"  prefix=“c”      XML: ...

  3. 在iOS模拟器上安装程式的ios-sim

    针对iOS装置进行开发时,绝大部分开发者采用的工具都是官方的Xcode.问题是负责图像设计和开发管理人员,却不一定熟悉Xcode的操作,这时ios-sim便是一个解决方案. 曾经从事iOS开发的朋友, ...

  4. Net 常用资源

    opensource: http://www.dotnetfoundation.org/projects https://github.com/dotnet/corefx Enterprise Lib ...

  5. 完全用nosql轻松打造千万级数据量的微博系统

    其实微博是一个结构相对简单,但数据量却是很庞大的一种产品.标题所说的是千万级数据量也并不是一千万条微博信息而已,而是千万级订阅关系之间发布.在看 我这篇文章之前,大多数人都看过sina的杨卫华大牛的微 ...

  6. 十個必用的 Vim Plugin

    ◎ The NERD Tree 操作 Vim 時,通常都在 Terminal 底下作用,無法像一般的 GUI    應用程式可以以樹狀目錄來瀏覽檔案. The NERD Tree    是一將檔案目錄 ...

  7. python爬虫系列(2)—— requests和BeautifulSoup

    本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...

  8. mysql监控优化(三)慢查询

    顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slowquery,通过设--log-slow-queries[=file_name]来打开该功能并设置记录位置和文件名.慢查询 ...

  9. usb-tooltip 重写.tooltip { word-break: break-all; }解决单词内换行

    <div style="padding: 5px 10px; font-size: 16px; text-align: left" class="truncate& ...

  10. vultr服务器上搭建PHP运行环境-centos7

    安装nginx: yum install nginx 开启Nginx systemctl start nginx 访问你的域名或者ip 例如:quantaedu.com 安装PHP 查看系统版本lsb ...