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 ...
随机推荐
- activity通过流程实例id动态获取流程图并展示在jsp页面上
提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...
- poj1742
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 33998 Accepted: 11554 Descripti ...
- pycharm如何自定义模板?
按照上图箭头方向设置即可.
- zookeeper 命令行使用
先用简易的客户端链接上 默认链接的就是本机上面的zkserver 我一上来help 下.没办法 ,用的少,先喊一声救命 ,看看有没有人救我! [zk: localhost:2181(CONNECTED ...
- 从jarray中删除指定元素的问题
string jsonText = "[{\"a\": \"aaa\",\"b\": \"bbb\",\&qu ...
- 比较运算符compareTo()、equals()、==之间的区别
前言 首先再次强调hashcode (==)和equals的真正含义(我记得以前有人会说,equals是判断对象内容,hashcode是判断是否相等之类): equals:是否同一个对象实例.注意,是 ...
- spring和hibernate整合时设置自动生成数据库的表
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFa ...
- mysql用户授权以及权限收回
语法 GRANT privileges [(columns)] ON DATABASE.TABLE TO 'username'@'hostname' [IDENTIFIED BY [PASSWORD] ...
- 设计一个算法,採用BFS方式输出图G中从顶点u到v的最短路径(不带权的无向连通图G採用邻接表存储)
思想:图G是不带权的无向连通图.一条边的长度计为1,因此,求带顶点u和顶点v的最短的路径即求顶点u和顶点v的边数最少的顶点序列.利用广度优先遍历算法,从u出发进行广度遍历,类似于从顶点u出发一层一层地 ...
- (4.6)sql server索引缺失提示
SQLSERVER如何查看索引缺失 sql server索引缺失提示 当大家发现数据库查询性能很慢的时候,大家都会想到加索引来优化数据库查询性能, 但是面对一个复杂的SQL语句,找到一个优化的索引组合 ...