参考:http://eric-gcm.iteye.com/blog/1807468

math.txt:

张三    88
李四 99
王五 66
赵六 77

china.txt:

张三    78
李四 89
王五 96
赵六 67

english.txt:

张三    80
李四 82
王五 84
赵六 86

JAVA代码:

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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class Score { public static class Map extends
Mapper<LongWritable, Text, Text, IntWritable> { // 实现map函数
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { // 将输入的纯文本文件的数据转化成String
String line = value.toString(); // 将输入的数据首先按行进行分割
StringTokenizer tokenizerArticle = new StringTokenizer(line, "\n"); // 分别对每一行进行处理
while (tokenizerArticle.hasMoreElements()) { // 每行按空格划分
StringTokenizer tokenizerLine = new StringTokenizer(
tokenizerArticle.nextToken()); String strName = tokenizerLine.nextToken();// 学生姓名部分
String strScore = tokenizerLine.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", "172.16.11.74:9001"); String[] ioArgs = new String[] { "score_in", "score_out" };
String[] otherArgs = new GenericOptionsParser(conf, ioArgs)
.getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: Score Average <in> <out>");
System.exit(2);
}
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); // 将输入的数据集分割成小数据块splites,提供一个RecordReder的实现
job.setInputFormatClass(TextInputFormat.class); // 提供一个RecordWriter的实现,负责数据输出
job.setOutputFormatClass(TextOutputFormat.class); // 设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

Score

运行结果:

张三    82
李四 90
王五 82
赵六 76

具体打包运行步骤:

参考博文:http://www.cnblogs.com/-wangjiannan/p/3590324.html

hadoop —— MapReduce例子 (求平均值)的更多相关文章

  1. Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货

    文章为作者原创,未经许可,禁止转载.    -Sun Yat-sen University 冯兴伟 一.    项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...

  2. hadoop —— MapReduce例子 (数据去重)

    参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...

  3. Mapreduce实例--求平均值

    求平均数是MapReduce比较常见的算法,求平均数的算法也比较简单,一种思路是Map端读取数据,在数据输入到Reduce之前先经过shuffle,将map函数输出的key值相同的所有的value值形 ...

  4. hadoop —— MapReduce例子 (数据排序)

    参考:http://eric-gcm.iteye.com/blog/1807468 file1.txt: 2 32 654 32 15 756 65223 file2.txt: 5956 22 650 ...

  5. MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案

    1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...

  6. Hadoop MapReduce执行过程详解(带hadoop例子)

    https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...

  7. 三.hadoop mapreduce之WordCount例子

    目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...

  8. hadoop mapreduce 简单例子

    本例子统计 用空格分开的单词出现数量(  这个Main.mian 启动方式是hadoop 2.0 的写法.1.0 不一样 ) 目录结构: 使用的 maven : 下面是maven 依赖. <de ...

  9. Hadoop 1.2.1 MapReduce 例子

    自学hadoop真的很困难,主要是hadoop版本太混乱了,各个版本之间兼容性并不算太好.更主要的是网上的很多MapReduce的Java例子不写import!!!只写类名!!!偏偏Hadoop中有很 ...

随机推荐

  1. 线段树专题—ZOJ1610 Count the Colors

    题意:给一个n,代表n次操作,接下来每次操作表示把[l.r]区间的线段涂成k的颜色当中,l,r,k的范围都是0到8000 分析:事实上就是拿线段树维护一段区间的颜色,整体用到的是线段树的区间更新把,可 ...

  2. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  3. 转:java工程师成神之路

    转自: http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 htt ...

  4. 【Excle数据透视表】如何重命名数据透视表

    如下图,是新生成的一个数据透视简表,现在需要将其数据透视表的名称修改为:汇总数据 解决办法 修改后的效果如下:

  5. C语言之基本算法32—鞍点

    //数组 /* ================================================================== 题目:求随意矩阵的全部鞍点.并统计个数.(在矩阵中 ...

  6. TFT、LCD、OLED、LPTS、CRT等显示屏的区别

    1.TFT TFT(Thin Film Transistor)是薄膜晶体管的缩写.TFT式显示屏是各类笔记本电脑和台式机上的主流显示设备,该类显示屏上的每个液晶像素点都是由集成在像素点后面的薄膜晶体管 ...

  7. 想全面理解JWT?一文足矣!

    有篇关于JWT的文章,叫"JWT: The Complete Guide to JSON Web Tokens",写得全面细致.为了自己能更清晰理解并惠及更多人,我把它大致翻译了过 ...

  8. CGI FASTCGI php-fpm

    CGI(Common Gateway Interface) CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工 ...

  9. Windows下安装Oracle 11g全过程。

    1. 解压Oracle11.1.0.6 for win32,然后点击setup 2.选择高级安装,下一步 3.选择企业版,下一步 4.修改Oracle基目录,也可以是默认,下一步 5.将状态复选框打上 ...

  10. HTML5实现两个视频循环播放!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...