MapReduce-多个Mapper
MapReduce的多输入、多mapper
虽然一个MapReduce作业的输入可能包含多个输入文件(由文件glob、过滤器和路径组成),但所有文件都由同一个InputFormat和同一个Mapper来解释。然而,数据格式往往会随时间而演变,所以必须写自己的mapper来处理应用中的遗留数据格式问题。或者,有些数据源会提供相同的数据,但是格式不同。
这些问题可以用MultipleInputs类来妥善处理,它允许为每条输入路径指定InputFormat和Mapper。
代码如下
package com.zhen.mapreduce.multipleInput; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author FengZhen
* @date 2018年8月25日
* 多输入、多mapper
*/
public class MultipleInputsTest extends Configured implements Tool{ /**
* 根据 ` 分隔字符串
* @author FengZhen
*
*/
static class SplitMapper1 extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String[] values = value.toString().split("`");
for (String string : values) {
context.write(new Text(string), new IntWritable(1));
}
}
} /**
* 根据 , 分隔字符串
* @author FengZhen
*
*/
static class SplitMapper2 extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String[] values = value.toString().split(",");
for (String string : values) {
context.write(new Text(string), new IntWritable(1));
}
}
} /**
* 同一个reduce
* @author FengZhen
*
*/
static class SplitReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> value,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable intWritable : value) {
sum += intWritable.get();
}
context.write(key, new IntWritable(sum));
}
} public int run(String[] args) throws Exception { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration);
job.setJobName("MultipleInputs");
job.setJarByClass(MultipleInputsTest.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setReducerClass(SplitReducer.class); //设置多输入、多mapper
MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, SplitMapper1.class);
MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, SplitMapper2.class); job.setOutputFormatClass(TextOutputFormat.class);
TextOutputFormat.setOutputPath(job, new Path(args[2])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) {
try {
String[] params = {"hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/test1","hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/test2", "hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/output"};
int exitCode = ToolRunner.run(new MultipleInputsTest(), params);
System.exit(exitCode);
} catch (Exception e) {
e.printStackTrace();
}
} }
MapReduce-多个Mapper的更多相关文章
- [Hadoop源码解读](二)MapReduce篇之Mapper类
前面在讲InputFormat的时候,讲到了Mapper类是如何利用RecordReader来读取InputSplit中的K-V对的. 这一篇里,开始对Mapper.class的子类进行解读. 先回忆 ...
- mapreduce中控制mapper的数量
很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数决定.在默认情况下,最终input占据了多少block,就应该启动多少个Mapper.如果输入的 ...
- mapreduce 只使用Mapper往多个hbase表中写数据
只使用Mapper不使用reduce会大大减少mapreduce程序的运行时间. 有时候程序会往多张hbase表写数据. 所以有如题的需求. 下面给出的代码,不是可以运行的代码,只是展示driver中 ...
- MapReduce之Mapper类,Reducer类中的函数(转载)
Mapper类4个函数的解析 Mapper有setup(),map(),cleanup()和run()四个方法.其中setup()一般是用来进行一些map()前的准备工作,map()则一般承担主要的处 ...
- Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类
前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...
- [Hadoop in Action] 第5章 高阶MapReduce
链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter 1.链接MapReduce作业 [顺序链接MapReduce作业] mapreduce-1 | mapr ...
- Kettle实现MapReduce之WordCount
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 欢迎转载 抽空用kettle配置了一个Mapreduce的Word count,发现还是很方便快捷的,废话不多说 ...
- MapReduce基础知识
hadoop版本:1.1.2 一.Mapper类的结构 Mapper类是Job.setInputFormatClass()方法的默认值,Mapper类将输入的键值对原封不动地输出. org.apach ...
- mapreduce job提交流程源码级分析(一)(原创)
首先,在自己写的MR程序中通过org.apache.hadoop.mapreduce.Job来创建Job.配置好之后通过waitForCompletion方法来提交Job并打印MR执行过程的log.H ...
- Hadoop实战2:MapReduce编程-WordCount实例-streaming-python环境
这是搭建hadoop环境后的第一个MapReduce程序: 基于hadoop streaming的python的脚本: 1 map.py文件,把文本的内容划分成单词: #!/usr/bin/pytho ...
随机推荐
- selenium的元素定位-鼠标事件
鼠标事件 ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_cl ...
- Permutation Descent Counts(递推)
1968: Permutation Descent Counts Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb ...
- sublime Text的一些用法(emmet插件、)
在学的过程中,看到了一个非常方便的html的标签的写法:,插件emmet 一.安装emmet 看清楚哦~~这是Sublime text 3不是2的版本,两者的安装还是有区别的1 ONE:建议到官方下载 ...
- 查找杀死指定进程delphi
//需要引用tlhelp32单元//查找进程function findProcessId(pname:string):Cardinal; var hsnapshot:THandle; lpe:TPro ...
- JSP页面中的tab页
首先下载bootstrap-tab.js和bootstrap.min.css <script type="text/javascript">$('#myTab a'). ...
- centos添加定时任务
安装crontab: yum install crontabs 查看crontab服务状态:service crond status 手动启动crontab服务:service crond start ...
- Longest Common Prefix -最长公共前缀
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
- 【云图】怎样制作全国KTV查询系统?
摘要:本文以[唱吧]531麦霸音乐节为案例,具体解读了怎样导入自有数据到高德云图,并进行检索和展示.最后,调起高德mobile地图来进行路线规划和周边查询. 本案例能够应用在微信开发平台,支付宝公众服 ...
- 字符串之strcmp
功能:比较两个字符串的ascII码大小 输入:两个字符串 返回值:相等为0,大于为大于零,小于为小于零 #include <iostream> #include <assert.h& ...
- 标准c数学函数使用方法
cppreference.com -> 标准c数学函数 -> 详解 标准c数学函数 abs 语法: #include <stdlib.h> int abs( int ...