hadoop版本:1.1.2

一、Mapper类的结构

Mapper类是Job.setInputFormatClass()方法的默认值,Mapper类将输入的键值对原封不动地输出。

org.apache.hadoop.mapreduce.Mapper类的结构如下:

  1. public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {
  2.  
  3. public class Context
  4. extends MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {
  5. public Context(Configuration conf, TaskAttemptID taskid,
  6. RecordReader<KEYIN,VALUEIN> reader,
  7. RecordWriter<KEYOUT,VALUEOUT> writer,
  8. OutputCommitter committer,
  9. StatusReporter reporter,
  10. InputSplit split) throws IOException, InterruptedException {
  11. super(conf, taskid, reader, writer, committer, reporter, split);
  12. }
  13. }
  14.  
  15. /**
  16. * Called once at the beginning of the task.
  17. * 在task开始之前调用一次
  18. *
  19. */
  20. protected void setup(Context context
  21. ) throws IOException, InterruptedException {
  22. // NOTHING
  23. }
  24.  
  25. /**
  26. * Called once for each key/value pair in the input split. Most applications
  27. * should override this, but the default is the identity function.
  28. * 对数据分块中的每个键值对都调用一次
  29. *
  30. */
  31. @SuppressWarnings("unchecked")
  32. protected void map(KEYIN key, VALUEIN value,
  33. Context context) throws IOException, InterruptedException {
  34. context.write((KEYOUT) key, (VALUEOUT) value);
  35. }
  36.  
  37. /**
  38. * Called once at the end of the task.
  39. * 在task结束后调用一次
  40. *
  41. */
  42. protected void cleanup(Context context
  43. ) throws IOException, InterruptedException {
  44. // NOTHING
  45. }
  46.  
  47. /**
  48. * Expert users can override this method for more complete control over the
  49. * execution of the Mapper.
  50. * 默认先调用一次setup方法,然后循环对每个键值对调用map方法,最后调用一次cleanup方法。
  51. *
  52. * @param context
  53. * @throws IOException
  54. */
  55. public void run(Context context) throws IOException, InterruptedException {
  56. setup(context);
  57. while (context.nextKeyValue()) {
  58. map(context.getCurrentKey(), context.getCurrentValue(), context);
  59. }
  60. cleanup(context);
  61. }
  62. }

二、Reducer类的结构

Reducer类是Job.setOutputFormatClass()方法的默认值,Reducer类将输入的键值对原封不动地输出。

org.apache.hadoop.mapreduce.Reduce与Mapper类似。

  1. public class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {
  2.  
  3. public class Context
  4. extends ReduceContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {
  5. public Context(Configuration conf, TaskAttemptID taskid,
  6. RawKeyValueIterator input,
  7. Counter inputKeyCounter,
  8. Counter inputValueCounter,
  9. RecordWriter<KEYOUT,VALUEOUT> output,
  10. OutputCommitter committer,
  11. StatusReporter reporter,
  12. RawComparator<KEYIN> comparator,
  13. Class<KEYIN> keyClass,
  14. Class<VALUEIN> valueClass
  15. ) throws IOException, InterruptedException {
  16. super(conf, taskid, input, inputKeyCounter, inputValueCounter,
  17. output, committer, reporter,
  18. comparator, keyClass, valueClass);
  19. }
  20. }
  21.  
  22. /**
  23. * Called once at the start of the task.
  24. */
  25. protected void setup(Context context
  26. ) throws IOException, InterruptedException {
  27. // NOTHING
  28. }
  29.  
  30. /**
  31. * This method is called once for each key. Most applications will define
  32. * their reduce class by overriding this method. The default implementation
  33. * is an identity function.
  34. */
  35. @SuppressWarnings("unchecked")
  36. protected void reduce(KEYIN key, Iterable<VALUEIN> values, Context context
  37. ) throws IOException, InterruptedException {
  38. for(VALUEIN value: values) {
  39. context.write((KEYOUT) key, (VALUEOUT) value);
  40. }
  41. }
  42.  
  43. /**
  44. * Called once at the end of the task.
  45. */
  46. protected void cleanup(Context context
  47. ) throws IOException, InterruptedException {
  48. // NOTHING
  49. }
  50.  
  51. /**
  52. * Advanced application writers can use the
  53. * {@link #run(org.apache.hadoop.mapreduce.Reducer.Context)} method to
  54. * control how the reduce task works.
  55. */
  56. public void run(Context context) throws IOException, InterruptedException {
  57. setup(context);
  58. while (context.nextKey()) {
  59. reduce(context.getCurrentKey(), context.getValues(), context);
  60. }
  61. cleanup(context);
  62. }
  63. }

三、hadoop提供的mapper和reducer实现

我们不一定总是要从头开始自己编写自己的Mapper和Reducer类。Hadoop提供了几种常见的Mapper和Reducer的子类,这些类可以直接用于我们的作业当中。

mapper可以在org.apache.hadoop.mapreduce.lib.map包下面找到如下子类:

  • InverseMapper:A Mapper hat swaps keys and values.
  • MultithreadedMapper:Multithreaded implementation for org.apache.hadoop.mapreduce.Mapper.
  • TokenCounterMapper:Tokenize the input values and emit each word with a count of 1.

reducer可以在org.apache.hadoop.mapreduce.lib.reduce包下面找到如下子类:

  • IntSumReducer:它输出每个键对应的整数值列表的总和。
  • LongSumReducer:它输出每个键对应的长整数值列表的总和。

四、MapReduce的输入

1、InputFormat抽象类
 

该类的作用是将输入的数据分割成一个个的split,并将split进一步拆分成键值对作为map函数的输入。

该类位于org.apache.hadoop.mapreduce包下。

InputFormat describes the input-specification for a Map-Reduce job.

The Map-Reduce framework relies on the InputFormat of the job to:

  1. Validate the input-specification of the job.
  2. Split-up the input file(s) into logical InputSplits, each of which is then assigned to an individual Mapper.
  3. Provide the RecordReader implementation to be used to glean input records from the logical InputSplit for processing by the Mapper.

The default behavior of file-based InputFormats, typically sub-classes of FileInputFormat, is to split the input into logical InputSplits based on the total size, in bytes, of the input files. However, the FileSystem blocksize of the input files is treated as an upper bound for input splits. A lower bound on the split size can be set via mapred.min.split.size.

Clearly, logical splits based on input-size is insufficient for many applications since record boundaries are to respected. In such cases, the application has to also implement a RecordReader on whom lies the responsibility to respect record-boundaries and present a record-oriented view of the logical InputSplit to the individual task.

2、RecordReader抽象类

该类位于org.apache.hadoop.mapreduce包下。

The record reader breaks the data into key/value pairs for input to the Mapper.

3、hadoop提供的InputFormat

hadoop在org.apache.hadoop.mapreduce.lib.input包下提供了一些InputFormat的实现。hadoop默认使用TextInputFormat类处理输入。

4、hadoop提供的RecordReader

hadoop在org.apache.hadoop.mapreduce.lib.input包下也提供了一些RecordReader的实现。

五、MapReduce的输出

1、OutputFormat抽象类
 
该类位于org.apache.hadoop.mapreduce包下。
OutputFormat describes the output-specification for a Map-Reduce job.

The Map-Reduce framework relies on the OutputFormat of the job to:

  • Validate the output-specification of the job. For e.g. check that the output directory doesn't already exist.
  • Provide the RecordWriter implementation to be used to write out the output files of the job. Output files are stored in a FileSystem.

2、RecordWriter抽象类

该类位于org.apache.hadoop.mapreduce包下。

RecordWriter writes the output <key, value> pairs to an output file.

RecordWriter implementations write the job outputs to the FileSystem.

3、hadoop提供的OutputFormat

hadoop在org.apache.hadoop.mapreduce.lib.output包下提供了一些OutputFormat的实现。hadoop默认使用TextOutputFormat类处理输出。

4、hadoop提供的RecordWriter

在org.apache.hadoop.mapreduce.lib.input包下的OutputFormat的实现类(子类)将它们所需的RecordWriter定义为内部类,因此不存在单独实现的RecordWriter类。

六、MapReduce各阶段涉及到的类

P70-71

1、InputFormat类

2、Mapper类

3、Combiner类

4、Partitioner类

5、Reducer类

6、OutputFormat类

7、其他

七、详解Shuffle过程:http://langyu.iteye.com/blog/992916

map->shuffle->reduce

P60-64,例子P64-68

附:WEB接口的端口号配置:

mapred-default.xml

  1. <property>
  2. <name>mapred.job.tracker.http.address</name>
  3. <value>0.0.0.0:50030</value>
  4. <description>
  5. The job tracker http server address and port the server will listen on.
  6. If the port is 0 then the server will start on a free port.
  7. </description>
  8. </property>

hdfs-default.xml

  1. <property>
  2. <name>dfs.http.address</name>
  3. <value>0.0.0.0:50070</value>
  4. <description>
  5. The address and the base port where the dfs namenode web ui will listen on.
  6. If the port is 0 then the server will start on a free port.
  7. </description>
  8. </property>

MapReduce基础知识的更多相关文章

  1. 小记---------Hadoop的MapReduce基础知识

    MapReduce是一种分布式计算模型,主要用于搜索领域,解决海量数据的计算问题 MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce()两个函数,即可实现分布式计算. 两 ...

  2. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  3. MongoDB基础知识 02

    MongoDB基础知识 02 6 数据类型 6.1 null : 表示空值或者不存在的字段 {"x":null} 6.2 布尔型 : 布尔类型只有两个值true和false {&q ...

  4. 大数据基础知识问答----spark篇,大数据生态圈

    Spark相关知识点 1.Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduce的通用的并行计算框架 dfsSpark基于mapredu ...

  5. 最全的spark基础知识解答

    原文:http://www.36dsj.com/archives/61155 一. Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduc ...

  6. JAVA基础知识|lambda与stream

    lambda与stream是java8中比较重要两个新特性,lambda表达式采用一种简洁的语法定义代码块,允许我们将行为传递到函数中.之前我们想将行为传递到函数中,仅有的选择是使用匿名内部类,现在我 ...

  7. 常见问题:MongoDB基础知识

    常见问题:MongoDB基础知识 ·MongoDB支持哪些平台? ·MongoDB作为托管服务提供吗? ·集合(collection)与表(table)有何不同? ·如何创建数据库(database) ...

  8. Hive 这些基础知识,你忘记了吗?

    Hive 其实是一个客户端,类似于navcat.plsql 这种,不同的是Hive 是读取 HDFS 上的数据,作为离线查询使用,离线就意味着速度很慢,有可能跑一个任务需要几个小时甚至更长时间都有可能 ...

  9. [源码解析] 深度学习分布式训练框架 Horovod (1) --- 基础知识

    [源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 目录 [源码解析] 深度学习分布式训练框架 Horovod --- (1) 基础知识 0x00 摘要 0x01 分布式并 ...

随机推荐

  1. (原创)mybatis学习二,spring和mybatis的融合

    mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...

  2. CodeSmith 使用说明

    〇.            前言 最近两天自己写了个简单的ORM框架,非常的Easy,但是没有相应的代码生成工具,于是就很杯具了! 于是乎,花费了一天的时间学习并写了一个CodeSmith可以使用的模 ...

  3. linux的一些常用命令

    这几天正好在研究linux系统,打算将下一个项目部署在linux系统的服务器上已提高安全性(被window 2003已经折磨的不行了),经过各方了解和深思熟虑后决定使用linux系统的CentOs版本 ...

  4. Java 生成 UUID

    1.UUID 简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Found ...

  5. sql基本命令

    --------------------------------------------------------SQL基本命令开始----------------------------------- ...

  6. SQL Server Management Studio无法记住密码

    用sa账户登录sql server 2008,勾选了“记住密码”,但重新登录时,SQL Server Management Studio无法记住密码.   后来发现,在重新登录时,登录名显示的并非是s ...

  7. web安全——代理(nginx)

    场景 过滤非正常用户使用的http请求. 限制正常用户使用的范围(下载速度.访问频率等). 通过架构规划来提升安全. 能自动解决http请求问题. 解决方案 代理自身的安全 千万不要使用root启动! ...

  8. 深入理解计算机系统(2.8)---浮点数的舍入,Java中的舍入例子以及浮点数运算(重要)

    前言 上一章我们简单介绍了IEEE浮点标准,本次我们主要讲解一下浮点运算舍入的问题,以及简单的介绍浮点数的运算. 之前我们已经提到过,有很多小数是二进制浮点数无法准确表示的,因此就难免会遇到舍入的问题 ...

  9. python基础-range用法_python2.x和3.x的区别

    #range帮助创建连续的数字,通过设置步长来指定不连续 python2.7 #直接就在内存中创建出来(0-99) >>> range(100)[0, 1, 2, 3, 4, 5, ...

  10. linux基础-第十六单元 yum管理RPM包

    第十六单元 yum管理RPM包 yum的功能 本地yum配置 光盘挂载和镜像挂载 本地yum配置 网络yum配置 网络yum配置 Yum命令的使用 使用yum安装软件 使用yum删除软件 安装组件 删 ...