MapReduce -- 统计天气信息
示例
数据:
-- :: 34c
-- :: 36c
-- :: 32c
-- :: 37c
-- :: 23c
-- :: 45c
-- :: 50c
-- :: 33c
-- :: 41c
-- :: 27c
-- :: 45c
-- :: 46c
-- :: 47c
要求:
将每年每月中的气温排名前三的数据找出来
实现:
1.每一年用一个reduce任务处理;
2.创建自定义数据类型,存储 [年-月-日-温度];
2.自己实现排序函数 根据 [年-月-温度] 降序排列,也可以在定义数据类型中进行排序;
3.自己实现分组函数,对 [年-月] 分组,reduce的key是分组结果,根据相同的分组值,统计reduce的value值,只统计三个值就可以,因为已经实现了自排序函数。
注意点:
1.自定义数据类型的使用;
2.自定义排序类的使用;
3.自定义分组类的使用,分组类对那些数据进行分组;
4.自定义分区类,分区类与reduce job个数的关系;
示例代码:
RunJob.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* weather 统计天气信息
*
* 数据:
* 1999-10-01 14:21:02 34c
* 1999-11-02 13:01:02 30c
*
* 要求:
* 将每年的每月中气温排名前三的数据找出来
*
* 实现:
* 1.每一年用一个reduce任务处理;
* 2.创建自定义数据类型,存储 [年-月-日-温度];
* 2.自己实现排序函数 根据 [年-月-温度] 降序排列,也可以在定义数据类型中进行排序;
* 3.自己实现分组函数,对 [年-月] 分组,reduce的key是分组结果,根据相同的分组值,统计reduce的value值,只统计三个值就可以,因为已经实现了自排序函数。
*
* Created by Edward on 2016/7/11.
*/
public class RunJob { public static void main(String[] args)
{
//access hdfs's user
System.setProperty("HADOOP_USER_NAME","root"); Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://node1:8020"); try {
FileSystem fs = FileSystem.get(conf); Job job = Job.getInstance(conf);
job.setJarByClass(RunJob.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class); //需要指定 map out 的 key 和 value
job.setOutputKeyClass(InfoWritable.class);
job.setOutputValueClass(Text.class); //设置分区 继承 HashPartitioner
job.setPartitionerClass(YearPartition.class);
//根据年份创建指定数量的reduce task
job.setNumReduceTasks(3); //设置排序 继承 WritableComparator
//job.setSortComparatorClass(SortComparator.class); //设置分组 继承 WritableComparator 对reduce中的key进行分组
job.setGroupingComparatorClass(GroupComparator.class); FileInputFormat.addInputPath(job, new Path("/test/weather/input")); Path path = new Path("/test/weather/output");
if(fs.exists(path))//如果目录存在,则删除目录
{
fs.delete(path,true);
}
FileOutputFormat.setOutputPath(job, path); boolean b = job.waitForCompletion(true);
if(b)
{
System.out.println("OK");
} } catch (Exception e) {
e.printStackTrace();
}
} public static class MyMapper extends Mapper<LongWritable, Text, InfoWritable, Text > { private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] str = value.toString().split("\t"); try {
Date date = sdf.parse(str[0]);
Calendar c = Calendar.getInstance();
c.setTime(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH); double temperature = Double.parseDouble(str[1].substring(0,str[1].length()-1)); InfoWritable info = new InfoWritable();
info.setYear(year);
info.setMonth(month);
info.setDay(day);
info.setTemperature(temperature); context.write(info, value); } catch (ParseException e) {
e.printStackTrace();
}
}
} public static class MyReducer extends Reducer<InfoWritable, Text, Text, NullWritable> {
@Override
protected void reduce(InfoWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int i=0;
for(Text t: values)
{
i++;
if(i>3)
break;
context.write(t, NullWritable.get());
}
}
}
}
InfoWritable.java
import org.apache.hadoop.io.WritableComparable; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; /**
* 自定义数据类型 继承 WritableComparable
* 【年-月-日-温度】
* Created by Edward on 2016/7/11.
*/
public class InfoWritable implements WritableComparable<InfoWritable> { private int year;
private int month;
private int day;
private double temperature; public void setYear(int year) {
this.year = year;
} public void setMonth(int month) {
this.month = month;
} public void setDay(int day) {
this.day = day;
} public void setTemperature(double temperature) {
this.temperature = temperature;
} public int getYear() {
return year;
} public int getMonth() {
return month;
} public int getDay() {
return day;
} public double getTemperature() {
return temperature;
} /**
*
* 对象比较,对温度进行倒序排序
*/
@Override
public int compareTo(InfoWritable o) { int result = Integer.compare(this.getYear(),o.getYear());
if(result == 0)
{
result = Integer.compare(this.getMonth(),o.getMonth());
if(result == 0)
{
return -Double.compare(this.getTemperature(), o.getTemperature());
}
else
return result;
}
else
return result; //return this==o?0:-1;
} @Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeInt(this.year);
dataOutput.writeInt(this.month);
dataOutput.writeInt(this.day);
dataOutput.writeDouble(this.temperature);
} @Override
public void readFields(DataInput dataInput) throws IOException {
this.year = dataInput.readInt();
this.month = dataInput.readInt();
this.day = dataInput.readInt();
this.temperature = dataInput.readDouble();
}
}
YearPartition.java
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner; /**
*
* 创建分区,通过key中的year来创建分区
*
* Created by Edward on 2016/7/11.
*/
public class YearPartition extends HashPartitioner <InfoWritable, Text>{
@Override
public int getPartition(InfoWritable key, Text value, int numReduceTasks) {
return key.getYear()%numReduceTasks;
}
}
GroupComparator.java
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 创建分组类,继承WritableComparator
* 【年-月】
* Created by Edward on 2016/7/11.
*/
public class GroupComparator extends WritableComparator { GroupComparator()
{
super(InfoWritable.class, true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
InfoWritable ia = (InfoWritable)a;
InfoWritable ib = (InfoWritable)b; int result = Integer.compare(ia.getYear(),ib.getYear());
if(result == 0)
{
return Integer.compare(ia.getMonth(),ib.getMonth());
}
else
return result;
}
}
SortComparator.java
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; /**
* 排序类,继承WritableComparator
* 排序规则【年-月-温度】 温度降序
* Created by Edward on 2016/7/11.
*/
public class SortComparator extends WritableComparator { /**
* 调用父类的构造函数
*/
SortComparator()
{
super(InfoWritable.class, true);
} /**
* 比较两个对象的大小,使用降序排列
* @param a
* @param b
* @return
*/
@Override
public int compare(WritableComparable a, WritableComparable b) { InfoWritable ia = (InfoWritable)a;
InfoWritable ib = (InfoWritable)b; int result = Integer.compare(ia.getYear(),ib.getYear());
if(result == 0)
{
result = Integer.compare(ia.getMonth(),ib.getMonth());
if(result == 0)
{
return -Double.compare(ia.getTemperature(), ib.getTemperature());
}
else
return result;
}
else
return result;
}
}
MapReduce -- 统计天气信息的更多相关文章
- 半吊子学习Swift--天气预报程序-获取天气信息
昨天申请的彩云天气Api开发者今天上午已审核通过  饭后运动过后就马不停蹄的来测试接口,接口是采用经纬度的方式来获取天气信息,接口地址如下 https://api.caiyunapp.com/v2/ ...
- 内网公告牌获取天气信息解决方案(C# WebForm)
需求:内网公告牌能够正确显示未来三天的天气信息 本文关键字:C#/WebForm/Web定时任务/Ajax跨域 规划: 1.天定时读取百度接口获取天气信息并存储至Txt文档: 2.示牌开启时请求Web ...
- 使用小米天气API获取天气信息
1. URL部分 以下url中"%s"代表的是城市Id,比如北京的cityId=101010100: //获取未来五天预报信息,红色部分信息不需要 WEATHER_DATA_URL ...
- C#调用WebService获取天气信息
概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...
- ASP获取json天气信息
ASP代码(ASP获取页面源码方法,有编码.超时时间参数,处理了乱码.超时的问题): Function GetHttpPage(HttpUrl) Then GetHttpPage="$Fal ...
- java获取天气信息
通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包: json-lib-2.4.jar ezmorph-1.0.6.jar commons-beanutils-1.8.3 ...
- 利用json获取天气信息
天气预报信息获取是利用json获取的,网上有非常多资源,源码.因为上面涉及到非常多天气信息,包含湿度,出行建议等,以及加入了全部城市代码的资源包.为了练手了解json的原理.我仅获取诚笃城市的最高温, ...
- PHP Ajax JavaScript Json 实现天气信息获取
使用第三方服务 间接方式 思路 使用到的服务 实现代码 前端完整代码 总结 要在自己的网站上添加一个天气预报功能,是一个很普通的需求,实现起来也不是很难.今天来介绍几个简单的方法. 使用第三方服务 有 ...
- MongoDb 用 mapreduce 统计留存率
MongoDb 用 mapreduce 统计留存率(金庆的专栏)留存的定义采用的是新增账号第X日:某日新增的账号中,在新增日后第X日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...
随机推荐
- 移动端地区选择mobile-select-area插件的使用方法
顾名思义,mobile-select-area插件就是使用在移动端上的进行地区选择的插件,而且使用方法简单,我就说我是怎么用的吧 一.准备工作 首先肯定要下载插件对应的css+js文件, 当你下载好这 ...
- github上手实践教程
简介: SSH公私钥的使用 github的使用 git 工具的基本使用 基本步骤: 一.github的使用 1.github账号的创建[官网一步一步创建就行了,这一步骤省略] 2.创建远程仓库: 创建 ...
- WPF ListView 使用GridView 带有Header 以及点击header排序 sort
ListView: <ListView x:Name="lvFiles" VerticalAlignment="Stretch" Background=& ...
- Difference between ReLU、LReLU、PReLU、CReLU、ELU、SELU
激活函数 ReLU.LReLU.PReLU.CReLU.ELU.SELU 的定义和区别 ReLU tensorflow中:tf.nn.relu(features, name=None) LReLU ...
- linux 根据进程名杀死进程 -kill进程名
前两天一个老师给我出了一个linux操作上的问题,现在知道进程名怎样杀死这个进程.或许很多人都会和我一样说用 #pkill 进程名 或是 #killall 进程名 的确这个两个命令都能做到这些,而且我 ...
- Android 对话框 (AlertDialog)
Android 提供了 AlertDialog 类可通过其内部类 Builder 轻松创建对话框窗口,但是没法对这个对话框窗口进行定制,为了修改 AlertDialog 窗口显示的外观,解决的办法就是 ...
- Oracle EBS 解决OAF黑屏,卡顿,反应慢
- where条件使用to_char条件太慢
where条件使用to_char 会不使用索引并使用nestedloop 可以用with as解决 最后再加上to_char的条件语句
- Oracle GoldenGate OGG管理员手册(较早资料)
第一章 系统实现简述 前言 编写本手册的目的是为系统管理员以及相关操作人员提供 Oracle Goldengat 软 件的日常维护和使用的技术参考: 3 ORACLE 第二章 OGG 日常维护操作 ...
- python安装lib库
time:2015/11/11 双十一 一.初衷 看到一篇帖子[1],里面有python代码,就想实现一下,代码如下: import cv2 as cv import numpy as np from ...