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