问题描述

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

                   

编程思想

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

代码

 package org.apache.hadoop.examples;

 import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class calcGPA {
public calcGPA() {
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration(); String fileAddress = "hdfs://localhost:9000/user/hadoop/"; //String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
String[] otherArgs = new String[]{fileAddress+"score1.txt", fileAddress+"score2.txt", fileAddress+"score3.txt", fileAddress+"output"};
if(otherArgs.length < 2) {
System.err.println("Usage: calcGPA <in> [<in>...] <out>");
System.exit(2);
} Job job = Job.getInstance(conf, "calc GPA");
job.setJarByClass(calcGPA.class);
job.setMapperClass(calcGPA.TokenizerMapper.class);
job.setCombinerClass(calcGPA.IntSumReducer.class);
job.setReducerClass(calcGPA.IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
} FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true)?0:1);
} public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public IntSumReducer() {
} public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int count = 0; IntWritable val;
for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get(),count++) {
val = (IntWritable)i$.next();
} int average = (int)sum/count;
context.write(key, new IntWritable(average));
}
} public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { public TokenizerMapper() {
} public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString(), "\n"); while(itr.hasMoreTokens()) {
StringTokenizer iitr = new StringTokenizer(itr.nextToken());
String name = iitr.nextToken();
String score = iitr.nextToken();
context.write(new Text(name), new IntWritable(Integer.parseInt(score)));
} }
}
}

疑问

在写这个的时候,我遇到个问题,就是输入输出文件的默认地址,为什么是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. 特殊字符url编码以后再解码后出现错误(&not , &cent, &curren, &pound)

    Url编码的原内容是 “&notify_url=xxxx”  经过url编码以后再解码回来  “&not”的部分就变成了“¬” 解决方案:把原文里面待url编码的&符号先替换成 ...

  2. Golang覆盖写入文件的小坑

    记录一点Golang文件操作的笔记,环境:Ubuntu // 删除文件 func removeFile() { err := os.Remove("test.txt") if er ...

  3. PowerPoint使用技巧

    1.右键Group两个元素,可以一起移动: 2.Insert 屏幕输入功能: 3.录制旁白: 4.录制完旁白之后可以生成视频: 5.如果不确定所有引用的组件是否可以在别的机器上使用,可以导出只CD,生 ...

  4. mysql in 排序 也可以按in里面的顺序来排序

    SQL: select * from table where id IN (3,9,6);这样的情况取出来后,其实,id还是按3,6,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能 ...

  5. 【Python全栈】HTML <!--...--> 注释 、CSS/JS //注释 和 /*.....*/ 注释

    HTML <!--...--> 注释 .CSS/JS //注释 和 /*.....*/ 注释 <!-- -->是HTML的注释标签,使用 < 和 > 是符合HTML ...

  6. Win7-IE11 For x86&x64离线安装包

    一.Internet Explorer11简体中文版离线安装包:       微软已停止了IE11以下版本(包括IE10/9/8)的技术支持.以后Win7用IE11的机会也越来越多,但IE11官方安装 ...

  7. python练习题-day11

    1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...

  8. Mac破解免费office软件

    一.下载安装包及破解工具 地址: https://pan.baidu.com/s/1hugtoLQ 密码: s5in 附官网地址 文件夹中包含三个文件: Office16安装文件 Microsoft_ ...

  9. python 当前时间多加一天、一小时、一分钟

    datetime模块 import datetime # 获取当前时间 print(datetime.datetime.now()) # 2017-07-15 15:01:24.619000 # 格式 ...

  10. HBase笔记6 过滤器

    过滤器 过滤器是GET或者SCAN时过滤结果用的,相当于SQL的where语句 HBase中的过滤器创建后会被序列化,然后分发到各个region server中,region server会还原过滤器 ...