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

实现MapReduce多文件自定义输出

[日期:2012-07-27] 来源:Linux社区  作者:liuzhoulong [字体:大 中 小]

接着你还需要自定义一个LineRecordWriter实现记录写入器RecordWriter类,自定义输出格式。

  1. import java.io.DataOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. import org.apache.Hadoop.io.NullWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.RecordWriter;  
  8. import org.apache.hadoop.mapreduce.TaskAttemptContext;  
  9.   
  10. /** 
  11.  *  
  12.  * 重新构造实现记录写入器RecordWriter类 
  13.  * Created on 2012-07-08 
  14.  * @author zhoulongliu 
  15.  * @param <K> 
  16.  * @param <V> 
  17.  */  
  18. public class LineRecordWriter<K, V> extends RecordWriter<K, V> {  
  19.   
  20.     private static final String utf8 = "UTF-8";//定义字符编码格式   
  21.     private static final byte[] newline;  
  22.     static {  
  23.         try {  
  24.             newline = "\n".getBytes(utf8);//定义换行符   
  25.         } catch (UnsupportedEncodingException uee) {  
  26.             throw new IllegalArgumentException("can't find " + utf8 + " encoding");  
  27.         }  
  28.     }  
  29.     protected DataOutputStream out;  
  30.     private final byte[] keyValueSeparator;  
  31.   
  32.      //实现构造方法,出入输出流对象和分隔符   
  33.     public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {  
  34.         this.out = out;  
  35.         try {  
  36.             this.keyValueSeparator = keyValueSeparator.getBytes(utf8);  
  37.         } catch (UnsupportedEncodingException uee) {  
  38.             throw new IllegalArgumentException("can't find " + utf8 + " encoding");  
  39.         }  
  40.     }  
  41.   
  42.     public LineRecordWriter(DataOutputStream out) {  
  43.         this(out, "\t");  
  44.     }  
  45.   
  46.     private void writeObject(Object o) throws IOException {  
  47.         if (o instanceof Text) {  
  48.             Text to = (Text) o;  
  49.             out.write(to.getBytes(), 0, to.getLength());  
  50.         } else {  
  51.             out.write(o.toString().getBytes(utf8));  
  52.         }  
  53.     }  
  54.      
  55.     /** 
  56.      * 将mapreduce的key,value以自定义格式写入到输出流中 
  57.      */  
  58.     public synchronized void write(K key, V value) throws IOException {  
  59.         boolean nullKey = key == null || key instanceof NullWritable;  
  60.         boolean nullValue = value == null || value instanceof NullWritable;  
  61.         if (nullKey && nullValue) {  
  62.             return;  
  63.         }  
  64.         if (!nullKey) {  
  65.             writeObject(key);  
  66.         }  
  67.         if (!(nullKey || nullValue)) {  
  68.             out.write(keyValueSeparator);  
  69.         }  
  70.         if (!nullValue) {  
  71.             writeObject(value);  
  72.         }  
  73.         out.write(newline);  
  74.     }  
  75.   
  76.     public synchronized void close(TaskAttemptContext context) throws IOException {  
  77.         out.close();  
  78.     }  
  79.   
  80. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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