file1.txt

a 1
b 2
a 3
b 3
a 5
b 7
c 3
c 5

file2.txt

a 1
b 7
c 5
a 1
c 3

结果:

a 2.2
b 4.75
c 4.0

代码:

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
//import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
//import org.apache.hadoop.util.GenericOptionsParser; public class Average { public static class AverageMaper extends Mapper<Object,Text,Text,Text>
{
//private final static IntWritable one=new IntWritable(1);
private static Text word=new Text();
public void map(Object key,Text value,Context context) throws
IOException,InterruptedException
{
StringTokenizer itr=new StringTokenizer(value.toString());
while(itr.hasMoreTokens())
{
word.set(itr.nextToken());
if(itr.hasMoreTokens())
context.write(word, new Text(itr.nextToken()+",1"));
}
}
}
public static class AveragerCombine extends Reducer<Text,Text,Text,Text>
{
public void reduce(Text key,Iterable<Text> values,Context context) throws
IOException,InterruptedException
{
int sum=0;
int cnt=0;
for(Text val:values)
{
String []str=val.toString().split(",");
sum+=Integer.parseInt(str[0]);
cnt+=Integer.parseInt(str[1]);
}
context.write(key,new Text(sum+","+cnt));
}
}
public static class AveragerReduce extends Reducer<Text,Text,Text,DoubleWritable>
{
public void reduce(Text key,Iterable<Text> values,Context context) throws
IOException,InterruptedException
{
int sum=0;
int cnt=0;
for(Text val:values)
{
String []str=val.toString().split(",");
sum+=Integer.parseInt(str[0]);
cnt+=Integer.parseInt(str[1]);
}
double res=(sum*1.0)/cnt;
context.write(key, new DoubleWritable(res));
}
} public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Configuration conf=new Configuration();
String []myargs=args;
if(myargs.length!=2)
{
System.err.println("please input two args<in> <out>\n");
System.exit(2);
}
Job job=new Job(conf,"average job");
job.setJarByClass(Average.class);
job.setMapperClass(AverageMaper.class);
job.setCombinerClass(AveragerCombine.class);
job.setReducerClass(AveragerReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(myargs[0]));
FileOutputFormat.setOutputPath(job,new Path(myargs[1]));
System.exit(job.waitForCompletion(true)?0:1);
}
}

Hadoop计算平均值【转】的更多相关文章

  1. matlab 利用while循环计算平均值和方差

    一.该程序是用来测输入数据的平均值和方差的 公式: 二. 项目流程: 1. State the problem假定所有测量数为正数或者0,计算这一系列测量数的平均值和方差.假定我们预先不知道有多少测量 ...

  2. [BigData - Hadoop - YARN] YARN:下一代 Hadoop 计算平台

    Apache Hadoop 是最流行的大数据处理工具之一.它多年来被许多公司成功部署在生产中.尽管 Hadoop 被视为可靠的.可扩展的.富有成本效益的解决方案,但大型开发人员社区仍在不断改进它.最终 ...

  3. PTA计算平均值(一波三折)

    PTA计算平均值( 一波三折) 现在为若干组整数分别计算平均值. 已知这些整数的绝对值都小于100,每组整数的数量不少于1个,不大于20个. 输入格式:首先输入K(不小于2,不大于20).接下来每一行 ...

  4. Hadoop计算中的Shuffle过程(转)

    Hadoop计算中的Shuffle过程 作者:左坚 来源:清华万博 时间:2013-07-02 15:04:44.0 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解Ma ...

  5. 为什么在 Java 中用 (low+high)>>>1 代替 (low+high)/2 或 (low+high)>>1 来计算平均值呢?好在哪里?

    >>>与>>是位运算符,只对整型有效(不能用于浮点型).当是整型的时候(low+high)>>1可以代替(low+high)/2.>>>是无 ...

  6. C 语言实例 - 计算平均值

    C 语言实例 - 计算平均值 C 语言实例 C 语言实例 使用数组来计算几个数的平均值. 实例 #include <stdio.h> int main() { int n, i; ], s ...

  7. PTA 计算平均值

    现在为若干组整数分别计算平均值. 已知这些整数的绝对值都小于100,每组整数的数量不少于1个,不大于20个. 输入格式:首先输入K(不小于2,不大于20).接下来每一行输入一组数据(至少有一组数据), ...

  8. hadoop streaming anaconda python 计算平均值

    原始Liunx 的python版本不带numpy ,安装了anaconda 之后,使用hadoop streaming 时无法调用anaconda python  , 后来发现是参数没设置好... 进 ...

  9. Spark与Hadoop计算模型的比较分析

    http://tech.it168.com/a2012/0401/1333/000001333287.shtml 最近很多人都在讨论Spark这个貌似通用的分布式计算模型,国内很多机器学习相关工作者都 ...

随机推荐

  1. Ubuntu上CUDA环境搭建

    1.下载CUDA:https://developer.nvidia.com/cuda-toolkit-archive (如果已经安装了N卡驱动,最好用.deb,如果没有安装,可以用.run) 2.根据 ...

  2. Tomcat 的 DefaultServlet

    问题描述: 群里有人测试 Spring MVC,没有配置任何Controller,只配置了一个view resolver,指定了前缀后缀. 然后,他问的是 当访问 localhost:8080/tes ...

  3. e614. Setting the Initial Focused Component in a Window

    There is no straightforward way to set the initial focused component in a window. The typical method ...

  4. C# 中 List.Sort运用(IComparer<T>)排序用法

    /// <summary> /// 比较人物类实例大小,实现接口IComparer /// </summary> public class InternetProtocolCo ...

  5. Linux 用户与用户组管理

    Linux 是一个多用户多任务的分是操作系统,用户是实现操作系统资源分配,同时也是安全权限模型的核 心要素之一:用户组是一个容纳很多用户的容器,可以分配权限组,进一步优化了权限分配. 一.用户管理 用 ...

  6. MySQL查看某库表大小及锁表情况

    查询所有数据库占用磁盘空间大小的SQL语句: 语句如下: select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB ...

  7. Linux 下 Nginx + JDK + Tomcat + MySQL 安装指南

    转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52026342 Nginx 是一款高性能的 http 服务器/反向代理服务器/电子邮 ...

  8. linux 上安裝lnmp

    1.確保有一台服務器可以正常運行 2.熟練知道一些基本的命令 3.這裡我以lnmp集成環境為例 https://lnmp.org/install.html 4.安裝大約30分鐘左右 5.安裝完畢,訪問 ...

  9. nginx-windows版

    nginx  windows版,添加 分别是: 重启.启动.停止 下载地址:https://files.cnblogs.com/files/007sx/nginx-windows.zip

  10. Getting Started with Google Guava.pdf