您的位置:首页 > 大数据 > Hadoop

hadoop计算平均值

2014-04-23 16:51 204 查看

combiner是运行在本地的,reduce是收集全部的,比如一个文件很大1G,比如一个文件很大1G,如果你的集群是5台双核的,如果你的集群是5台双核的,这样这16个块会被分到这10个块里面,相当于要2轮,假设是1、2分给1号机,3、4分给2号机,这样1、2求和完了之后会在1号机上运行一次combiner,3、4完了再2号机上运行一次combiner,所有的combiner运行完了,所有的数据会汇集到reduce上做最终处理。

 

 

输入(数据摘自互联网):

data1:

 

data2:

 

程序源代码:

 

[java] view plaincopy  
  1. package org.apache.hadoop.examples;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.DoubleWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15.   
  16. public class CountAverage {  
  17.     public static class AverageMapper extends Mapper<Object, Text, Text, Text>{  
  18.          public void map(Object key, Text value, Context context)   
  19.             throws IOException, InterruptedException{  
  20.              String inputline=value.toString();  
  21.              StringTokenizer itr = new StringTokenizer(inputline);  
  22.              String mapkey="";  
  23.              String mapvalue="";  
  24.              int count=0;  
  25.              while (itr.hasMoreTokens()) {  
  26.                  if(count>0){  
  27.                      mapvalue=itr.nextToken();  
  28.                      continue;  
  29.                  }  
  30.                  mapkey=itr.nextToken();  
  31.                  count++;  
  32.              }  
  33.              context.write(new Text(mapkey),new Text(mapvalue));  
  34.          }  
  35.     }  
  36.       
  37.     public static class AverageCombiner extends Reducer<Text,Text,Text,Text> {  
  38.         public void reduce(Text key, Iterable<Text> values, Context context)  
  39.         throws IOException, InterruptedException {  
  40.             Double sum=0.00;  
  41.             int count=0;  
  42.             for(Text t:values){  
  43.                 sum=sum+Double.parseDouble(t.toString());  
  44.                 count++;  
  45.             }  
  46.             context.write(new Text(key),new Text(sum+"-"+count));  
  47.         }  
  48.     }  
  49.       
  50.     public static class AverageReducer extends Reducer<Text,Text,Text,DoubleWritable> {  
  51.         public void reduce(Text key, Iterable<Text> values, Context context)  
  52.         throws IOException, InterruptedException {  
  53.             Double sum=0.00;  
  54.             int count=0;  
  55.             for(Text t:values){  
  56.                 String[] str=t.toString().split("-");  
  57.                 sum+=Double.parseDouble(str[0]);  
  58.                 count+=Integer.parseInt(str[1]);  
  59.             }  
  60.             context.write(new Text(key),new DoubleWritable(sum/count));  
  61.         }  
  62.     }  
  63.       
  64.     /** 
  65.      * @param args 
  66.      * @throws IOException  
  67.      * @throws ClassNotFoundException  
  68.      * @throws InterruptedException  
  69.      */  
  70.     public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {  
  71.         // TODO Auto-generated method stub  
  72.         Configuration conf = new Configuration();  
  73.         Job job = new Job(conf, "count average");  
  74.         job.setJarByClass(CountAverage.class);  
  75.         job.setMapperClass(AverageMapper.class);  
  76.         job.setCombinerClass(AverageCombiner.class);  
  77.         job.setReducerClass(AverageReducer.class);  
  78.         job.setOutputKeyClass(Text.class);  
  79.         job.setOutputValueClass(Text.class);  
  80.         FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/user/pu/input/*"));  
  81.         FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/user/pu/output/*"));  
  82.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  83.     }  
  84.   
  85. }  



 

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: