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日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...
随机推荐
- Asp.net MVC 移除不用的视图引擎
Asp.net MVC 默认提供两个视图引擎,分别为: WebFormViewEngine 和 RazorViewEngine.MVC在查找视图时,会按照指定的顺序进行查找.当我们的MVC程序未找到相 ...
- java获取当月天数,指定年月的天数,指定日期获取对应星期 .
package huolongluo.family.util; import java.text.SimpleDateFormat; import java.util.Calendar; import ...
- Android自定义Button按钮显示样式
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- svn 同步资源库时忽略某些文件类型和文件夹
项目开发中,开发人员经常用SVN来管理代码,在和服务器同步时,每次都看到一堆.class,.log,target等文件,这样很不舒服. 解决方法: 打开:window-->preferences ...
- The directory '/home/stone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If execu
使用sudo pip install ......的时候出现下面一段黄色的代码: The directory '/home/stone/.cache/pip/http' or its parent d ...
- 在 Azure 中的 Linux 虚拟机上使用 SSL 证书保护 Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Linux 虚 ...
- Linxu系统修改文件描述符
修改系统文件描述符 文件描述符:无符号整数(0-65535),进程使用它来标示打开的文件 /etc/security/limits.conf:可以修改CPU,堆栈, 1.查看最大的标示符 u ...
- 记Git报错-Everything up-to-date
文:铁乐与猫 今天git push 到github远程仓库的时候,出现报错"Everything up-to-date",严格来说也不算报错,它只是在告诉你,提交区所有的东西都是最 ...
- October 06th 2017 Week 40th Friday
The greatest ideal man can set before himself is self-perfection. 一个人最高的理想是自我完善. To be better than t ...
- 解决qpidd服务安装不上的问题
前几天对一个文件夹命名,忘记了qpidd的路径在这个文件夹的下面,导致后来qpidd用不了. 并且有打开计算机-管理-服务时,看到Advanced MessageQueuing Protocol 未启 ...