mao/reduce實現求平均值

林六天發表於2014-07-07
 1 import java.io.*;
 2 import java.util.*;
 3 
 4 import org.apache.hadoop.fs.Path;
 5 import org.apache.hadoop.io.*;
 6 import org.apache.hadoop.mapreduce.*;
 7 import org.apache.hadoop.mapreduce.lib.output.*;
 8 import org.apache.hadoop.mapreduce.lib.input.*;
 9 import org.apache.hadoop.conf.*;
10 import org.apache.hadoop.util.*;
11 public class Score_Process extends Configured implements Tool {
12     /**
13      * 程式說明:主要用來實現計算學生的平均成績。
14      * 資料輸入:檔案形式輸入,每一行包含學生姓名 學生成績。一個學生有多門成績則有多行。例如:張三 98
15      * 資料輸出:張三 84 學生姓名 學生平均成績
16      * 實現思路:在map階段<張三,(98,68,……)>
17      * **/
18     
19     public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
20         public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
21             String line=value.toString();
22             System.out.println(line);//測試
23             StringTokenizer tokenizer=new StringTokenizer(line);
24             while(tokenizer.hasMoreTokens()){
25                 String name=tokenizer.nextToken();
26                 String strscore=tokenizer.nextToken();
27                 int intscore=Integer.parseInt(strscore);
28                 context.write(new Text(name), new IntWritable(intscore));
29             }
30             
31         }
32         
33     }
34     
35     public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
36         public void reduce(Text key,Iterable<IntWritable>values,Context context) throws IOException, InterruptedException{
37             int sun=0,count=0;
38             for(IntWritable val:values){
39                 sun+=val.get();
40                 count++;
41             }
42             int averscore=(int)sun/count;
43             context.write(key, new IntWritable(averscore));
44         }
45         
46     }
47     
48     public int run(String[] args) throws Exception{
49         Job job=new Job(getConf());
50         job.setJarByClass(Score_Process.class);
51         job.setJobName("Score_Process");
52         
53         job.setOutputKeyClass(Text.class);
54         job.setOutputValueClass(IntWritable.class);
55         
56         job.setMapperClass(Map.class);
57         //job.setCombinerClass(Reduce.class);
58         job.setReducerClass(Reduce.class);
59         
60         job.setInputFormatClass(TextInputFormat.class);
61         job.setOutputFormatClass(TextOutputFormat.class);
62         
63         FileInputFormat.setInputPaths(job, new Path(args[0]));
64         FileOutputFormat.setOutputPath(job, new Path(args[1]));
65         boolean success=job.waitForCompletion(true);
66         
67         return success?0:1;
68     }
69     
70     public static void main(String[] args)throws Exception{
71         int ret=ToolRunner.run(new Score_Process(), args);
72         System.exit(ret);
73     }
74 
75 }

 

相關文章