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

样板: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. python 字符和数值转换

    # python 字符和数值转换 ### 字符转数值------------------------------ ord('A') ==> 65- ord('B') ==> 66- ord ...

  2. 【Android】使用Pull生成/解析XML文件

    一.生成XML文件,即是将对象集合转为XML文件存储. 对象集合 –> XML(序列化) Android中使用android.util.Xml类对其进行了描述,提供相应的API. 步骤大致如下: ...

  3. linux性能评估与分析工具

    linux是一个开源系统,其内核负责管理系统的进程,内存,设备驱动程序,文件和网络系统, 决定着系统的性能和稳定性.由于内核源码很容易获取,任何人都可以将自己认为优秀的代码 加入到其中.linux默认 ...

  4. DataGridView使用技巧九:DataGridView的右键菜单(ContextMenuStrip)

    DataGridView,DataGridViewColumn,DataGridViewRow,DataGridViewCell有ContextMenuStrip属性.可以通过设置ContextMen ...

  5. rails rake和示例

    一篇看到的讲解得不错的文章 http://blog.csdn.net/clskkk2222/article/details/6735365 这里还有一些例子: Rake Documentation R ...

  6. 虚拟IP和IP漂移

    学习一下虚拟IP和IP漂移的概念. 1.虚拟IP 在 TCP/IP 的架构下,所有想上网的电脑,不论是用何种方式连上网路,都必须要有一个唯一的 IP-address.事实上IP地址是主机硬件地址的一种 ...

  7. Jquery判断某字符串中是否包含某个字符

    if(!(to_city_value.indexOf("(")>0){ //code..... }

  8. 【F12】网络面板

    使用网络面板了解请求和下载的资源文件并优化网页加载性能 (1)网络面板基础 测量资源加载时间 使用 Network 面板测量您的网站网络性能. Network 面板记录页面上每个网络操作的相关信息,包 ...

  9. Checked Exception & Unchecked Exception

    查Spring事务管理时看到一句话: Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是chec ...

  10. (转) 从ffmpeg中提取出YUV数据

    有时需要从ffmpeg中提取出YUV数据用作预览,另存什么的. ffmpeg是先解码成YUV, 再以这个YUV作为输入进行编码,所以YUV数据有两种:  解码后的YUV数据, 以及  编码重建的YUV ...