思路:

  首先从文本读入一行数据,按空格对字符串进行切割,切割后包含学生姓名和某一科的成绩,map输出key->学生姓名    value->某一个成绩

  然后在reduce里面对成绩进行遍历求和,求平均数,然后输出key->学生姓名    value->平均成绩

  源数据:

   chines.txt 

zhangsan    78
lisi 89
wangwu 96
zhaoliu 67

  english.txt

zhangsan    80
lisi 82
wangwu 84
zhaoliu 86

  math.txt

zhangsan    88
lisi 99
wangwu 66
zhaoliu 77

  源代码:

package com.duking.hadoop;

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.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.GenericOptionsParser; public class Score { public static class Map extends Mapper<Object, Text, Text, IntWritable> { // 实现map函数 public void map(Object key, Text value, Context context) throws IOException, InterruptedException { // 将输入的纯文本文件的数据转化成String String line = value.toString(); // 将输入的数据首先按行进行分割 StringTokenizer tokenizerArticle = new StringTokenizer(line); //以空格分隔字符串 // 分别对每一行进行处理 while (tokenizerArticle.hasMoreElements()) { String strName= tokenizerArticle.nextToken(); // 学生姓名部分 String strScore = tokenizerArticle.nextToken();// 成绩部分 Text name = new Text(strName); int scoreInt = Integer.parseInt(strScore);
// 输出姓名和成绩 context.write(name, new IntWritable(scoreInt)); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { // 实现reduce函数 public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; int count = 0; Iterator<IntWritable> iterator = values.iterator(); //循环遍历成绩 while (iterator.hasNext()) { sum += iterator.next().get();// 计算总分 count++;// 统计总的科目数 } int average = (int) sum / count;// 计算平均成绩 context.write(key, new IntWritable(average)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("mapred.job.tracker", "192.168.60.129:9000"); // 指定带运行参数的目录为输入输出目录
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs(); /*
* 指定工程下的input2为文件输入目录 output2为文件输出目录 String[] ioArgs = new String[] {
* "input2", "output2" };
*
* String[] otherArgs = new GenericOptionsParser(conf, ioArgs)
* .getRemainingArgs();
*/ if (otherArgs.length != 2) { // 判断路径参数是否为2个 System.err.println("Usage: Data Deduplication <in> <out>"); System.exit(2); } // set maprduce job name
Job job = new Job(conf, "Score Average"); job.setJarByClass(Score.class); // 设置Map、Combine和Reduce处理类 job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); // 设置输出类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); // 设置输入和输出目录 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

  

mapreduce实现学生平均成绩的更多相关文章

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

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

  2. Hadoop 学生平均成绩

    1.实例描述 通过一个计算学生平均成绩的例子来讲解开发MapReduce程序的流程.输入文件都是纯文本文件,输入文件中的每行内容均为一个学生的姓名和他相应的成绩,如果有多门学科,则每门学科为一个文件. ...

  3. PTA的Python练习题(十二)-第4章-7 统计学生平均成绩与及格人数

    第4章-7 统计学生平均成绩与及格人数 a=eval(input()) b=list(map(int,input().split())) sum=sum(b) c=[i for i in b if i ...

  4. MapReduce编程:平均成绩

    问题描述 现在有三个文件分别代表学生的各科成绩,编程求各位同学的平均成绩.                     编程思想 map函数将姓名作为key,成绩作为value输出,reduce根据key ...

  5. SQL 查询:查询学生平均成绩

    编程萌新,因为遇到这么个SQL 查询的问题:在一张表A里有如下字段:学生姓名.学科名.学科成绩.写一条SQL 语句查出各科平均成绩并按学生姓名分组,按如下格式显示:学生姓名|语文|数学|英语.一开始遇 ...

  6. HDU2023-求平均成绩

    描述: 假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量. 输入数据有多个测试实例,每个测试 ...

  7. sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)

    题记:一直在写各种sql查询语句,最长的有一百多行,自信什么需求都可以接,可......,想了想,可能一直在固定的场景下写,平时也是以满足实际需求为目的,竟不知道应试的题都是怎么出的,又应该怎么做.遂 ...

  8. 案例:利用累加器计算前N个学生的总成绩和平均成绩

    /* *录入N个学生的成绩,并求出这些学生的总成绩和平均成绩! * */ import java.util.Scanner; public class SumTest{ public static v ...

  9. /* * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据 *(包括学生号,姓名,三门课成绩),计算出平均成绩, *将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。 */

    1.Student类:类中有五个变量,分别是学号,姓名,三门成绩 package test3; public class Student { private int num; private Stri ...

随机推荐

  1. CSU-1632 Repeated Substrings[后缀数组求重复出现的子串数目]

    评测地址:https://cn.vjudge.net/problem/CSU-1632 Description 求字符串中所有出现至少2次的子串个数 Input 第一行为一整数T(T<=10)表 ...

  2. B - The Suspects(并查集)

    B - The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Desc ...

  3. git commit -a -m "M 1、引入mixin,公共样式mixin传参处理;";git push origin master:master

    <script> import wepy from 'wepy' import api from '../api/api' export default class recharge ex ...

  4. ASP非法赋值

    Microsoft VBScript 运行时错误 错误 '800a01f5' 非法赋值: 'isCloudSpeedupMz' /records/config/class-records.asp,行 ...

  5. Centos中查询目录中内容命名ls(六)

    首先解释下这块, root代表当前登录用户,localhost代表主机名, ~代表当前主机目录,#代表用户权限 #表示超级用户,$表示普通用户: 查询目录中内容命令 ls  (list缩写) 格式 l ...

  6. CalendarUtil 日期操作工具类

    版权声明:本文为博主原创文章,未经博主允许不得转载. [java] view plain copy import java.util.Calendar; import java.text.DateFo ...

  7. python并发编程&多线程(二)

    前导理论知识见:python并发编程&多线程(一) 一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 官网链 ...

  8. mysql用户授权以及权限收回

    语法 GRANT privileges [(columns)] ON DATABASE.TABLE TO 'username'@'hostname' [IDENTIFIED BY [PASSWORD] ...

  9. 《Python机器学习》笔记(五)

    通过降维压缩数据 在前面已经介绍了几种不同的特征选择技术对数据集进行降维的方法.另一种常用于降维的特征选择方法就是特征抽取.数据压缩也是机器学习领域中的一个重要内容.数据压缩技术可以帮助我们对数据及逆 ...

  10. 2015/7/29 (高开,V形反转,各种指标背离——可惜没买进,填补空缺图形的心理分析)

    1.李大--謝先生℡:早盘决策:如今日再次出现大幅低开  或者盘中大幅下跌可逢低 3成仓位左右分散资金做短线抄底,切记是超短 绝不追高,设置5个点止损.市场有很多名家在谈论3373点即前低点,本人告诉 ...