你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop0.20+ custom MultipleOutputFormat

[日期:2012-05-21] 来源:Linux社区  作者:chenwq [字体:大 中 小]

MultipleOutputFormat:

  1. package cn.xmu.dm;   
  2.   
  3. import java.io.DataOutputStream;   
  4. import java.io.IOException;   
  5. import java.util.HashMap;   
  6. import java.util.Iterator;   
  7. import org.apache.Hadoop.conf.Configuration;   
  8. import org.apache.hadoop.fs.FSDataOutputStream;   
  9. import org.apache.hadoop.fs.Path;   
  10. import org.apache.hadoop.io.Writable;   
  11. import org.apache.hadoop.io.WritableComparable;   
  12. import org.apache.hadoop.io.compress.CompressionCodec;   
  13. import org.apache.hadoop.io.compress.GzipCodec;   
  14. import org.apache.hadoop.mapreduce.OutputCommitter;   
  15. import org.apache.hadoop.mapreduce.RecordWriter;   
  16. import org.apache.hadoop.mapreduce.TaskAttemptContext;   
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter;   
  18. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;   
  19. import org.apache.hadoop.util.ReflectionUtils;   
  20. public abstract class MultipleOutputFormat<K extends WritableComparable<?>, V extends Writable>   
  21.         extends FileOutputFormat<K, V> {   
  22.     private MultiRecordWriter writer = null;   
  23.     public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job) throws IOException,   
  24.             InterruptedException {   
  25.         if (writer == null) {   
  26.             writer = new MultiRecordWriter(job, getTaskOutputPath(job));   
  27.         }   
  28.         return writer;   
  29.     }   
  30.     private Path getTaskOutputPath(TaskAttemptContext conf) throws IOException {   
  31.         Path workPath = null;   
  32.         OutputCommitter committer = super.getOutputCommitter(conf);   
  33.         if (committer instanceof FileOutputCommitter) {   
  34.             workPath = ((FileOutputCommitter) committer).getWorkPath();   
  35.         } else {   
  36.             Path outputPath = super.getOutputPath(conf);   
  37.             if (outputPath == null) {   
  38.                 throw new IOException("Undefined job output-path");   
  39.             }   
  40.             workPath = outputPath;   
  41.         }   
  42.         return workPath;   
  43.     }   
  44.        
  45.     protected abstract String generateFileNameForKeyValue(K key, V value, Configuration conf);   
  46.     public class MultiRecordWriter extends RecordWriter<K, V> {   
  47.            
  48.         private HashMap<String, RecordWriter<K, V>> recordWriters = null;   
  49.         private TaskAttemptContext job = null;   
  50.            
  51.         private Path workPath = null;   
  52.         public MultiRecordWriter(TaskAttemptContext job, Path workPath) {   
  53.             super();   
  54.             this.job = job;   
  55.             this.workPath = workPath;   
  56.             recordWriters = new HashMap<String, RecordWriter<K, V>>();   
  57.         }   
  58.         @Override  
  59.         public void close(TaskAttemptContext context) throws IOException, InterruptedException {   
  60.             Iterator<RecordWriter<K, V>> values = this.recordWriters.values().iterator();   
  61.             while (values.hasNext()) {   
  62.                 values.next().close(context);   
  63.             }   
  64.             this.recordWriters.clear();   
  65.         }   
  66.         @Override  
  67.         public void write(K key, V value) throws IOException, InterruptedException {   
  68.            
  69.             String baseName = generateFileNameForKeyValue(key, value, job.getConfiguration());   
  70.             RecordWriter<K, V> rw = this.recordWriters.get(baseName);   
  71.             if (rw == null) {   
  72.                 rw = getBaseRecordWriter(job, baseName);   
  73.                 this.recordWriters.put(baseName, rw);   
  74.             }   
  75.             rw.write(key, value);   
  76.         }   
  77.        
  78.         private RecordWriter<K, V> getBaseRecordWriter(TaskAttemptContext job, String baseName)   
  79.                 throws IOException, InterruptedException {   
  80.             Configuration conf = job.getConfiguration();   
  81.             boolean isCompressed = getCompressOutput(job);   
  82.             String keyValueSeparator = ",";   
  83.             RecordWriter<K, V> recordWriter = null;   
  84.             if (isCompressed) {   
  85.                 Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job,   
  86.                         GzipCodec.class);   
  87.                 CompressionCodec codec = ReflectionUtils.newInstance(codecClass, conf);   
  88.                 Path file = new Path(workPath, baseName + codec.getDefaultExtension());   
  89.                 FSDataOutputStream fileOut = file.getFileSystem(conf).create(file, false);   
  90.                 recordWriter = new LineRecordWriter<K, V>(new DataOutputStream(codec   
  91.                         .createOutputStream(fileOut)), keyValueSeparator);   
  92.             } else {   
  93.                 Path file = new Path(workPath, baseName);   
  94.                 FSDataOutputStream fileOut = file.getFileSystem(conf).create(file, false);   
  95.                 recordWriter = new LineRecordWriter<K, V>(fileOut, keyValueSeparator);   
  96.             }   
  97.             return recordWriter;   
  98.         }   
  99.     }   
  100. }  
linux
相关资讯       Hadoop应用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款