前言

  前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量。

一、作业的默认配置

  MapReduce程序的默认配置  

1)概述

  在我们的MapReduce程序中有一些默认的配置。所以说当我们程序如果要使用这些默认配置时,可以不用写。

  

  我们的一个MapReduce程序一定会有Mapper和Reducer,但是我们程序中不写的话,它也有默认的Mapper和Reducer。

  当我们使用默认的Mapper和Reducer的时候,map和reducer的输入和输出都是偏移量和数据文件的一行数据,所以就是相当于原样输出!

2)默认的MapReduce程序

  1. /**
  2. * 没有指定Mapper和Reducer的最小作业配置
  3. */
  4. public class MinimalMapReduce {
  5. public static void main(String[] args) throws Exception{
  6. // 构建新的作业
  7. Configuration conf=new Configuration();
  8. Job job = Job.getInstance(conf, "MinimalMapReduce");
  9. job.setJarByClass(MinimalMapReduce.class);
  10. // 设置输入输出路径
  11. FileInputFormat.addInputPath(job, new Path(args[]));
  12. FileOutputFormat.setOutputPath(job, new Path(args[]));
  13. // ᨀ交作业运行
  14. System.exit(job.waitForCompletion(true)?:);
  15.   }
  16. }

  输入是:

    

  输出是:

    

二、作业的配置方式

  MapReduce的类型配置

  1)用于配置类型的属性

    

    

    在命令行中,怎么去配置呢?

      比如说mapreduce.job.inputformat.class。首先我们要继承Configured实现Tool工具才能这样去指定:

      -Dmapreduce.job.inputformat.class = 某一个类的类全名(一定要记得加报名)

    这是Map端的输出类型控制

    这是整个MapReduce程序输出类型控制,其实就是reduce的类型格式控制

  2)No Reducer的MapReduce程序--Mapper

    第一步:写一个TokenCounterMapper继承Mapper

  1. /**
  2. * 将输入的文本内容拆分为word,做一个简单输出的Mapper
  3. */
  4. public class TokenCounterMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
  5. private Text word=new Text();
  6. private static final IntWritable one=new IntWritable();
  7. @Override
  8. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
  9. throws IOException, InterruptedException {
  10. // TODO Auto-generated method stub
  11. StringTokenizer itr=new StringTokenizer(value.toString());
  12. while(itr.hasMoreTokens()){
  13. word.set(itr.nextToken());
  14. context.write(word, one);
  15. }
  16. }
  17. }

TokenCounterMapper

    第二步:写一个NoReducerMRDriver完成作业配置

  1. /**
  2. *没有设置Reducer的MR程序
  3. */
  4. public class NoReducerMRDriver {
  5. public static void main(String[] args) throws Exception {
  6. // 构建新的作业
  7. Configuration conf=new Configuration();
  8. Job job = Job.getInstance(conf, "NoReducer");
  9. job.setJarByClass(NoReducerMRDriver.class);
  10. // 设置Mapper
  11. job.setMapperClass(TokenCounterMapper.class);
  12. // 设置reducer的数量为0
  13. job.setNumReduceTasks();
  14. // 设置输出格式
  15. job.setMapOutputKeyClass(Text.class);
  16. job.setMapOutputValueClass(IntWritable.class);
  17. // 设置输入输出路径
  18. FileInputFormat.setInputPaths(job, new Path(args[]));
  19. FileOutputFormat.setOutputPath(job, new Path(args[]));
  20. // ᨀ交运行作业
  21. System.exit(job.waitForCompletion(true)?:);
  22. }
  23. }

NoReducerMRDriver

    输入:

      

    结果:

      

    注意:如果作业拥有0个Reducer,则Mapper结果直接写入OutputFormat而不经key值排序。

  3)No Mapper的MapReduce程序--Reducer

    第一步:写一个TokenCounterReducer继承Reducer

  1. /**
  2. * 将reduce输入的values内容拆分为word,做一个简单输出的Reducer
  3. */
  4. public class TokenCounterReducer extends Reducer<LongWritable, Text, Text, IntWritable>{
  5. private Text word=new Text();
  6. private static final IntWritable one=new IntWritable();
  7. @Override
  8. protected void reduce(LongWritable key, Iterable<Text> values,Reducer<LongWritable, Text, Text, IntWritable>.Context context)
  9. throws IOException, InterruptedException {
  10. // TODO Auto-generated method stub
  11. for(Text value:values){
  12. StringTokenizer itr=new StringTokenizer(value.toString());
  13. while(itr.hasMoreTokens()){
  14. word.set(itr.nextToken());
  15. context.write(word, one);
  16. }
  17. }
  18. }
  19. }

TokenCounterReducer

    第二步:写一个NoMapperMRDrive完成作业配置

  1. /**
  2. *没有设置Mapper的MR程序
  3. */
  4. public class NoMapperMRDriver {
  5. public static void main(String[] args) throws Exception {
  6. // 构建新的作业
  7. Configuration conf=new Configuration();
  8. Job job = Job.getInstance(conf, "NoMapper");
  9. job.setJarByClass(NoMapperMRDriver.class);
  10. // 设置Reducer
  11. job.setReducerClass(TokenCounterReducer.class);
  12. // 设置输出格式
  13. job.setMapOutputKeyClass(LongWritable.class);
  14. job.setMapOutputValueClass(Text.class);
  15. job.setOutputKeyClass(Text.class);
  16. job.setOutputValueClass(IntWritable.class);
  17. // 设置输入输出路径
  18. FileInputFormat.setInputPaths(job, new Path(args[]));
  19. FileOutputFormat.setOutputPath(job, new Path(args[]));
  20. // ᨀ交运行作业
  21. System.exit(job.waitForCompletion(true)?:);
  22. }
  23. }

NoMapperMRDrive

    输入:

      

    输出:

      

三、Mapper类和Reducer类以及它们的子类(实现类)

3.1、Mapper概述

  Mapper:封装了应用程序Mapper阶段的数据处理逻辑

   

  1)ChainMapper

    方便用户编写链式Map任务, 即Map阶段包含多个Mapper,即可以别写多个自定义map去参与运算。
  2)InverseMapper

    一个能交换key和value的Mapper
  3)RegexMapper

    检查输入是否匹配某正则表达式, 输出匹配字符串和计数器(用的很少)
  4)TockenCounterMapper

    将输入分解为独立的单词, 输出个单词和计数器(以空格分割单词,value值为1)

3.2、Reducer概述

  Mapper:封装了应用程序Mapper阶段的数据处理逻辑

  

  1)ChainMapper:

    方便用户编写链式Map任务, 即Map阶段只能有一个Reducer,后面还可以用ChainMapper去多加Mapper。

  2)IntSumReducer/LongSumReducer

    对各key的所有整型值求和

3.2、写一个实例去使用

  注意:这里用到了一个输入格式为KeyValueTextInputFormat,我们查看源码注释:

    

    我们需要用mapreduce.input.keyvaluelinerecordreader.key.value.separator去指定key和value的分隔符是什么,它的默认分隔符是"\t"也就是tab键。

    这个需要在配置文件中去指定,但是我们知道在配置文件中能设置的在程序中也是可以设置的。

  1.     conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator",",");

  代码实现: 

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.conf.Configured;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
  10. import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
  11. import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
  13. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  14. import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer;
  15. import org.apache.hadoop.util.Tool;
  16. import org.apache.hadoop.util.ToolRunner;
  17.  
  18. public class PatentReference_0010 extends Configured implements Tool{
  19.  
  20. static class PatentReferenceMapper extends Mapper<Text,Text,Text,IntWritable>{
  21. private IntWritable one=new IntWritable();
  22. @Override
  23. protected void map(Text key,Text value,Context context) throws IOException, InterruptedException{
  24. context.write(key,one);
  25. }
  26. }
  27.  
  28. @Override
  29. public int run(String[] args) throws Exception{
  30. Configuration conf=getConf();
  31. Path input=new Path(conf.get("input"));
  32. Path output=new Path(conf.get("output"));
  33. conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator",",");
  34.  
  35. Job job=Job.getInstance(conf,this.getClass().getSimpleName());
  36. job.setJarByClass(this.getClass());
  37.  
  38. ChainMapper.addMapper(job,InverseMapper.class,
  39. // 输入的键值类型由InputFormat决定
  40. Text.class,Text.class,
  41. // 输出的键值类型与输入的键值类型相反
  42. Text.class,Text.class,conf);
  43.  
  44. ChainMapper.addMapper(job,PatentReferenceMapper.class,
  45. // 输入的键值类型由前一个Mapper输出的键值类型决定
  46. Text.class,Text.class,
  47. Text.class,IntWritable.class,conf);
  48.  
  49. ChainReducer.setReducer(job,IntSumReducer.class,
  50. Text.class,IntWritable.class,
  51. Text.class,IntWritable.class,conf);
  52.  
  53. ChainReducer.addMapper(job,InverseMapper.class,
  54. Text.class,IntWritable.class,
  55. IntWritable.class,Text.class,conf);
  56.  
  57. job.setInputFormatClass(KeyValueTextInputFormat.class);
  58. job.setOutputFormatClass(TextOutputFormat.class);
  59.  
  60. KeyValueTextInputFormat.addInputPath(job,input);
  61. TextOutputFormat.setOutputPath(job,output);
  62.  
  63. return job.waitForCompletion(true)?:;
  64. }
  65.  
  66. public static void main(String[] args) throws Exception{
  67. System.exit(ToolRunner.run(new P00010_PatentReference_0010(),args));
  68. }
  69. }

  在Job job=Job.getInstance(conf,this.getClass().getSimpleName());设置中,job把conf也就是配置文件做了一个拷贝,因为hadoop要重复利用一个对象,如果是引用的话,发现值得改变就都改变了。        

  

    

    

喜欢就点个“推荐”哦!

Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类的更多相关文章

  1. hadoop学习(七)----mapReduce原理以及操作过程

    前面我们使用HDFS进行了相关的操作,也了解了HDFS的原理和机制,有了分布式文件系统我们如何去处理文件呢,这就的提到hadoop的第二个组成部分-MapReduce. MapReduce充分借鉴了分 ...

  2. 关于Mapper、Reducer的个人总结(转)

    Mapper的处理过程: 1.1. InputFormat 产生 InputSplit,并且调用RecordReader将这些逻辑单元(InputSplit)转化为map task的输入.其中Inpu ...

  3. 使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试

    0.preliminary 环境搭建 Setup development environment Download the latest version of MRUnit jar from Apac ...

  4. 分布式配置 tachyon 并执行Hadoop样例 MapReduce

    ----------此文章.笔者按着tachyon官网教程进行安装并记录. (本地安装tachyon具体解释:http://blog.csdn.net/u012587561/article/detai ...

  5. 使用IDEA远程向伪分布式搭建的Hadoop提交MapReduce作业

    环境 VirtualBox 6.1 IntelliJ IDEA 2020.1.1 Ubuntu-18.04.4-live-server-amd64 jdk-8u251-linux-x64 hadoop ...

  6. 高可用,完全分布式Hadoop集群HDFS和MapReduce安装配置指南

    原文:http://my.oschina.net/wstone/blog/365010#OSC_h3_13 (WJW)高可用,完全分布式Hadoop集群HDFS和MapReduce安装配置指南 [X] ...

  7. Hadoop学习之路(二十七)MapReduce的API使用(四)

    第一题 下面是三种商品的销售数据 要求:根据以上数据,用 MapReduce 统计出如下数据: 1.每种商品的销售总金额,并降序排序 2.每种商品销售额最多的三周 第二题:MapReduce 题 现有 ...

  8. Hadoop官方文档翻译——MapReduce Tutorial

    MapReduce Tutorial(个人指导) Purpose(目的) Prerequisites(必备条件) Overview(综述) Inputs and Outputs(输入输出) MapRe ...

  9. 剖析MapReduce 作业运行机制

    包含四个独立的实体: ·  Client Node 客户端:编写 MapReduce代码,配置作业,提交MapReduce作业. ·  JobTracker :初始化作业,分配作业,与 TaskTra ...

随机推荐

  1. SVN不出现绿色对勾的情况

    就目前而言,我出现了两种情况. Num1:电脑云盘可能不兼容,导致无法出现svn提示小icon:----->删除云盘重新启动. Num2:被设置覆盖.----->鼠标右键-->Tor ...

  2. Python的自学之路:Python基础(一)

    声明:我写博客不是为了什么,只是为了记录自己的学习状态,学过的知识点!方便以后进行好的复习!python小白,勿喷 python环境的搭建,在这里就不细说了,这里有我的链接,可以参考一下:https: ...

  3. Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】

    什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...

  4. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  5. 如何保存或读取数据(到android的data目录)利用context获取常见目录可优化代码

    读取用户信息 当然这里可以有多种返回值 非硬性

  6. IE无法获得cookie,ie不支持cookie的解决办法,火狐支持

    发现用自己的电脑 IE7.0总是无法正常登录,别的电脑都可以. 每次登录后又被重定向回了登录页面. 可换成Firefox和google chrome 却一切OK,后来还把浏览器升级到IE8.0 问题依 ...

  7. Sql Server——查询(一)

    查询数据就是对数据库中的数据进行筛选显示.显示数据的表格只是一个"虚拟表". 查询 (1)对列的筛选: 1.查询表中所有数据: select * from 表名           ...

  8. UIScollview 添加UICollectionView 实现放大缩小

    创建一个空的工程 打开storyboard,添加UIScollview 设置代理 实现代理方法 - (UIView *)viewForZoomingInScrollView:(UIScrollView ...

  9. 【运维】CPU负载

    最近对我的本本(4核8线程)用top命令看系统状况出现了CPU利用率超过200%的情况,非常诧异,查了下相关资料,把这个问题弄清楚了.首先来分析下CPU Load load average: 0.09 ...

  10. Linux入门之常用命令(8)上传下载

    [什么是rz/sz (lsz/lrz)]  简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/s ...