博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MapReduce高级编程——自定义InputFormat——深入理解
阅读量:2395 次
发布时间:2019-05-10

本文共 3160 字,大约阅读时间需要 10 分钟。

0、本文承接上文 

1、环境配置,本文的开发环境请直接参考

2、Mapper,Reducer参数解释

 

import java.io.IOException;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;public class Point3DMapper extends Mapper
{ protected void map(Text key, Point3D value, Context context) throws IOException, InterruptedException{ context.write(key, value); }}

 

   进入Mapper源代码可以看到

 

public class Mapper
{ public class Context extends MapContext
{ public Context(Configuration conf, TaskAttemptID taskid, RecordReader
reader, RecordWriter
writer, OutputCommitter committer, StatusReporter reporter, InputSplit split) throws IOException, InterruptedException { super(conf, taskid, reader, writer, committer, reporter, split); } } /** * Called once at the beginning of the task. */ protected void setup(Context context ) throws IOException, InterruptedException { // NOTHING } /** * Called once for each key/value pair in the input split. Most applications * should override this, but the default is the identity function. */ @SuppressWarnings("unchecked") protected void map(KEYIN key, VALUEIN value, Context context) throws IOException, InterruptedException { context.write((KEYOUT) key, (VALUEOUT) value); }
 

 

即,/**

* Text      -> KEYIN* Point3D  -> VALUEIN* Text       -> KEYOUT* Point3D  -> VALUEOUT**/Mapper

 

而,// Text -> ball etc. -> KEYOUT

// value -> .5, 12.7, 9.0 etc. -> VALUEOUTcontext.write(key, value);

 

同理,我们查看Reducer源代码,可以看到,Mapper的KEYOUT类型和VALUEOUT类型,必须对应Reducer的KEYIN类型和VLUEIN类型。

 

public class Reducer
{ public class Context extends ReduceContext
{ public Context(Configuration conf, TaskAttemptID taskid, RawKeyValueIterator input, Counter inputKeyCounter, Counter inputValueCounter, RecordWriter
output, OutputCommitter committer, StatusReporter reporter, RawComparator
comparator, Class
keyClass, Class
valueClass ) throws IOException, InterruptedException { super(conf, taskid, input, inputKeyCounter, inputValueCounter, output, committer, reporter, comparator, keyClass, valueClass); } } /** * Called once at the start of the task. */ protected void setup(Context context ) throws IOException, InterruptedException { // NOTHING } /** * This method is called once for each key. Most applications will define * their reduce class by overriding this method. The default implementation * is an identity function. */ @SuppressWarnings("unchecked") protected void reduce(KEYIN key, Iterable
values, Context context ) throws IOException, InterruptedException { for(VALUEIN value: values) { context.write((KEYOUT) key, (VALUEOUT) value); } }

 

 

本文的目的是因为有些时候文档不是很清楚,而最好的方法是看源代码。“源码之前,了无秘密”。

 

对于MapReduce的细致执行流程,我推荐看例子经典,博客作者一流的。相信看完会有所收获!

 

 

转载地址:http://jvwob.baihongyu.com/

你可能感兴趣的文章
glibc-printf
查看>>
Web--JavaWeb应用中文乱码问题原理及解决方法
查看>>
Servlet--HttpServletRequest获取请求信息(请求头、请求行、参数)详解
查看>>
Web--request解决表单参数的中文乱码问题(GET方式和POST方式)
查看>>
Web--Request对象传递数据、转发与重定向的区别
查看>>
UML--类图详解
查看>>
Servlet--关于RequestDispatcher(forward、include)的原理
查看>>
Servlet--Cookie原理及API使用详解
查看>>
Servlet--Session原理及API的使用
查看>>
Servlet--三个作用域(Request、Session、ServletContext)总结
查看>>
Listener--监听器的分类、功能及API详解
查看>>
Listener--ServletContextListener接口的使用详解
查看>>
Listener--HttpSessionListener、ServletRequestListener接口的使用详解
查看>>
Listener--域对象属性变化监听器([ServletRequest | HttpSession | ServletContext] AttributeListener)API详解
查看>>
Listener--HttpSessionBindingListenerAPI及使用(在线人数统计)详解
查看>>
Listener--HttpSessionActivationListener(钝化、活化)API、配置和使用详解
查看>>
Filter--过滤器Filter概述、API、配置与使用详解
查看>>
Web--使用Filter和Cookie实现自动登录
查看>>
Web--Filter使用装饰器模式解决全站中文乱码问题
查看>>
JS--JavaScript入门(script标签使用与外部JavaScript文件引入)
查看>>