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

样板: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);
}
}

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

hadoop程序MapReduce之average的更多相关文章

  1. hadoop程序MapReduce之SingletonTableJoin

    需求:单表关联问题.从文件中孩子和父母的关系挖掘出孙子和爷奶关系 样板:child-parent.txt xiaoming daxiong daxiong alice daxiong jack 输出: ...

  2. hadoop程序MapReduce之DataSort

    需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...

  3. hadoop程序MapReduce之DataDeduplication

    需求:去掉文件中重复的数据. 样板:data.log 2016-3-1 a 2016-3-2 b 2016-3-2 c         2016-3-2 b 输出结果: 2016-3-1 a 2016 ...

  4. hadoop程序MapReduce之MaxTemperature

    需求:求每年当中最高的温度 样本:temp.log 2016080623 2016072330 2015030420 输出结果:2016 30 2015 20 MapReduce分析设计: Mappe ...

  5. hadoop程序MapReduce之WordCount

    需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...

  6. 用PHP编写Hadoop的MapReduce程序

    用PHP编写Hadoop的MapReduce程序     Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...

  7. Hadoop之MapReduce程序应用三

    摘要:MapReduce程序进行数据去重. 关键词:MapReduce   数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...

  8. 如何在Windows下面运行hadoop的MapReduce程序

    在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...

  9. Hadoop之Mapreduce 程序

    package com.gylhaut.hadoop.senior.mapreduce; import java.io.IOException; import java.util.StringToke ...

随机推荐

  1. IOS 使用XIB 自定义View

    一般自定义View       代码方式 有 在初始化的时候添加 子Views - (id)initWithFrame:(CGRect)frame { self = [super initWithFr ...

  2. 如何在 Java 中正确使用 wait, notify 和 notifyAll?

    简介     wait,notify,notifyAll,都是属于object对象提供的方法,但在实际工作中怎么使用这几个方法,确是很多程序员清楚,不够明白,在群里问,有人说,哪个线程想wait,就用 ...

  3. java资料——线程(转)

    线程       线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...

  4. hive输出json字符串

    目前没发现有什么方便的函数可以直接使用,只能使用concat来手工拼接. 注意将null的字段值转为空,使用nvl函数 如果将hql语句写在script.q文件里面如下: select concat( ...

  5. 设置cookies第二天0点过期

    网上这个资料很少,试了csdn其中一个人的方法去算时间,返回的是Invalid Date,不知道是不是不同IE版本 返回结果不同 后面找到另外一个很简答粗暴的方法 直接加一天后,设置时分秒为0,代码如 ...

  6. 用户数据验证的正确姿势之assert

    用户数据验证灰常重要, 不用多说了, 但是实现方法(准确的说是表现形式)有很多人, 如何优雅的完成一个后端验证过滤器是一个值得考量的问题, 我尝试过许多方法, 比如validator.js模块, ex ...

  7. 透明遮罩图层VS高斯模糊滤镜 效果分析

    前端流行布局中最常见的弹出图层有popup, 对话框, tooltip等, 他们都使用了新的图层,但是实现办法各不相同, 有 的是通过半通明的黑白图层实现的, 有的是通过滤镜实现的, 我们来研究一下两 ...

  8. Juint测试

    添加"Juint测试"组件: 之后不用写在main方法里面便可以直接测试:

  9. kettle启动时候报a fatal exception has occurred

    本人刚接触ETL工具 Data Integration - Kettle ,下载了kettle6.0版本,但是在window +jdk1.7(32位)下启动报错, 使用SpoonDebug.bat写的 ...

  10. Mysql各种存储引擎的特性以及如何选择存储引擎

    几个常用存储引擎的特点 下面我们重点介绍几种常用的存储引擎并对比各个存储引擎之间的区别和推荐使用方式. 特点 Myisam BDB Memory InnoDB Archive 存储限制 没有 没有 有 ...