Hadoopmapreduce average score

Source: Internet
Author: User
The key to calculating the average score of hadoopmapreduce is that a key is aggregated during the mapreduce process and delivered to a reduce. Assume that the scores of the three courses are as follows: china.txt?plain=zhang san78li si89wang wu96zhao liu67english.txt [plain] Zhang san80 Li 482 Wang wu84 Zhao liu86 math

The key to calculating the average score of hadoop mapreduce is that a key is aggregated during the mapreduce process and delivered to a reduce. Assume that the scores of the three courses are as follows: china.txt [plain] Zhang San 78 Li Si 89 Wang Wu 96 Zhao Liu 67 english.txt [plain] Zhang San 80 Li Si 82 Wang Wu 84 Zhao Liu 86 math

Hadoop mapreduce average score

The key to calculating the average score is that a key is aggregated in the mapreduce process and delivered to a reduce.

Assume that the scores of the three courses are as follows:

China.txt

[Plain]

John 78

Li Si 89

Wang Wu 96

Zhao liu67

English.txt

[Plain]

Zhang San 80

Li Si 82

Wang Wu 84

Zhao liu86

Math.txt

[Plain]

John 88

Li Si 99

Wang Wu 66

Zhao liu72

Mapreduce is as follows:

[Plain]

Public static class Map extends Mapper {

// Implement the map function

Public void map (LongWritable key, Text value, Context context) throws IOException, InterruptedException {

// Convert the data of the input plain text file to a String

String line = value. toString ();

// Split the input data by rows.

StringTokenizer tokenizerArticle = new StringTokenizer (line, "\ n ");

// Process each row separately

While (tokenizerArticle. hasMoreElements ()){

// Each line is divided by space

StringTokenizer tokenizerLine = new StringTokenizer (tokenizerArticle. nextToken ());

String strName = tokenizerLine. nextToken (); // student name section

String strScore = tokenizerLine. nextToken (); // score

Text name = new Text (strName );

Int scoreInt = Integer. parseInt (strScore );

// Output the name and score

Context. write (name, new IntWritable (scoreInt ));

}

}

}

Public static class Reduce extends Reducer {

// Implement the reduce function

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 (); // calculate the total score

Count ++; // count the total number of subjects

}

Int average = (int) sum/count; // calculate the average score

Context. write (key, new IntWritable (average ));

}

}

The output is as follows:

[Plain]

Zhang San 82

Li Si 90

Wang wu82

Zhao liu75

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.