hadoop程序MapReduce之MaxTemperature
需求:求每年当中最高的温度
样本:temp.log
2016080623
2016072330
2015030420
输出结果:2016 30
2015 20
MapReduce分析设计:
Mapper分析设计:
1、将文件分割成键值队<k1,v1>,k1代表:行位置,v1代表:一行数据。
2、将这行数据进行分割成<k2,v2>,k2代表:年份,v1代表:温度。
Reduce分析设计:
3、将一些列合并后的相同key的一系列温度<k3,v3>,k3代表:年份,v1代表:list<int>多个温度。
4、统计比较最大温度<k4,v4>,k4代表:年份,v4代表:最大的温度。
程序部分:
TempMapper类:
package com.cn.temperature; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class TempMapper extends Mapper<Object, Text, Text, IntWritable>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String lineValue = value.toString();
String year = lineValue.substring(0, 4);
int temperature = Integer.parseInt(lineValue.substring(8));
context.write(new Text(year), new IntWritable(temperature));
}
}
TempReduce部分:
package com.cn.temperature; import java.io.IOException; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class TempReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int maxTemp = Integer.MIN_VALUE;
for(IntWritable value : values){
maxTemp = Math.max(maxTemp, value.get());
}
context.write(key, new IntWritable(maxTemp));
}
}
MaxTemperature部分:
package com.cn.temperature; 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.GenericOptionsParser; public class MaxTemperature {
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: wordcount ");
System.exit(2);
}
Job job = new Job(conf, "max tempperature"); //运行的jar
job.setJarByClass(MaxTemperature.class); //job执行作业时输入和输出文件的路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); //指定自定义的Mapper和Reducer作为两个阶段的任务处理类
job.setMapperClass(TempMapper.class);
job.setReducerClass(TempReduce.class); //设置最后输出结果的Key和Value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //提交作业并等待它完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
记录自己成长的过程。我觉得这点很重要。
hadoop程序MapReduce之MaxTemperature的更多相关文章
- hadoop程序MapReduce之SingletonTableJoin
需求:单表关联问题.从文件中孩子和父母的关系挖掘出孙子和爷奶关系 样板:child-parent.txt xiaoming daxiong daxiong alice daxiong jack 输出: ...
- hadoop程序MapReduce之average
需求:求多门课程的平均值. 样板:math.txt zhangsan 90 lisi 88 wanghua 80 china.txt zhangsan 80lisi 90wanghua 88 输出:z ...
- hadoop程序MapReduce之DataSort
需求:对文件中的数据进行排序. 样本:sort.log 10 13 10 20 输出:1 10 2 10 3 13 4 20 分析部分: mapper分析: 1.<k1,v1>k1代表:行 ...
- 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 ...
- hadoop程序MapReduce之WordCount
需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...
- 用PHP编写Hadoop的MapReduce程序
用PHP编写Hadoop的MapReduce程序 Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...
- Hadoop之MapReduce程序应用三
摘要:MapReduce程序进行数据去重. 关键词:MapReduce 数据去重 数据源:人工构造日志数据集log-file1.txt和log-file2.txt. log-file1.txt内容 ...
- 如何在Windows下面运行hadoop的MapReduce程序
在Windows下面运行hadoop的MapReduce程序的方法: 1.下载hadoop的安装包,这里使用的是"hadoop-2.6.4.tar.gz": 2.将安装包直接解压到 ...
- Hadoop之Mapreduce 程序
package com.gylhaut.hadoop.senior.mapreduce; import java.io.IOException; import java.util.StringToke ...
随机推荐
- Mysql取分组中前N条记录
表结构如下:CREATE TABLE `dwb_rmirror_req_d` ( `thedate` varchar(10) NOT NULL DEFAULT '', `node` varchar(1 ...
- poj 50道dp题
1.poj 3267 题意:给你一个字符串,下面有若干单词,问字符串要变成由下面单词组成的字符串,至少要删除多少个字母...... 例如: 6 10 browndcodw cow milk whit ...
- C#处理文本文件TXT实例详解(转)
作者:安静平和 字体:[增加 减小] 类型:转载 时间:2015-02-02我要评论 这篇文章主要介绍了C#处理文本文件TXT的方法,以实例形式详细分析了txt文本文件的读取.修改及打印等功能的实现技 ...
- nio入门教程
1.通过拿NIO和传统IO做对比来了解NIO 面向流与面向缓冲 Java NIO和IO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的. Java IO面向流意味着每次从流中读一个或多个字 ...
- centos 7 上配置mysql 开机启动详解
之前多次在centos7环境下配置mysql开机自启动出现了错误.现留下篇文章已做记录 一.centos7与centos6相比有什么不同: 1 在centos7中服务不在是用service这个命令来启 ...
- C语言 · 核桃的数量
历届试题 核桃的数量 时间限制:1.0s 内存限制:256.0MB 锦囊1 最小公倍数. 锦囊2 答案是a, b, c的最小公倍数. 问题描述 小张是软件项目经理,他带领3个 ...
- EFM32 DMA/PRS例程
/**************************************************************************//** * @file * @brief H ...
- Android APP 分享图片文字到微信刚開始正常,后面就不弹出分享框了
依照官方的流程走了一遍.一切顺利,分享成功了.本来以为能够大功告成了,结果睡了一觉,第二天要给客户演示了.才发现TMD坑爹了,不能分享了,第三方的分享栏弹不出来了.我一阵惊慌,还好非常快找到了解决的方 ...
- 【转】C# Async/Await 异步编程中的最佳做法
Async/Await 异步编程中的最佳做法 Stephen Cleary 近日来,涌现了许多关于 Microsoft .NET Framework 4.5 中新增了对 async 和 await 支 ...
- e558. 在Applet中多图片交互显示
This is the simplest applet to animate an array of images. In practice, you should use double-buffer ...