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

实现MapReduce多文件自定义输出

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

接着,你实现刚刚重写MultipleOutputFormat类中的generateFileNameForKeyValue方法自定义返回需要输出文件的名称,我这里是以key值中以逗号分割取第一个字段的值作为输出文件名,这样第一个字段值相同的会输出到一个文件中并以其值作为文件名。

  1. public static class VVLogNameMultipleTextOutputFormat extends MultipleOutputFormat<Text, NullWritable> {  
  2.          
  3.        @Override  
  4.        protected String generateFileNameForKeyValue(Text key, NullWritable value, Configuration conf) {   
  5.            String sp[] = key.toString().split(",");  
  6.            String filename = sp[1];  
  7.            try {  
  8.                Long.parseLong(sp[1]);  
  9.            } catch (NumberFormatException e) {  
  10.                filename = "000000000000";  
  11.            }  
  12.            return filename;  
  13.        }  
  14.   
  15.   
  16.    }  

最后就是在job调用时设置了

Configuration conf = getConf();
Job job = new Job(conf);
job.setNumReduceTasks(12);
......
job.setMapperClass(VVEtlMapper.class);
job.setReducerClass(EtlReducer.class);
job.setOutputFormatClass(VVLogNameMultipleTextOutputFormat.class);//设置自定义的多文件输出类
FileInputFormat.setInputPaths(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
FileOutputFormat.setCompressOutput(job, true);//设置输出结果采用压缩
FileOutputFormat.setOutputCompressorClass(job, LzopCodec.class); //设置输出结果采用lzo压缩

ok,这样你就完成了支持新的Hadoop api自定义的多文件输出mapreduce编写。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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