当前位置:   article > 正文

MapReduce--平均分,最高,低分以及及格率的计算

使用mapreduce实现求平均值,最高分,最低分

                    MapReduce--平均分,最高,低分以及及格率的计算

计算班级的平均分,以及个人的最高最低分,以及每个班级的及格率。

来先看一下我的数据。

  1. 时间 班级 姓名 科目 成绩
  2. 20180501 1708a1 li bishi 80
  3. 20180501 1708a1 li jishi 55
  4. 20180501 1708a1 li project 90
  5. 20180501 1708a1 li2 bishi 80
  6. 20180501 1708a1 li2 jishi 20
  7. 20180501 1708a1 li2 project 90
  8. 20180501 1708a1 li3 bishi 50
  9. 20180501 1708a1 li3 jishi 70
  10. 20180501 1708a1 li3 project 60
  11. 20180501 1708a1 zhangsan bishi 88
  12. 20180501 1708a1 zhangsan jishi 55
  13. 20180501 1708a1 zhangsan project 98
  14. 20180501 1708a1 lishi bishi 18
  15. 20180501 1708a1 lishi jishi 15
  16. 20180501 1708a1 lishi project 15
  17. 20180501 1708a1 wangwu bishi 88
  18. 20180501 1708a1 wangwu jishi 76
  19. 20180501 1708a1 wangwu project 70
  20. 20180501 1708a2 li1 bishi 80
  21. 20180501 1708a2 li1 jishi 71
  22. 20180501 1708a2 li1 project 96
  23. 20180501 1708a2 li2 bishi 80
  24. 20180501 1708a2 li2 jishi 26
  25. 20180501 1708a2 li2 project 90
  26. 20180501 1708a2 li3 bishi 80
  27. 20180501 1708a2 li3 jishi 55
  28. 20180501 1708a2 li3 project 90
  29. 20180501 1708a2 zhangliang bishi 81
  30. 20180501 1708a2 zhangliang jishi 55
  31. 20180501 1708a2 zhangliang project 98
  32. 20180501 1708a2 liuli bishi 70
  33. 20180501 1708a2 liuli jishi 95
  34. 20180501 1708a2 liuli project 75
  35. 20180501 1708a2 wangwu bishi 80
  36. 20180501 1708a2 wangwu jishi 76
  37. 20180501 1708a2 wangwu project 70
  38. 20180501 1708a2 zhangxi bishi 18
  39. 20180501 1708a2 zhangxi jishi 16
  40. 20180501 1708a2 zhangxi project 10

数据之间是空格。。。。

代码来了 -- 平均分,最高分,最低分

  1. package com.huhu.day01;
  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.LongWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. /**
  13. * 切割文本: 平均分,最高低分
  14. *
  15. * @author huhu_k
  16. *
  17. */
  18. public class HomeWork2 {
  19. // map
  20. public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
  21. Text keys = new Text();
  22. Text values = new Text();
  23. @Override
  24. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  25. // 数据切割方式(文本中的内容)
  26. // 按行分
  27. String[] line = value.toString().split(" ");
  28. keys.set(line[0] + ":" + line[2]);
  29. values.set(line[3] + ":" + line[4]);
  30. context.write(keys, values);
  31. }
  32. }
  33. // reduce
  34. public static class MyReducer extends Reducer<Text, Text, Text, Text> {
  35. @Override
  36. protected void reduce(Text key, Iterable<Text> value, Context context)
  37. throws IOException, InterruptedException {
  38. int max = Integer.MIN_VALUE;
  39. int min = Integer.MAX_VALUE;
  40. // 和
  41. int sum = 0;
  42. // 人数
  43. int count = 0;
  44. // 分数
  45. int score = 0;
  46. String classs = "";
  47. for (Text t : value) {
  48. classs = t.toString().split(":")[0];
  49. score = Integer.parseInt(t.toString().split(":")[1]);
  50. if (max < score)
  51. max = score;
  52. if (min > score)
  53. min = score;
  54. switch (classs) {
  55. case "bishi":
  56. score += score * 0.4;
  57. break;
  58. case "jishi":
  59. score += score * 0.3;
  60. break;
  61. case "project":
  62. score += score * 0.3;
  63. break;
  64. }
  65. sum += score;
  66. count++;
  67. }
  68. int avg = (int) sum / count;
  69. String[] student = key.toString().split(":");
  70. Text ky = new Text(student[0] + "\t" + student[1]);
  71. context.write(ky, new Text("平均分 " + avg));
  72. context.write(ky, new Text("最高值为 " + max));
  73. context.write(ky, new Text("最低值 " + min));
  74. }
  75. }
  76. public static void main(String[] args) throws Exception {
  77. // 配置容器
  78. Configuration conf = new Configuration();
  79. // 创建一个job
  80. @SuppressWarnings("deprecation")
  81. Job job = new Job(conf, "MyMapReduce Two");
  82. // 配置job
  83. job.setJarByClass(HomeWork2.class);
  84. job.setMapperClass(MyMapper.class);
  85. job.setMapOutputKeyClass(Text.class);
  86. job.setMapOutputValueClass(Text.class);
  87. job.setReducerClass(MyReducer.class);
  88. job.setOutputKeyClass(Text.class);
  89. job.setOutputValueClass(Text.class);
  90. // 输入输出
  91. FileInputFormat.addInputPath(job, new Path(args[0]));
  92. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  93. // 执行程序
  94. boolean waitForCompletion = job.waitForCompletion(true);
  95. System.exit(waitForCompletion ? 0 : 1);
  96. }
  97. }

运行结果:




2.及格率

  1. package com.huhu.day01;
  2. import java.io.IOException;
  3. import java.text.DecimalFormat;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  15. /**
  16. * 切割文本:及格率
  17. *
  18. * @author huhu_k
  19. *
  20. */
  21. public class HomeWork3 {
  22. // map
  23. public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
  24. Text keys = new Text();
  25. Text values = new Text();
  26. @Override
  27. protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  28. // 数据切割方式(文本中的内容)
  29. // 按行分
  30. String[] line = value.toString().split(" ");
  31. keys.set(line[0] + ":" + line[1]);
  32. context.write(keys, value);
  33. }
  34. }
  35. // reduce
  36. public static class MyReducer extends Reducer<Text, Text, Text, Text> {
  37. Map<String, Double> map = new HashMap<>();
  38. Map<String, String> maps = new HashMap<>();
  39. @Override
  40. protected void reduce(Text key, Iterable<Text> value, Context context)
  41. throws IOException, InterruptedException {
  42. for (Text t : value) {
  43. String[] values = t.toString().split(" ");
  44. String student = values[2] + ":" + values[0] + ":" + values[1];
  45. String subject = values[3];
  46. double score = Integer.valueOf(values[4]);
  47. if ("bishi".equals(subject)) {
  48. score *= 0.4;
  49. } else {
  50. score *= 0.3;
  51. }
  52. // 如果map中有学生,累加学生的没门课程的分数
  53. if (map.containsKey(student)) {
  54. double scores = map.get(student);
  55. scores += score;
  56. map.put(student, scores);
  57. } else {
  58. // 第一次进入时不包含,则直接添加
  59. map.put(student, score);
  60. }
  61. }
  62. for (Map.Entry<String, Double> m : map.entrySet()) {
  63. String classname = m.getKey().split(":")[2];
  64. Double score = m.getValue();
  65. if (maps.containsKey(classname) && score >= 60) {
  66. String k = Integer.parseInt(maps.get(classname).split(":")[0]) + 1 + "";
  67. String v = Integer.parseInt(maps.get(classname).split(":")[1]) + 1 + "";
  68. maps.put(classname, k + ":" + v);
  69. } else if (maps.containsKey(classname) && score < 60) {
  70. String k = Integer.parseInt(maps.get(classname).split(":")[0]) + 1 + "";
  71. String v = Integer.parseInt(maps.get(classname).split(":")[1]) + "";
  72. maps.put(classname, k + ":" + v);
  73. } else if (!maps.containsKey(classname) && score < 60) {
  74. maps.put(classname, "1:0");
  75. } else if (!maps.containsKey(classname) && score >= 60) {
  76. maps.put(classname, "1:1");
  77. }
  78. }
  79. }
  80. @Override
  81. protected void cleanup(Reducer<Text, Text, Text, Text>.Context context)
  82. throws IOException, InterruptedException {
  83. for (Map.Entry<String, String> m : maps.entrySet()) {
  84. DecimalFormat d = new DecimalFormat("0.00%");
  85. double pass = Double.valueOf(m.getValue().split(":")[1]) / Double.valueOf(m.getValue().split(":")[0]);
  86. context.write(new Text(m.getKey()), new Text("及格率为:" + d.format(pass)));
  87. }
  88. }
  89. }
  90. public static void main(String[] args) throws Exception {
  91. // 配置容器
  92. Configuration conf = new Configuration();
  93. // 创建一个job
  94. @SuppressWarnings("deprecation")
  95. Job job = new Job(conf, "MyMapReduce Count");
  96. // 配置job
  97. job.setJarByClass(HomeWork3.class);
  98. job.setMapperClass(MyMapper.class);
  99. job.setMapOutputKeyClass(Text.class);
  100. job.setMapOutputValueClass(Text.class);
  101. job.setReducerClass(MyReducer.class);
  102. job.setOutputKeyClass(Text.class);
  103. job.setOutputValueClass(Text.class);
  104. // 输入输出
  105. FileInputFormat.addInputPath(job, new Path(args[0]));
  106. FileOutputFormat.setOutputPath(job, new Path(args[1]));
  107. // 执行程序
  108. boolean waitForCompletion = job.waitForCompletion(true);
  109. System.exit(waitForCompletion ? 0 : 1);
  110. }
  111. }


   MapReduce一个分布式并行离线计算框架。我们只需要知道map(),reduce(),input,output,剩下的由框架完成



基于yarn的工作流程




转载于:https://www.cnblogs.com/meiLinYa/p/9231026.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/797083
推荐阅读
相关标签
  

闽ICP备14008679号