自己定义一个outputFormat

自定义一个OutputFormat,用于输出<Text, MapWritable>格式的数据

MapWritable的内容是 [Text: LongWritable]

输出格式 [url    url2:times2,url3:times3,...]

 

参考TextOutputFormat,修改简化之

 

Java代码   收藏代码
  1. public class TextAndMapWritableOutputFormat extends  
  2.         FileOutputFormat<Text, MapWritable> {  
  3.   
  4.     @Override  
  5.     public RecordWriter<Text, MapWritable> getRecordWriter(FileSystem ignored,  
  6.             JobConf job, String name, Progressable progress) throws IOException {  
  7.         Path file = FileOutputFormat.getTaskOutputPath(job, name);  
  8.         FileSystem fs = file.getFileSystem(job);  
  9.         FSDataOutputStream fileOut = fs.create(file, progress);  
  10.   
  11.         return new TextAndMapWritableRecordWriter(fileOut);  
  12.     }  
  13.   
  14.     protected static class TextAndMapWritableRecordWriter implements RecordWriter<Text, MapWritable> {  
  15.   
  16.         private static final String utf8 = "UTF-8";  
  17.         private static final byte[] newline;  
  18.         private static final byte[] keyValueSeparator;  
  19.         private static final byte[] colon;  
  20.         private static final byte[] comma;  
  21.         static {  
  22.             try {  
  23.                 newline = "\n".getBytes(utf8);  
  24.                 keyValueSeparator = "\t".getBytes(utf8);  
  25.                 colon = ":".getBytes(utf8);  
  26.                 comma = ",".getBytes(utf8);  
  27.             } catch (UnsupportedEncodingException uee) {  
  28.                 throw new IllegalArgumentException("can't find " + utf8  
  29.                         + " encoding");  
  30.             }  
  31.         }  
  32.   
  33.         protected DataOutputStream out;  
  34.   
  35.         public TextAndMapWritableRecordWriter(DataOutputStream out) {  
  36.             this.out = out;  
  37.         }  
  38.   
  39.         @Override  
  40.         public synchronized void write(Text key, MapWritable value)  
  41.                 throws IOException {  
  42.             out.write(key.getBytes(), 0, key.getLength());  
  43.             out.write(keyValueSeparator);  
  44.   
  45.             Iterator<Writable> it = value.keySet().iterator();  
  46.             while (it.hasNext()) {  
  47.                 Writable k = it.next();  
  48.                 LongWritable v = (LongWritable) value.get(k);  
  49.   
  50.                 out.write(((Text) k).getBytes());  
  51.                 out.write(colon);  
  52.                 out.write(v.toString().getBytes(utf8));  
  53.                 out.write(comma);  
  54.             }  
  55.   
  56.             out.write(newline);  
  57.         }  
  58.   
  59.         @Override  
  60.         public synchronized void close(Reporter reporter) throws IOException {  
  61.             out.close();  
  62.         }  
  63.   
  64.     }  
  65.   

转自:http://roserouge.iteye.com/blog/347857
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值