设为首页 收藏本站
查看: 836|回复: 0

[经验分享] Hadoop深入学习:OutputFormat组件

[复制链接]

尚未签到

发表于 2016-12-8 07:03:30 | 显示全部楼层 |阅读模式
在本节中,我们着重来学习一下MapReduce编程框架中的OutputFormat组件。
         在MapReduce中,OutputFormat主要用于描述经reducer处理后输出的数据的格式,它会将用户提供的key/value键值对而写入到特定格式的文件中。
         我们先看一下在1.0老版本中的OutputFormat接口的代码:
public interface OutputFormat<K, V> {
/**
* 返回一个RercordWriter类的对象,并有该对象接收用户的key/value数据对儿,然后
* 按指定的格式将数据写入文件
*
* @param ignored
* @param job configuration for the job whose output is being written.
* @param name the unique name for this part of the output.
* @param progress mechanism for reporting progress while writing to file.
* @return a {@link RecordWriter} to write the output for the job.
* @throws IOException
*/
RecordWriter<K, V> getRecordWriter(FileSystem ignored, JobConf job,
String name, Progressable progress)throws IOException;
/**
* 该方法一般用在用户作业被提交到JobTracker之前,由JobClient自动调用该接口来检查
*  数据目录是否合法
*  
* <p>This is to validate the output specification for the job when it is
* a job is submitted.  Typically checks that it does not already exist,
* throwing an exception when it already exists, so that output is not
* overwritten.</p>
*
* @param ignored
* @param job job configuration.
* @throws IOException when output should not be attempted
*/
void checkOutputSpecs(FileSystem ignored, JobConf job) throws IOException;
}


         Hadoop自带了很多的OutputFormat的实现,它们与InputFormat实现县对应。这里我们就着重学习所有基于文件的OutputFormat的实现基类FileOutputFormat类。
         FileOutputFormat有两个最主要的功能:
         1)、实现checkOutputSpecs()方法,该方法是在作业运行之前被调用的,默认会检查用户的输出目录是否存在,如果存在则爆出异常,防止该输出目录中已有数据被覆盖。
         2)、处理side-effect file,该文件并不是任务的最在中输出文件,而是具有特殊用途的专属文件,其典型应用是执行推测式任务。
              在Hadoop中,有硬件或软件再或数据的原因,一个作业的某些子任务可能会慢于其他同类型的子任务,这些“慢子任务”明显会拖慢整个作业的执行进度,因此hadoop对这些慢任务实行推测式执行,即在另外一个节点上启动一个和慢子任务相同的任务——该任务就是推测式任务,最先完成的任务的计算结果后就被认为是这个数据块最终的处理结果,然后系统会杀死还没执行完的慢子任务。
              为了防止两个相同的子任务同时往一个输出文件中写入数据而发生冲突,FileOutputFormat会为每一个Reduce Task的输出数据创建一个side-effect file文件,并将产生的数据临时写入该文件,然后带Reduce Task完成后,在将之移动到最终的输出目录。默认情况下,当MapReduce作业完成后,会在最终的结果目录${mapred.out.dir}下生成一个空的_SUCCESS文件,该文件主要作为一个作业完成的标志。如果使用Oozie,那么Oozie就是通过最终结果目录下的_SUCCESS文件的存在来判断作业是否执行完成的。
          上述过程都是由一个叫OutputCommiter的接口实现的,Hadoop提供了默认的该接口的实现类FileOutputCommiter,当然开发者也可以根据需求来编写自己的OutputCommiter实现类,并通过${mapred.output.commiter.class}指定,下面是接口中的方法及FileOutputCommiter中的实现:     方法            何时使用            FileOutputCommiter中的实现            setupJob      作业初始化           创建临时目录${mapred.out.dir}_temorary         commitJob     作业成功完成         删除临时目录,并在${mapred.out.dir}下创建空_SUCCESS文件         abortJob      作业运行失败         删除临时目录         setupTask     任务初始化           不进行任何操作         needsTaskCommit判断是否要提交结果  只要存在side-effect file,就返回true         commitTask    任务运行完成         提交结果,将side-effect file移到${mapred.out.dir}目录下         abortTask     任务允许失败         删除任务的side-effect file
         对于1.0的新版本,除了将接口变成抽象类外,还添加了一个getOutputCommitter()方法:代码如下:
public abstract class OutputFormat<K, V> {
/**
* 返回一个RercordWriter类的对象,并有该对象接收用户的key/value数据对儿,然后
* 按指定的格式将数据写入文件
*
* @param context the information about the current task.
* @return a {@link RecordWriter} to write the output for the job.
* @throws IOException
*/
public abstract RecordWriter<K, V>
getRecordWriter(TaskAttemptContext context
) throws IOException, InterruptedException;
/**
* 作业提交前由JobClient调用来检查输出目录是否存在,存在就报异常放在数据覆盖
*  
* <p>This is to validate the output specification for the job when it is
* a job is submitted.  Typically checks that it does not already exist,
* throwing an exception when it already exists, so that output is not
* overwritten.</p>
*
* @param context information about the job
* @throws IOException when output should not be attempted
*/
public abstract void checkOutputSpecs(JobContext context
) throws IOException,
InterruptedException;
/**
* Get the output committer for this output format. This is responsible
* for ensuring the output is committed correctly.
* @param context the task context
* @return an output committer
* @throws IOException
* @throws InterruptedException
*/
public abstract OutputCommitter getOutputCommitter(TaskAttemptContext context
) throws IOException, InterruptedException;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-311063-1-1.html 上篇帖子: hadoop学习顺序【转】 下篇帖子: awk and hadoop之mapper
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表