Home >Database >Mysql Tutorial >hadoop mapreduce求平均分

hadoop mapreduce求平均分

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 14:58:432399browse

hadoop mapreduce求平均分 求平均分的关键在于,利用mapreduce过程中,一个key聚合在一起,输送到一个reduce的特性。 假设三门课的成绩如下: china.txt [plain] 张三 78 李四 89 王五 96 赵六 67 english.txt [plain] 张三 80 李四 82 王五 84 赵六 86 math

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 {  

          

        // 实现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 {  

        // 实现reduce函数  

        public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {  

            int sum = 0;  

            int count = 0;  

            Iterator 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  

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn