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的更多相关文章

  1. [Hadoop源码解读](二)MapReduce篇之Mapper类

    前面在讲InputFormat的时候,讲到了Mapper类是如何利用RecordReader来读取InputSplit中的K-V对的. 这一篇里,开始对Mapper.class的子类进行解读. 先回忆 ...

  2. mapreduce中控制mapper的数量

    很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数决定.在默认情况下,最终input占据了多少block,就应该启动多少个Mapper.如果输入的 ...

  3. mapreduce 只使用Mapper往多个hbase表中写数据

    只使用Mapper不使用reduce会大大减少mapreduce程序的运行时间. 有时候程序会往多张hbase表写数据. 所以有如题的需求. 下面给出的代码,不是可以运行的代码,只是展示driver中 ...

  4. MapReduce之Mapper类,Reducer类中的函数(转载)

    Mapper类4个函数的解析 Mapper有setup(),map(),cleanup()和run()四个方法.其中setup()一般是用来进行一些map()前的准备工作,map()则一般承担主要的处 ...

  5. Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类

    前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...

  6. [Hadoop in Action] 第5章 高阶MapReduce

    链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter   1.链接MapReduce作业   [顺序链接MapReduce作业]   mapreduce-1 | mapr ...

  7. Kettle实现MapReduce之WordCount

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 欢迎转载 抽空用kettle配置了一个Mapreduce的Word count,发现还是很方便快捷的,废话不多说 ...

  8. MapReduce基础知识

    hadoop版本:1.1.2 一.Mapper类的结构 Mapper类是Job.setInputFormatClass()方法的默认值,Mapper类将输入的键值对原封不动地输出. org.apach ...

  9. mapreduce job提交流程源码级分析(一)(原创)

    首先,在自己写的MR程序中通过org.apache.hadoop.mapreduce.Job来创建Job.配置好之后通过waitForCompletion方法来提交Job并打印MR执行过程的log.H ...

  10. Hadoop实战2:MapReduce编程-WordCount实例-streaming-python环境

    这是搭建hadoop环境后的第一个MapReduce程序: 基于hadoop streaming的python的脚本: 1 map.py文件,把文本的内容划分成单词: #!/usr/bin/pytho ...

随机推荐

  1. mybatis 一次执行多条SQL MySql+Mybatis+Druid之SqlException:sql injection violation, multi-statement not allow

    如果用JDBC jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8 ...

  2. restful demo 演示; jquery min1.1;

    [说明]上午建立了一个restful风格的一个测试,运行通过:下午试了试postman,想看看http请求的具体过程,但是chrome浏览器的network面板也可以查看,并且很方便,就索性用它了 一 ...

  3. Splitting Pile

    Splitting Pile Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke a ...

  4. WCF基础之传输

    WCF中使用的主要传输的方式有HTTP,TCP和命名管道. 绑定包括可选的协议绑定元素(如安全),必需的编码绑定元素和必须的传输协定绑定元素三个部分,而由传输方式则是由传输绑定元素来决定的. HTTP ...

  5. Oracle 提供的start with 关键字用法

    在相关业务查询中,我们常常遇到相关的上下级关系情况,如下图中行政区划关联,此时就要用到Oracle 提供的start with 关键字来帮助我们进行递归查询 基本语法 SELECT ... FROM ...

  6. python函数回顾:getattr()

    描述 getattr() 函数用于返回一个对象属性值. 语法 getattr 语法: getattr(object, name[, default]) 参数 object -- 对象. name -- ...

  7. 0x01 MySQL What's DataBase

    0x01 数据库管理软件的由来 在此之前,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅能存在于某一台机器上. 如果暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件都运行在 ...

  8. 我的Android进阶之旅------>Android中编解码学习笔记

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  9. hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...

  10. SQL语句中的Having子句与where子句

    一.介绍 聚合函数如SUM, COUNT, MAX, AVG等.这些函数和其它函数的根本区别就是它们一般作用在多条记录上.而通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于 ...