需求:求多门课程的平均值。

样板:math.txt

zhangsan 90

lisi 88

wanghua 80

china.txt

zhangsan 80
lisi 90
wanghua 88

输出:zhangsan 85

lisi 89

wanghua 84

分析部分:

mapper部分分析:

1、<k1,v1>k1代表:一行数据的编号位置,v1代表:一行数据。

2、<k2,v2>k2代表:名字,v2代表:分数。

reduce部分分析:

3、<k3,v3>k3代表:相同key(名字),v3代表:list<int>。

4、统计输出<k4,v4>k4代表:名字,v4代表:平均值。

程序部分:

AverageMapper类:

package com.cn.average;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class AverageMapper extends Mapper<Object, Text, Text, IntWritable> {
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String [] strings = new String[2];
int i = 0;
String line = value.toString();
StringTokenizer tokenizerVal = new StringTokenizer(line);
while (tokenizerVal.hasMoreElements()) {
strings[i] = tokenizerVal.nextToken();
i++;
}
context.write(new Text(strings[0]), new IntWritable(Integer.parseInt(strings[1])));
}
}

AverageReduce类:

package com.cn.average;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class AverageReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
int sum = 0;
int i = 0;
for(IntWritable value : values){
sum += value.get();
i++;
}
context.write(key, new IntWritable(sum/i));
}
}

DataAverage类:

package com.cn.average;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; /**
* 平均值
* @author root
*
*/
public class DataAverage {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: DataAverage ");
System.exit(2);
}
//创建一个job
Job job = new Job(conf, "Data Average");
job.setJarByClass(DataAverage.class); //设置文件的输入输出路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //设置mapper和reduce处理类
job.setMapperClass(AverageMapper.class);
job.setReducerClass(AverageReduce.class); //设置输出key-value数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //提交作业并等待它完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

每天总结一点点,总有不一样的收获。

最新文章

  1. 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool
  2. rename 快速移动文件或者文件夹
  3. RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
  4. SQL 操作结果集 -并集、差集、交集、结果集排序
  5. 新建虚拟目录使用UNC共享文件夹(即:虚拟目录使用UNC共享文件夹)的方法 -摘自网络
  6. 修改Tomcat内存大小
  7. angularJs工作日记-自定义指令Directive01
  8. 随时可以给doT模板传任何你想要的值
  9. VB 增强的部件与引用
  10. Shell continue循环
  11. Java程序栈信息文件中的秘密(五)
  12. R语言做文本挖掘 Part5情感分析
  13. QT Creater与libusb使用
  14. 第十三节 Ajax基础
  15. tigervnc-server安装使用
  16. WIN32控件使用系统样式
  17. 将目录结构输出为json格式(zTree)
  18. 3192: [JLOI2013]删除物品
  19. pthread到Win32thread
  20. java 安全 技术

热门文章

  1. Tomcat的URL中文乱码解决以及传输优化
  2. [转]Windows Server 2008 对 CPU 及 RAM 的支持规格
  3. 未在本地计算机上注册&quot;Microsoft.Jet.OLEDB.4.0&quot;
  4. laravel 连接mongodb
  5. 【C#/WPF】图片的切割/切图/裁剪图片
  6. http://blog.csdn.net/ouyang_peng/article/details/8732644
  7. beeline执行hql过程中出现错误,权限不足
  8. 完美解决方案:wordpress后台进不去,用户名、密码输入了登陆没有反应(有更新)
  9. ggplot2——简介
  10. Windows通用知识讲解一