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日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...
随机推荐
- react组件里阻事件冒泡
e.nativeEvent.stopImmediatePropagation();
- C# 新建文档CreateNewDocument
// Copyright 2010 ESRI// // All rights reserved under the copyright laws of the United States// and ...
- web service概念、架构及相关知识
原文:http://blog.csdn.net/liu_shi_jun/article/details/51121597 一.WebService的定义 WebService有好几种定义: W3C组织 ...
- openwrt-rpcd服务ACL配置错误风险分析
前言 openwrt 是一个用于的 路由器 的开源系统. 其他类似的路由器系统相比它的更新速度非常的快,可以看看 github 的更新速度 https://github.com/openwrt/ope ...
- windows Ctrl + Alt + 方向键 取消屏幕反转
1.在桌面右击 2.再次右击桌面 3.单击选项和支持 4.点击禁用和应用
- jdk下载及安装
下载下载 jdk 下载 java se 版本的即可. web 开发前不需要像安装 java se 一样安装java ee,只要在项目中添加 java ee 的jar 包就可以了,里面大多是接口和抽象类 ...
- 回归JavaScript基础(七)
主题:引用类型Function的介绍. 今天首先说的就是Function类型.下面就是定义函数的两种方法,第一种使用函数声明语法定义,第二种使用函数表达式定义.这两种定义函数的方式几乎没有什么区别. ...
- npm用法及离线安装方法
npm用法及离线安装方法 基本的用法 查看某个模块的全部信息,或者可以查看单个信息 npm info name npm info name version npm info name homepage ...
- 1. 跟踪标记 (Trace Flag) 1117, 1118 文件增长及空间分配方式
跟踪标记:1117 功能: 默认,同一个文件组下的多个文件,如果某个文件没有可用空间,且设置了自动增长,则该文件自动增长,其他文件大小保持不变: 开启后,同一文件组下的多个文件,如果某个文件没有可用空 ...
- 给UIScrollView添加category实现UIScrollView的轮播效果
给UIScrollView添加category实现UIScrollView的轮播效果 大家都知道,要给category添加属性是必须通过runtime来实现的,本教程中给UIScrollView添加c ...