参考: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

最新文章

  1. SQLServer 数据库查看死锁、堵塞的SQL语句
  2. Java基础-final和static的区别
  3. 学习笔记:APP切图那点事儿–详细介绍android和ios平台
  4. leetcode 118 Pascal&#39;s Triangle ----- java
  5. solr环境搭建
  6. For和While在C和MATLAB中的区别——MATLAB的大坑
  7. JVM内存区域模型
  8. linux磁盘简单分区方式
  9. spring setter方法注入
  10. WebApp之PC客户端
  11. 老李分享:Web Services 特性 1
  12. Project 1 :创建链表与显示链表
  13. 工作随笔——ember框架去除url上的#号
  14. 高通平台手机开发之LCD
  15. [math][mathematica] mathematica入门
  16. Java中的Calendar日历用法详解
  17. e822. 监听JScrollPane的滚动
  18. mac下使用brew安装java等应用
  19. gulp入门与一些基本设置
  20. MongoDB DBA 实践7-----MongoDB的分片集群操

热门文章

  1. 由内省引出JavaBean的应用
  2. 【2048小游戏】——原生js爬坑之遍历算法显示二维数组内容
  3. JAVA Eclipse的Android的进程和生命周期是什么
  4. Win7如何解决精简版的迅雷7无法运行
  5. C++里面定时器的使用
  6. 【Android实战】Gallary+ImageSwicther图片查看器
  7. Synchronized修饰静态变量和普通变量的区别
  8. org hibernate querytimeoutexception
  9. java 表示当前时间的第二天的几点
  10. servletResponse 文件下载