Job的处理结果输出格式化器——OutputFormat

在前面的博文中,我已经详细的讲述了MapReduce是如何格式化一个作业的输入数据的,也就是用户应该如何来定义作业的输入数据的key-value类型,即如何读取输入数据,并把它们包装成一个个key-value对,最后来交给map函数来处理。那么对应的,本文将主要重点介绍如何格式化保存作业的最后输出结果,很明显,作业的最后输出结果实际上就是reduce函数的输出:key-value。因此,这个问题的关键就转换成了如何保存这些key-value对了,例如如何组织这些key-value,组织之后又把它们存储在什么地方,是HDFS还是HBase还是传统的关系型数据库?对于作业的最后输出结果的处理,Hadoop是完全交给了用户自己来定义或者设置——OutputFormat的实现。

     先来看看输出格式化器在Hadoop内部的设计与实现体系:

     从上面的类图可以看出输出格式化器(OutputFormat)主要包括两大组件:记录写入器(RecordWriter)和任务提交器(OutputCommitter),因此,OutputFormat的实现主要需要完成三个任务:

[java] view plaincopy
  1. public abstract class OutputFormat<K, V> {  
  2.   
  3.   /**  
  4.    * 创建一个记录写入器 
  5.    */  
  6.   public abstract RecordWriter<K, V> getRecordWriter(TaskAttemptContext context) throws IOException, InterruptedException;  
  7.   
  8.   /**  
  9.    * 检查结果输出的存储空间是否有效 
  10.    */  
  11.   public abstract void checkOutputSpecs(JobContext context) throws IOException, InterruptedException;  
  12.   
  13.   /** 
  14.    * 创建一个任务提交器 
  15.    */  
  16.   public abstract OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException;  
  17. }  

     接下来就来好好讲一讲与输出格式化器(OutputFormat)相关的两大组件:

1.记录写入器(RecordWriter<K,V>)

      记录写入器主要负责根据存储环境的需要先对对一个key-value对进行组织,然后把这个组织好的key-value存储到某个存储环境中,如:分布式文件系统、数据库等。例如它的一个实现LineRecordWriter,就是把一个key-value组织成一行文本,然后把这一行文本存储到设置好个某一个文件系统下的某一个文件中。当然在LineRecordWriter中,每一行中的key和value的值是通过分隔符来区别的,这个分隔符默认是'\t',但也可以通过配置文件来设置,对应的配置项为:mapred.textoutputformat.separator


2.任务提交器(OutputCommitter)

     这里的任务提交器主要是用来对任务(包括map任何和rduce任务)的输出进行管理,具体的工作如下:

[java] view plaincopy
  1. public abstract class OutputCommitter {  
  2.   /** 
  3.    * 启动作业 
  4.    */  
  5.   public abstract void setupJob(JobContext jobContext) throws IOException;  
  6.   
  7.   /** 
  8.    * 清理作业 
  9.    */  
  10.   public abstract void cleanupJob(JobContext jobContext) throws IOException;  
  11.   
  12.   /** 
  13.    * 启动任务 
  14.    */  
  15.   public abstract void setupTask(TaskAttemptContext taskContext)  
  16.   throws IOException;  
  17.     
  18.   /** 
  19.    * 检查是否能够提交任务 
  20.    */  
  21.   public abstract boolean needsTaskCommit(TaskAttemptContext taskContext)  
  22.   throws IOException;  
  23.   
  24.   /** 
  25.    *  提交任务 
  26.    */  
  27.   public abstract void commitTask(TaskAttemptContext taskContext)  
  28.   throws IOException;  
  29.     
  30.   /** 
  31.    * 放弃任务 
  32.    */  
  33.   public abstract void abortTask(TaskAttemptContext taskContext)  
  34.   throws IOException;  
  35. }  
    现在以它的一个具体实现——FileOutputCommitter来详细的介绍用户应该如何来根据自己的实际情况自定义一个任务提交器

[java] view plaincopy
  1. public FileOutputCommitter(Path outputPath, TaskAttemptContext context) throws IOException {  
  2.   if (outputPath != null) {  
  3.     this.outputPath = outputPath;//最终存放任务输出结果的目录  
  4.     outputFileSystem = outputPath.getFileSystem(context.getConfiguration());  
  5.     //任务运行时的工作目录(这个工作目录是临时目录的子目录)  
  6.     workPath = new Path(outputPath,(FileOutputCommitter.TEMP_DIR_NAME + Path.SEPARATOR + "_" + context.getTaskAttemptID().toString())).makeQualified(outputFileSystem);  
  7.   }  
  8. }  
  9.   
  10. /** 
  11.  * 启动作业:为作业的最终输出创建一个中间临时目录(这个临时目录是最终目录的子目录) 
  12.  */  
  13. public void setupJob(JobContext context) throws IOException {  
  14.   if (outputPath != null) {  
  15.     Path tmpDir = new Path(outputPath, FileOutputCommitter.TEMP_DIR_NAME);  
  16.     FileSystem fileSys = tmpDir.getFileSystem(context.getConfiguration());  
  17.       
  18.     if (!fileSys.mkdirs(tmpDir)) {  
  19.       LOG.error("Mkdirs failed to create " + tmpDir.toString());  
  20.     }  
  21.   }  
  22. }  
  23.   
  24. /** 
  25.  * 清理作业:清空存放作业最终输出的临时目录 
  26.  */  
  27. public void cleanupJob(JobContext context) throws IOException {  
  28.   if (outputPath != null) {  
  29.     Path tmpDir = new Path(outputPath, FileOutputCommitter.TEMP_DIR_NAME);  
  30.     FileSystem fileSys = tmpDir.getFileSystem(context.getConfiguration());  
  31.       
  32.     if (fileSys.exists(tmpDir)) {  
  33.       fileSys.delete(tmpDir, true);  
  34.     }  
  35.   }  
  36. }  
  37.   
  38. /** 
  39.  * 启动任务:无需做任何处理 
  40.  */  
  41. @Override  
  42. public void setupTask(TaskAttemptContext context) throws IOException {  
  43.   
  44. }  
  45.   
  46.  /** 
  47.  * 移动输出结果 
  48.  */  
  49. private void moveTaskOutputs(TaskAttemptContext context, FileSystem fs, Path jobOutputDir, Path taskOutput) throws IOException {  
  50.    
  51.   TaskAttemptID attemptId = context.getTaskAttemptID();  
  52.   context.progress();  
  53.     
  54.   if (fs.isFile(taskOutput)) {  
  55.     Path finalOutputPath = getFinalPath(jobOutputDir, taskOutput, workPath);  
  56.       
  57.     if (!fs.rename(taskOutput, finalOutputPath)) {  
  58.       
  59.       if (!fs.delete(finalOutputPath, true)) {  
  60.         throw new IOException("Failed to delete earlier output of task: " + attemptId);  
  61.       }  
  62.        
  63.       if (!fs.rename(taskOutput, finalOutputPath)) {  
  64.         throw new IOException("Failed to save output of task: " + attemptId);  
  65.       }  
  66.     }  
  67.       
  68.     LOG.debug("Moved " + taskOutput + " to " + finalOutputPath);  
  69.   } else if(fs.getFileStatus(taskOutput).isDir()) {  
  70.     FileStatus[] paths = fs.listStatus(taskOutput);  
  71.     Path finalOutputPath = getFinalPath(jobOutputDir, taskOutput, workPath);  
  72.     fs.mkdirs(finalOutputPath);  
  73.     if (paths != null) {  
  74.       for (FileStatus path : paths) {  
  75.         moveTaskOutputs(context, fs, jobOutputDir, path.getPath());  
  76.       }  
  77.     }  
  78.   }  
  79. }  
  80.   
  81. /** 
  82.  * 提交任务:1.把中间目录中的输出结果移动到最终目录;2.删除工作目录 
  83.  */  
  84. public void commitTask(TaskAttemptContext context) throws IOException {  
  85.    
  86.   TaskAttemptID attemptId = context.getTaskAttemptID();  
  87.     
  88.   LOG.debug("using [org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter] to commit Task["+attemptId+"]..");  
  89.     
  90.   if (workPath != null) {  
  91.     context.progress();  
  92.     if (outputFileSystem.exists(workPath)) {  
  93.       // Move the task outputs to their final place  
  94.       moveTaskOutputs(context, outputFileSystem, outputPath, workPath);  
  95.       // Delete the temporary task-specific output directory  
  96.       if (!outputFileSystem.delete(workPath, true)) {  
  97.         LOG.warn("Failed to delete the temporary output" + " directory of task: " + attemptId + " - " + workPath);  
  98.       }  
  99.         
  100.       LOG.debug("Saved output of task '" + attemptId + "' to " + outputPath);  
  101.     }  
  102.   }  
  103. }  
  104.   
  105. /** 
  106.  * 放弃任务:删除工作目录 
  107.  */  
  108. public void abortTask(TaskAttemptContext context) {  
  109.   try {  
  110.     if (workPath != null) {   
  111.       context.progress();  
  112.       outputFileSystem.delete(workPath, true);  
  113.     }  
  114.   } catch (IOException ie) {  
  115.     LOG.warn("Error discarding output" + StringUtils.stringifyException(ie));  
  116.   }  
  117. }  
  118.   
  119. /** 
  120.  *根据工作目录是否存在来判断任务是否可以提交 
  121.  */  
  122. public boolean needsTaskCommit(TaskAttemptContext context) throws IOException {  
  123.   return workPath != null && outputFileSystem.exists(workPath);  
  124. }  
     

     在上面的FileOutputCommitter实现中,任务处理过程中的输出结果存储在工作目录(在临时目录下)下,最后成功处理完之后才把结果移动到最终目录(临时目录是其子目录)。作业最终的输出目录可以通过配置文件或FileOutputFormat.setOutputPath()来设置,对应的配置项为:mapred.output.dir

      另外,用户在提交作业之前可以通过Job.setOuputFormatClass()或者配置文件来设置自己所需要的输出格式化器,配置项为:mapreduce.outputformat.class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值