hadoop程序MapReduce之average
2024-09-01 08:16:20
需求:求多门课程的平均值。
样板: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);
}
}
每天总结一点点,总有不一样的收获。
最新文章
- 【转】线程及同步的性能 - 线程池 / ThreadPoolExecutors / ForkJoinPool
- rename 快速移动文件或者文件夹
- RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
- SQL 操作结果集 -并集、差集、交集、结果集排序
- 新建虚拟目录使用UNC共享文件夹(即:虚拟目录使用UNC共享文件夹)的方法 -摘自网络
- 修改Tomcat内存大小
- angularJs工作日记-自定义指令Directive01
- 随时可以给doT模板传任何你想要的值
- VB 增强的部件与引用
- Shell continue循环
- Java程序栈信息文件中的秘密(五)
- R语言做文本挖掘 Part5情感分析
- QT Creater与libusb使用
- 第十三节 Ajax基础
- tigervnc-server安装使用
- WIN32控件使用系统样式
- 将目录结构输出为json格式(zTree)
- 3192: [JLOI2013]删除物品
- pthread到Win32thread
- java 安全 技术
热门文章
- Tomcat的URL中文乱码解决以及传输优化
- [转]Windows Server 2008 对 CPU 及 RAM 的支持规格
- 未在本地计算机上注册";Microsoft.Jet.OLEDB.4.0";
- laravel 连接mongodb
- 【C#/WPF】图片的切割/切图/裁剪图片
- http://blog.csdn.net/ouyang_peng/article/details/8732644
- beeline执行hql过程中出现错误,权限不足
- 完美解决方案:wordpress后台进不去,用户名、密码输入了登陆没有反应(有更新)
- ggplot2——简介
- Windows通用知识讲解一