MapReduce——计算温度最大值 (基于全新2.2.0API)
MapReduce——计算温度最大值 (基于全新2.2.0API)
deprecated: Job类的所有Constructors, 新的API用静态方法getInstance(conf)来去的Job的实例;
Code:
import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class getMaxTemperature extends Configured implements Tool { class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> { @Override public void map(LongWritable key, Text val, Context context) throws IOException, InterruptedException { String line = val.toString(); String year = line.substring(15, 19); int Temperature; if (!hasPlus(line)){ Temperature = Integer.parseInt(line.substring(87, 92)); } else { Temperature = Integer.parseInt(line.substring(88,92)); } String qual = line.substring(92, 93); if(!matched(qual)) { context.write(new Text(year), new IntWritable(Temperature)); } } private boolean hasPlus(String line) { return line.charAt(87) == '+' ? true : false; } private boolean matched(String line) { return line.matches("[01459") ? true : false; } } class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text key, Iterable<IntWritable> vals, Context context) throws IOException, InterruptedException { int maxValue = Integer.MIN_VALUE; for( IntWritable value : vals ) { maxValue = Math.max(maxValue, value.get()); } context.write(key, new IntWritable(maxValue)); } } @Override public int run(String[] args) throws Exception { Configuration conf = getConf(); Job job = Job.getInstance(conf); job.setJobName("helloRuby"); job.setJarByClass(getClass()); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MaxTemperatureMapper.class); job.setCombinerClass(MaxTemperatureReducer.class); job.setReducerClass(MaxTemperatureReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { ToolRunner.run(new getMaxTemperature() , args); } }
MapReduce——计算温度最大值 (基于全新2.2.0API)的更多相关文章
- MapReduce计算每年最大值测试样例生成程序
Demo.java package com.java; import java.io.BufferedWriter; import java.io.File; import java.io.FileW ...
- MapReduce计算每年最大值
1. 测试文件生成程序,参考 https://www.cnblogs.com/jonban/p/10555364.html MapReduce程序示例如下: 2. 新建Maven项目 hadoop ...
- write & read a MapFile(基于全新2.2.0API)
write & read a MapFile import java.io.IOException; import org.apache.hadoop.io.IntWritable; imp ...
- write & read a sequence file(基于全新2.2.0API)
write & read a sequence file write & read a sequence file import java.io.IOException; import ...
- Configurataion Printer(基于全新2.2.0API)
Configurataion Printer import java.util.Map.Entry; import org.apache.hadoop.conf.Configuration; impo ...
- Hadoop—MapReduce计算气象温度
Hadoop-MapReduce计算气象温度 1 运行环境说明 1.1 硬软件环境 主机操作系统:Mac OS 64 bit ,8G内存 虚拟软件:Parallers Desktop12 虚拟机操作系 ...
- 使用mapreduce计算环比的实例
最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...
- MapReduce计算模型的优化
MapReduce 计算模型的优化涉及了方方面面的内容,但是主要集中在两个方面:一是计算性能方面的优化:二是I/O操作方面的优化.这其中,又包含六个方面的内容. 1.任务调度 任务调度是Hadoop中 ...
- (第4篇)hadoop之魂--mapreduce计算框架,让收集的数据产生价值
摘要: 通过前面的学习,大家已经了解了HDFS文件系统.有了数据,下一步就要分析计算这些数据,产生价值.接下来我们介绍Mapreduce计算框架,学习数据是怎样被利用的. 博主福利 给大家赠送一套ha ...
随机推荐
- jQuery—一些常见方法(3)【width(),innerWidth(),outerWidth()】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery入门[1]-构造函数【转载】
最近看了一些jquery的进阶教程,感觉很不错,与大家分享下! jQuery——构造函数 ◦体积小(v1.2.3 15kb)◦丰富的DOM选择器(CSS1-3 + XPath) ◦跨浏览器(IE6,F ...
- 浏览器检测(BrowserDetect.js)
浏览器检测是在工作中经常用到的,如果只是简单判断当前是什么浏览器的话可以通过window.navigator.useragent这样的js来直接判断就可以了! 但是针对浏览器版本要求比较高的时候,如果 ...
- spring data mongodb中,如果对象中的属性不想加入到数据库字段中
spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...
- .Net 平台下的互联网架构新思考
上一篇<互联网应用架构谈>有很多阅读量,但后来实践中发现基于WCF服务层技术用于实现网站访问的效率是有问题的. 我的本意是以WCF中间层解决商业逻辑,供各个平台调用,然而还是带来诸多的麻烦 ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- __sync_fetch_and_add
最近在公司离职的前辈写的代码哪里看到了__sync_fetch_and_add这个东东.比较好奇.找些资料学习学习 http://www.lxway.com/4091061956.htm http:/ ...
- 【JSTL EL】 jsp 页面学习
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1.2 ...
- C# - 中断模式下的调试
1. 设置断点 选中需要设置断点的行,右键选择断点插入断点,此行左侧显示红色圆形标志.或者F9 有几个条件断点类型: a. 条件断点 b. 命中次数,大于,几倍于,大于等于你设置的断点次数此时中断 c ...
- tableView -- tips
1. 如果发现TableView的第一个sectionHeader不显示, 那么可以断定, 你没有用代理方法来设置 sectionHeader的高度! #pragma mark - delegate ...