Hadoop:mapreduce程序reduce输出控制

1,Hadoop中,reduce支持多个输出,输出的文件名也是可控的,就是继承MultipleTextOutputFormat类,重写generateFileNameForKey方法

[java]
  1. public class LzoHandleLogMr extends Configured implements Tool {  
  2.   
  3.      static class LzoHandleLogMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text> {  
  4.          
  5.         
  6.         public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter)  
  7.                 throws IOException {  
  8.             try {  
  9.                 String[] sp = value.toString().split(",");  
  10.                 output.collect(new Text(sp[0]), value);  
  11.             }catch (Exception e) {  
  12.                e.printStackTrace();  
  13.             }         
  14.         }  
  15.   
  16.   
  17.     }  
  18.     static class LzoHandleLogReducer  extends MapReduceBase implements Reducer<Text, Text, Text, NullWritable> {  
  19.           
  20.   
  21.   
  22.         @Override  
  23.         public void reduce(Text key, Iterator<Text> values,  
  24.                 OutputCollector<Text, NullWritable> output, Reporter reporter)  
  25.                 throws IOException {  
  26.             while (values.hasNext()) {  
  27.                   output.collect(values.next(), NullWritable.get());     
  28.                }  
  29.               
  30.         }     
  31.     }  
  32.       
  33.     public static class LogNameMultipleTextOutputFormat extends MultipleTextOutputFormat<Text, NullWritable>   
  34.        {  
  35.   
  36.   
  37.         @Override  
  38.         protected String generateFileNameForKeyValue(Text key,  
  39.                 NullWritable value, String name) {  
  40.             String sp[] = key.toString().split(",");  
  41.             String filename = sp[0];  
  42.             if(sp[0].contains(".")) filename="000000000000";  
  43.             return filename;  
  44.         }  
  45.           
  46.     }  
  47.       
  48.   
  49.   
  50.     @Override  
  51.     public int run(String[] args) throws Exception {  
  52.            
  53.             JobConf jobconf = new JobConf(LzoHandleLogMr.class);  
  54.             jobconf.setMapperClass(LzoHandleLogMapper.class);  
  55.             jobconf.setReducerClass(LzoHandleLogReducer.class);  
  56.             jobconf.setOutputFormat(LogNameMultipleTextOutputFormat.class);  
  57.             jobconf.setOutputKeyClass(Text.class);  
  58.             jobconf.setNumReduceTasks(12);  
  59.               
  60.               
  61.          FileInputFormat.setInputPaths(jobconf,new Path(args[0]));  
  62.             FileOutputFormat.setOutputPath(jobconf,new Path(args[1]));  
  63.             FileOutputFormat.setCompressOutput(jobconf, true);  
  64.             FileOutputFormat.setOutputCompressorClass(jobconf, LzopCodec.class);    
  65.               
  66.             JobClient.runJob(jobconf);  
  67.           return 0;  
  68.               
  69.     }  
  70. }  

在新版本的hadoopAPI是通过Job类来设置各种参数的,但是我调用 Job.setOutputFormatClass()来使用MultipleTextOutputFormat的时候,竟然报错,原因是必须继承子org.apache.hadoop.mapreduce.OutputFormat。0.20.2比较致命的其中一个bug, 升级到0.21能解决


2, 如果同一行数据,需要同时输出至多个文件的话,我们可以使用MultipleOutputs类:

  1. public class MultiFile extends Confi gured implements Tool {  
  2.     public static class MapClass extends MapReduceBase  
  3.         implements Mapper<LongWritable, Text, NullWritable, Text> {  
  4.             private MultipleOutputs mos;  
  5.   
  6.             private OutputCollector<NullWritable, Text> collector;  
  7.             public void confi gure(JobConf conf) {  
  8.                 mos = new MultipleOutputs(conf);  
  9.             }  
  10.   
  11.             public void map(LongWritable key, Text value,  
  12.                     OutputCollector<NullWritable, Text> output,  
  13.                     Reporter reporter) throws IOException {  
  14.                 String[] arr = value.toString().split(",", -1);  
  15.                 String chrono = arr[0] + "," + arr[1] + "," + arr[2];  
  16.                 String geo = arr[0] + "," + arr[4] + "," + arr[5];  
  17.                 collector = mos.getCollector("chrono", reporter);  
  18.                 collector.collect(NullWritable.get(), new Text(chrono));  
  19.                 collector = mos.getCollector("geo", reporter);  
  20.                 collector.collect(NullWritable.get(), new Text(geo));  
  21.             }  
  22.   
  23.             public void close() throws IOException {  
  24.                 mos.close();  
  25.             }  
  26.     }  
  27.   
  28.     public int run(String[] args) throws Exception {  
  29.         Confi guration conf = getConf();  
  30.         JobConf job = new JobConf(conf, MultiFile.class);  
  31.         Path in = new Path(args[0]);  
  32.         Path out = new Path(args[1]);  
  33.         FileInputFormat.setInputPaths(job, in);  
  34.         FileOutputFormat.setOutputPath(job, out);  
  35.         job.setJobName("MultiFile");  
  36.         job.setMapperClass(MapClass.class);  
  37.         job.setInputFormat(TextInputFormat.class);  
  38.         job.setOutputKeyClass(NullWritable.class);  
  39.         job.setOutputValueClass(Text.class);  
  40.         job.setNumReduceTasks(0);  
  41.         MultipleOutputs.addNamedOutput(job,  
  42.                 "chrono",  
  43.                 TextOutputFormat.class,  
  44.                 NullWritable.class,  
  45.                 Text.class);  
  46.         MultipleOutputs.addNamedOutput(job,  
  47.                 "geo",  
  48.                 TextOutputFormat.class,  
  49.                 NullWritable.class,  
  50.                 Text.class);  
  51.         JobClient.runJob(job);  
  52.         return 0;  
  53.     }  
  54. }  

这个类维护了一个<name, OutputCollector>的map。我们可以在job配置里添加collector,然后在reduce方法中,取得对应的collector并调用collector.write即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop编写MapReduce程序是指使用Hadoop框架来实现MapReduce算法。MapReduce是一种分布式计算模型,它将大规模数据集分成小的数据块,然后在分布式计算集群上并行处理这些数据块。MapReduce程序由两个部分组成:Map和Reduce。 Map阶段:Map阶段将输入数据分成小的数据块,然后对每个数据块进行处理,生成键值对。Map阶段的输出结果是一个键值对列表。 Reduce阶段:Reduce阶段将Map阶段输出的键值对列表进行合并,生成最终的输出结果。Reduce阶段的输出结果是一个键值对列表。 编写MapReduce程序的步骤如下: 1. 定义Map函数:Map函数将输入数据分成小的数据块,然后对每个数据块进行处理,生成键值对。 2. 定义Reduce函数:Reduce函数将Map函数输出的键值对列表进行合并,生成最终的输出结果。 3. 定义输入格式:定义输入数据的格式,例如文本文件、CSV文件等。 4. 定义输出格式:定义输出数据的格式,例如文本文件、CSV文件等。 5. 配置Hadoop环境:配置Hadoop环境,包括Hadoop的安装、配置、启动等。 6. 编写MapReduce程序:编写MapReduce程序,包括Map函数、Reduce函数、输入格式、输出格式等。 7. 运行MapReduce程序:将编写好的MapReduce程序提交到Hadoop集群上运行。 8. 查看输出结果:查看MapReduce程序输出结果,进行调试和优化。 以上就是Hadoop编写MapReduce程序的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值