当前位置:操作系统 > Unix/Linux >>

hadoop mapreduce求平均分

hadoop mapreduce求平均分
 
求平均分的关键在于,利用mapreduce过程中,一个key聚合在一起,输送到一个reduce的特性。
 
假设三门课的成绩如下:
 
china.txt
 
[plain] 
张三    78  
李四    89  
王五    96  
赵六    67  
 
english.txt
[plain] 
张三    80  
李四    82  
王五    84  
赵六    86  
 
math.txt
[plain] 
张三  88  
李四  99  
王五  66  
赵六  72  
 
mapreduce如下:
[plain] 
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {  
          
        // 实现map函数  
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {  
            // 将输入的纯文本文件的数据转化成String  
            String line = value.toString();  
            // 将输入的数据首先按行进行分割  
            StringTokenizer tokenizerArticle = new StringTokenizer(line, "\n");  
            // 分别对每一行进行处理  
            while (tokenizerArticle.hasMoreElements()) {  
                // 每行按空格划分  
                StringTokenizer tokenizerLine = new StringTokenizer(tokenizerArticle.nextToken());  
                String strName = tokenizerLine.nextToken();// 学生姓名部分  
                String strScore = tokenizerLine.nextToken();// 成绩部分  
                Text name = new Text(strName);  
                int scoreInt = Integer.parseInt(strScore);  
                // 输出姓名和成绩  
                context.write(name, new IntWritable(scoreInt));  
            }  
        }  
    }  
      
    public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {  
        // 实现reduce函数  
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
            int sum = 0;  
            int count = 0;  
            Iterator<IntWritable> iterator = values.iterator();  
            while (iterator.hasNext()) {  
                sum += iterator.next().get();// 计算总分  
                count++;// 统计总的科目数  
            }  
            int average = (int) sum / count;// 计算平均成绩  
            context.write(key, new IntWritable(average));  
        }  
    }  
 
输出如下:
[plain] 
张三  82  
李四  90  
王五  82  
赵六  75  
 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,