问题描述

现在有三个文件分别代表学生的各科成绩,编程求各位同学的平均成绩。

                   

编程思想

map函数将姓名作为key,成绩作为value输出,reduce根据key即可将三门成绩相加。

代码

  1. package org.apache.hadoop.examples;
  2.  
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.StringTokenizer;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  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. public class calcGPA {
  17. public calcGPA() {
  18. }
  19.  
  20. public static void main(String[] args) throws Exception {
  21. Configuration conf = new Configuration();
  22.  
  23. String fileAddress = "hdfs://localhost:9000/user/hadoop/";
  24.  
  25. //String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
  26. String[] otherArgs = new String[]{fileAddress+"score1.txt", fileAddress+"score2.txt", fileAddress+"score3.txt", fileAddress+"output"};
  27. if(otherArgs.length < 2) {
  28. System.err.println("Usage: calcGPA <in> [<in>...] <out>");
  29. System.exit(2);
  30. }
  31.  
  32. Job job = Job.getInstance(conf, "calc GPA");
  33. job.setJarByClass(calcGPA.class);
  34. job.setMapperClass(calcGPA.TokenizerMapper.class);
  35. job.setCombinerClass(calcGPA.IntSumReducer.class);
  36. job.setReducerClass(calcGPA.IntSumReducer.class);
  37. job.setOutputKeyClass(Text.class);
  38. job.setOutputValueClass(IntWritable.class);
  39.  
  40. for(int i = 0; i < otherArgs.length - 1; ++i) {
  41. FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
  42. }
  43.  
  44. FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
  45. System.exit(job.waitForCompletion(true)?0:1);
  46. }
  47.  
  48. public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  49.  
  50. public IntSumReducer() {
  51. }
  52.  
  53. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
  54. int sum = 0;
  55. int count = 0;
  56.  
  57. IntWritable val;
  58. for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get(),count++) {
  59. val = (IntWritable)i$.next();
  60. }
  61.  
  62. int average = (int)sum/count;
  63. context.write(key, new IntWritable(average));
  64. }
  65. }
  66.  
  67. public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
  68.  
  69. public TokenizerMapper() {
  70. }
  71.  
  72. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  73. StringTokenizer itr = new StringTokenizer(value.toString(), "\n");
  74.  
  75. while(itr.hasMoreTokens()) {
  76. StringTokenizer iitr = new StringTokenizer(itr.nextToken());
  77. String name = iitr.nextToken();
  78. String score = iitr.nextToken();
  79. context.write(new Text(name), new IntWritable(Integer.parseInt(score)));
  80. }
  81.  
  82. }
  83. }
  84. }

疑问

在写这个的时候,我遇到个问题,就是输入输出文件的默认地址,为什么是user/hadoop/,我看了一下配置文件的信息,好像也没有出现过这个地址啊,希望有人能解答一下,万分感谢。

MapReduce编程:平均成绩的更多相关文章

  1. Hadoop 学习笔记 (十一) MapReduce 求平均成绩

    china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五    84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...

  2. 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行

    [TOC] 简单的java Hadoop MapReduce程序(计算平均成绩)从打包到提交及运行 程序源码 import java.io.IOException; import java.util. ...

  3. mapreduce实现学生平均成绩

    思路: 首先从文本读入一行数据,按空格对字符串进行切割,切割后包含学生姓名和某一科的成绩,map输出key->学生姓名    value->某一个成绩 然后在reduce里面对成绩进行遍历 ...

  4. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  5. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  6. MapReduce编程实例6

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  7. MapReduce编程实例5

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  8. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  9. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

随机推荐

  1. Oracle 修改表 Alter Table...

    --增加列ALTER TABLE Student add sex number(2);--删除列ALTER TABLE Student drop column sex;--更改列属性 ALTER TA ...

  2. date格式互转

    +"%Y/%m/%d-%H:%M:%S" date -d "2017/11/21 17:02:09" +%s

  3. react子传父

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. JS实现表格使用上下左右键聚集

    //调用:new tabTableInput("tblGrid","text"); var tabTableInput = function (tableId, ...

  5. Dijkstra模板

    Dijkstra struct node { long long x,d; node(); node(long long xx,long long dd){ x = xx; d = dd; } }; ...

  6. vue里面的v-for列表循环

    列表渲染 v-for v-for可以把数据中的一个数组对应为一组元素v-for 指令需要以 item in items 形式的特殊语法, items 是源数据数组并且 item 是数组元素迭代的别名. ...

  7. C++11 std::ref使用场景

    C++本身有引用(&),为什么C++11又引入了std::ref(或者std::cref)? 主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用.如下例子: # ...

  8. C++中vector使用详细说明 (转)

    转自:http://blog.chinaunix.net/uid-26000296-id-3785610.html http://www.cnblogs.com/mr-wid/archive/2013 ...

  9. ADC裸机程序

    硬件平台:JZ2440 实现功能:通过采集触摸屏ADC的电压值,推算触摸xy坐标 start.s init.c nand.c interrupt.c uart.c uart.h my_stdio.c ...

  10. spring BeanFactory VS FactoryBean

    一.FactoryBean示例 public class DateStringFactoryBean implements FactoryBean<Object> { private bo ...