需求:求每年当中最高的温度

样本: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的更多相关文章

  1. hadoop程序MapReduce之SingletonTableJoin

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

  2. hadoop程序MapReduce之average

    需求:求多门课程的平均值. 样板:math.txt zhangsan 90 lisi 88 wanghua 80 china.txt zhangsan 80lisi 90wanghua 88 输出:z ...

  3. hadoop程序MapReduce之DataSort

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

  4. 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 ...

  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. Windows下对postgre开启远程连接权限

    编辑 删除 前言:Windows下对postgre开启远程连接权限,下面是实际操作过程中的手顺 1.找到postgresql.conf文件,注意安装路径 D:\Program Files (x86)\ ...

  2. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  3. mysql too many max_connections

    debian 环境 mysql  MySQL Community Server 5.6.27 首先修改 my.cnf文件  全局查找  find / -name my.cnf* [mysqld] 配置 ...

  4. 【Unity笔记】将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标

    int floorMask; // 自动寻路层 void Awake() { floorMask = LayerMask.NameToLayer("Floor"); } void ...

  5. 解析:SO_REUSEADDR bind: address in use

    http://blog.sina.com.cn/s/blog_53a2ecbf010095db.html socket中的SO_REUSEADDR Q: 我正在写一个unix server程序,不是d ...

  6. 【译】Linux概念架构的理解

    声明:本文转载,原路径地址:http://www.jianshu.com/p/c5ae8f061cfe 摘要 Linux kernel成功的两个原因:(1)灵活的架构设计使得大量的志愿开发者能够很容易 ...

  7. ​网页图表Highcharts实践教程标之加入题副标题版权信息

    ​网页图表Highcharts实践教程标之加入题副标题版权信息 Highcharts辅助元素 辅助元素图表的非必要元素.如标题.版权信息.标签.加载动态.它们不和图表数据发生关联,仅仅是额外说明一些基 ...

  8. MongoDB(六):使用C#代码连接并读取MongoDB数据库

    在上篇文章中,讲解了MongoDB的基本操作,包括增.删.改.查,但是这些操作都是在命令行模式下进行的,这篇文章中讲解如何使用C#程序连接到MongoDB数据库,并且读取里面的文档. 一.新建项目 新 ...

  9. 400错误,Required String parameter 'paramter' is not present

    1.就拿简单的登录来说吧,这是开始的代码 @RequestMapping(value="/login")public ModelAndView login(@RequestPara ...

  10. 事件总线demo

    经过几天的努力拜读大牛高手文章,终于对事件总线有所了解,特此记录下来,以免忘记 1.定义相关的接口: A  事件接口 public interface IDomainEvent { DateTime ...