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

Hadoop Combiner 操作

[日期:2012-10-01] 来源:Linux社区  作者:fansy1990 [字体:大 中 小]

近期看了一本书:Data-intensive Text Processing with MapReduce,是讲如何设计MR程序的,看到一个例子是Combiner的设计模式,然后就动手实现了下。具体问题如下:

现有输入数据如下:

  1. one 3.9 
  2. one 4.0 
  3. one 3.8 
  4. two 44 
  5. two 44 
  6. two 44 
  7. three   9898 
  8. four    2323 
  9. four    2323 
  10. five    2323 
  11. six 23 
  12. six 2323 
  13. four    232 
  14. five    2323 

第一列代表用户,第二列代表用户在一个网站上所停留的时间,现在想求每个用户在这个网站的平均停留时间。如果不用combine操作的话,那么其MR伪代码如下(复制书上的内容):

  1. class Mapper 
  2. method Map(string t, integer r) 
  3. Emit(string t, integer r) 
  4. class Reducer 
  5. method Reduce(string t, integers [r1 , r2 , . . .]) 
  6. sum ← 0 
  7. cnt ← 0 
  8. for all integer r ∈ integers [r1 , r2 , . . .] do 
  9. sum ← sum + r 
  10. cnt ← cnt + 1 
  11. ravg ← sum/cnt 
  12. Emit(string t, integer ravg ) 

如果要加combine怎么操作呢?Combiner和Reducer一样么(求最大气温的例子或许是一样的,但这里却不是,而且现实中的很多例子都不是一样的),如果一样的话那么就会变成下面的错误操作了:

Mean(1, 2, 3, 4, 5) = Mean(Mean(1, 2), Mean(3, 4, 5)) 

正确的伪代码如下(书上摘录):

  1. class Mapper 
  2. method Map(string t, integer r) 
  3. Emit(string t, pair (r, 1)) 
  4. class Combiner 
  5. method Combine(string t, pairs [(s1 , c1 ), (s2 , c2 ) . . .]) 
  6. sum ← 0 
  7. cnt ← 0 
  8. for all pair (s, c) ∈ pairs [(s1 , c1 ), (s2 , c2 ) . . .] do 
  9. sum ← sum + s 
  10. cnt ← cnt + c 
  11. Emit(string t, pair (sum, cnt)) 
  12. class Reducer 
  13. method Reduce(string t, pairs [(s1 , c1 ), (s2 , c2 ) . . .]) 
  14. sum ← 0 
  15. cnt ← 0 
  16. for all pair (s, c) ∈ pairs [(s1 , c1 ), (s2 , c2 ) . . .] do 
  17. sum ← sum + s 
  18. cnt ← cnt + c 
  19. ravg ← sum/cnt 
  20. Emit(string t, integer ravg ) 

由于Combiner的输入和输出格式要一样,即Combiner的输入要和Mapper的输出格式一样,Combiner的输出要和Reducer的输入格式一样。所以上面有pairs。参考上面的伪代码编写的代码如下:

Driver:

  1. package org.fansy.date922; 
  2.  
  3. import org.apache.Hadoop.conf.Configuration; 
  4. import org.apache.hadoop.fs.Path; 
  5. import org.apache.hadoop.io.DoubleWritable; 
  6. import org.apache.hadoop.io.Text; 
  7. import org.apache.hadoop.mapreduce.Job; 
  8. import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat; 
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
  10. import org.apache.hadoop.util.GenericOptionsParser; 
  11.  
  12. public class AverageDriver3 { 
  13.     public static void main(String[] args) throws Exception{ 
  14.         // TODO Auto-generated method stub   
  15.         Configuration conf1 = new Configuration();   
  16.         String[] otherArgs = new GenericOptionsParser(conf1, args).getRemainingArgs(); 
  17.         if (otherArgs.length != 2) { 
  18.           System.err.println("Usage: AverageDriver<in> <out>"); 
  19.           System.exit(2); 
  20.         } 
  21.         Job job1 = new Job(conf1, "AverageDriver  job "); 
  22.         job1.setInputFormatClass(KeyValueTextInputFormat.class);     
  23.         job1.setNumReduceTasks(1); 
  24.         job1.setJarByClass(AverageDriver3.class);   
  25.         job1.setMapperClass(AverageM2.class); 
  26.         job1.setMapOutputKeyClass(Text.class); 
  27.         job1.setMapOutputValueClass(TextPair.class); 
  28.         job1.setCombinerClass(AverageC3.class); 
  29.         job1.setReducerClass(AverageR2.class); 
  30.         job1.setOutputKeyClass(Text.class); 
  31.         job1.setOutputValueClass(DoubleWritable.class); 
  32.         KeyValueTextInputFormat.addInputPath(job1, new Path(otherArgs[0])); 
  33.         FileOutputFormat.setOutputPath(job1, new Path(otherArgs[1]));     
  34.         if(!job1.waitForCompletion(true)){ 
  35.             System.exit(1); // run error then exit  
  36.         }   
  37.         System.out.println("************************"); 
  38.     } 
  39. } 

Mapper:

  1. package org.fansy.date922; 
  2.  
  3. import java.io.IOException; 
  4. import org.apache.hadoop.io.DoubleWritable; 
  5. import org.apache.hadoop.io.IntWritable; 
  6. import org.apache.hadoop.io.Text; 
  7. import org.apache.hadoop.mapreduce.Mapper; 
  8.  
  9. public class AverageM3 extends Mapper<Text,Text,Text,TextPair>{ 
  10.  
  11. //  private Text newkey=new Text();  
  12.     private TextPair newvalue=new TextPair(); 
  13.     private DoubleWritable r=new DoubleWritable(); 
  14.     private IntWritable number=new IntWritable(1); 
  15.     public  void map(Text key,Text value,Context context)throws IOException,InterruptedException { 
  16.         // TODO Auto-generated method stub  
  17.         System.out.println(key.toString()); 
  18.         double shuzhi=Double.parseDouble(value.toString()); 
  19.         r.set(shuzhi); 
  20.         newvalue.set(r, number); 
  21.         context.write(key, newvalue); 
  22.     } 
  23. } 

Combiner:

  1. package org.fansy.date922; 
  2.  
  3. import java.io.IOException; 
  4. import org.apache.hadoop.io.DoubleWritable; 
  5. import org.apache.hadoop.io.IntWritable; 
  6. import org.apache.hadoop.io.Text; 
  7. import org.apache.hadoop.mapreduce.Reducer; 
  8.  
  9. public class AverageC3 extends Reducer<Text,TextPair,Text,TextPair>{ 
  10.     private DoubleWritable newvalued=new DoubleWritable(); 
  11.     private IntWritable newvaluei=new IntWritable(); 
  12.     private TextPair newvalue=new TextPair(); 
  13.     public  void reduce(Text key,Iterable<TextPair> values,Context context) throws IOException,InterruptedException{ 
  14.         // TODO Auto-generated method stub  
  15.         double sum= 0.0; 
  16.         int num=0; 
  17.         for(TextPair val:values){ 
  18.             sum+=val.getFirst().get(); 
  19.             num+=val.getSecond().get(); 
  20.         } 
  21.         newvalued.set(sum); 
  22.         newvaluei.set(num); 
  23.         newvalue.set(newvalued,newvaluei); 
  24.         context.write(key, newvalue); 
  25.     } 
  26. } 
linux
相关资讯       Hadoop应用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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