MapReduce编程实例2

时间:2023-03-08 20:14:13

MapReduce编程实例:

MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析

MapReduce编程实例(二),计算学生平均成绩

MapReduce编程实例(三),数据去重

MapReduce编程实例(四),排序

MapReduce编程实例(五),MapReduce实现单表关联

实例二,计算学生的平均成绩,每个文件包括所有的学生成绩,格式为 姓名 成绩,有多少个科目,就有多少个输入文件。

如下

小明 23 
小强 57
小红 80
小飞 93
小刚 32
小木 99

实现代码:

  1. import java.io.IOException;
  2. import java.util.Iterator;
  3. import java.util.StringTokenizer;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.FloatWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.util.GenericOptionsParser;
  14. /**
  15. * 计算学生的平均成绩
  16. * 学生成绩以每科一个文件输入
  17. * 文件内容为:姓名 成绩
  18. * @author daT dev.tao@gmail.com
  19. *
  20. */
  21. public class AverageScore {
  22. public static class AverageMapper extends Mapper<Object, Text, Text, FloatWritable>{
  23. @Override
  24. protected void map(Object key, Text value, Context context)
  25. throws IOException, InterruptedException {
  26. String line = value.toString();
  27. StringTokenizer tokens = new StringTokenizer(line,"\n");
  28. while(tokens.hasMoreTokens()){
  29. String tmp = tokens.nextToken();
  30. StringTokenizer sz = new StringTokenizer(tmp);
  31. String name = sz.nextToken();
  32. float score = Float.valueOf(sz.nextToken());
  33. Text outName = new Text(name);//new新的,set老是不对,具体为什么现在也不太清楚。
  34. FloatWritable outScore  = new FloatWritable(score);
  35. context.write(outName, outScore);
  36. }
  37. }
  38. }
  39. public static class AverageReducer extends Reducer<Text, FloatWritable, Text, FloatWritable>{
  40. @Override
  41. protected void reduce(Text key, Iterable<FloatWritable> value,Context context)
  42. throws IOException, InterruptedException {
  43. float sum = 0;
  44. int count = 0;
  45. for(FloatWritable f:value){
  46. sum += f.get();
  47. count ++;//shuffle之后肯定是<名字,<成绩1,成绩2,成绩3....>>故一个value肯定是一门学科
  48. }
  49. FloatWritable averageScore = new FloatWritable(sum/count);////new新的,set老是不对,具体为什么现在也不太清楚。
  50. context.write(key, averageScore);
  51. }
  52. }
  53. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
  54. System.out.println("Begin");
  55. Configuration conf = new Configuration();
  56. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  57. if(otherArgs.length<2){
  58. System.out.println("please input at least 2 arguments");
  59. System.exit(2);
  60. }
  61. Job job = new Job(conf,"Average Score");
  62. job.setJarByClass(AverageScore.class);
  63. job.setMapperClass(AverageMapper.class);
  64. job.setCombinerClass(AverageReducer.class);
  65. job.setReducerClass(AverageReducer.class);
  66. job.setOutputKeyClass(Text.class);
  67. job.setOutputValueClass(FloatWritable.class);
  68. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  69. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  70. System.exit(job.waitForCompletion(true)?0:1);
  71. System.out.println("End");
  72. }
  73. }

配置输入输出参数:

  1. hdfs://localhost:9000/user/dat/average_score_input hdfs://localhost:9000/user/dat/average_score_output

得到输出结果:

小刚 65.333336
小强 80.333336
小明 48.333332
小木 92.333336
小红 83.333336
小飞 83.0

版权声明:本文为博主原创文章,未经博主允许不得转载。