mr统计每年中每月温度的前三名
weatherMapper
package com.laoxiao.mr.weather; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class WeatherMapper extends Mapper<Text, Text, MyKey, DoubleWritable>{ SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
protected void map(Text key, Text value, Context context)
throws java.io.IOException ,InterruptedException {
try {
Date d = df.parse(key.toString());
Calendar c=Calendar.getInstance();
c.setTime(d);
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
double hot =Double.parseDouble(value.toString().substring(0, value.toString().lastIndexOf("c")));
context.write(new MyKey(year,month+1,hot), new DoubleWritable(hot));
} catch (Exception e) {
e.printStackTrace();
} }; }
weatherReducer
package com.laoxiao.mr.weather; import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class WeatherReducer extends Reducer<MyKey, DoubleWritable, Text, NullWritable>{
protected void reduce(MyKey arg0, java.lang.Iterable<DoubleWritable> arg1, Context arg2)
throws java.io.IOException ,InterruptedException {
int i=0;
for(DoubleWritable d:arg1){
i++;
arg2.write(new Text(arg0.getYear()+"\t"+arg0.getMonth()+"\t"+d.get()),NullWritable.get());
if(i==3){
break;
}
}
}; }
MyKey
package com.laoxiao.mr.weather; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class MyKey implements WritableComparable<MyKey>{ private int year;
private int month;
private double hot;
public MyKey(int year, int month, double hot) {
super();
this.year = year;
this.month = month;
this.hot = hot;
}
public MyKey() {
// TODO Auto-generated constructor stub
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getHot() {
return hot;
}
public void setHot(double hot) {
this.hot = hot;
} public void readFields(DataInput arg0) throws IOException {
this.year=arg0.readInt();
this.month=arg0.readInt();
this.hot=arg0.readDouble();
}
public void write(DataOutput arg0) throws IOException {
arg0.writeInt(year);
arg0.writeInt(month);
arg0.writeDouble(hot);
} //判断对象是否是同一个对象,当该对象作为输出的key
public int compareTo(MyKey o) {
int r1 =Integer.compare(this.year, o.getYear());
if(r1==0){
int r2 =Integer.compare(this.month, o.getMonth());
if(r2==0){
return Double.compare(this.hot, o.getHot());
}else{
return r2;
}
}else{
return r1;
}
} }
MyPartitioner
package com.laoxiao.mr.weather; import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner; public class MyPartitioner extends HashPartitioner<MyKey, DoubleWritable>{ //执行时间越短越好
public int getPartition(MyKey key, DoubleWritable value, int numReduceTasks) {
return (key.getYear()-1949)%numReduceTasks;
} }
MySort
package com.laoxiao.mr.weather; import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.io.WritableComparable; public class MySort extends WritableComparator{ public MySort() {
super(MyKey.class,true);
} public int compare(WritableComparable a, WritableComparable b) {
MyKey k1=(MyKey)a;
MyKey k2=(MyKey)b;
int r1=Integer.compare(k1.getYear(), k2.getYear());
if(r1==0){
int r2=Integer.compare(k1.getMonth(), k2.getMonth());
if(r2==0){
return -Double.compare(k1.getHot(),k2.getHot());
}else{
return r2;
}
}else{
return r1;
} }
}
MyGroup
package com.laoxiao.mr.weather; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class MyGroup extends WritableComparator{ public MyGroup(){
super(MyKey.class,true);
} public int compare(WritableComparable a, WritableComparable b) {
MyKey k1 =(MyKey) a;
MyKey k2 =(MyKey) b;
int r1 =Integer.compare(k1.getYear(), k2.getYear());
if(r1==0){
return Integer.compare(k1.getMonth(), k2.getMonth());
}else{
return r1;
} }
}
设置了三个reducer进程,最后的结果就放到了三个文件中。
mr统计每年中每月温度的前三名的更多相关文章
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
- SQL语句统计每天、每月、每年的 数据
SQL语句统计每天.每月.每年的数据 1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月 ...
- 【转】SQL语句统计每天、每月、每年的数据
原文:https://www.cnblogs.com/Fooo/p/3435687.html SQL语句统计每天.每月.每年的数据 1.每年select year(ordertime) 年,sum(T ...
- JAVA实验--统计文章中单词的个数并排序
分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...
- php实现 统计输入中各种字符的个数
php实现 统计输入中各种字符的个数 一.总结 一句话总结:谋而后动,想清楚,会非常节约编写代码的时间. 1.对结果可能是0的变量,记得初始化? 4 $len=0; 5 $len=strlen($st ...
- linux命令统计文件中某个字符串出现的次数
1.使用grep linux grep命令在我的随笔linux分类里有过简单的介绍,这里就只简单的介绍下使用grep命令统计某个文件这某个字符串出现的次数,首先介绍grep命令的几个参数,详细参数请自 ...
- 新增访客数量MR统计之MR数据输出到MySQL
关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...
- 新增访客数量MR统计之数据库准备
关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...
- 新增访客数量MR统计之Reduce和Runner相关准备
关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...
随机推荐
- QTP测试.NET程序的时候,ComboBox下拉框控件选择后,运行时对象不可见解决方案
解决方法: 录制时,选择下拉框数据的时候,不要鼠标单击选择,而是要用ENTER(回车键)来选择,才能完成选择,这样录制就OK了.
- HiveServer2的WEB UI界面
1.hive-site.xml配置如下: <property> <name>hive.server2.webui.host</name> <val ...
- WIN SERVER 2012 自启动tomcat
本来手动启动时在开始cmd输入startup 自启动的方法是https://blog.csdn.net/ailo555/article/details/82754005 Windows Server ...
- java面试题复习(一)
//基础最重要,如果面试官问一个答不上一个,那有难度的都都不用问了,直接就pass了,就像我,嘿嘿. //每天最好熟记10个问题,这些问题在编程是会很厚帮助,帮你避免很多不应该出现的错误. 一.面向对 ...
- Mybatis pageHelper.startPage(...)是物理分页
使用PageHelper.startPage(...)进行物理分页 业务需求只显示其中的100条数据 之前是在业务逻辑里对参数limit进行了处理 后来试试sql的limit查询100条数据 但是不确 ...
- API & Web API
The follow content refer refers to: Baidu Baike : https://baike.baidu.com/item/api/ ...
- Redis内存分析工具redis-rdb-tools
一.安装redis-rdb-tools(项目地址:github) # git clone https://github.com/sripathikrishnan/redis-rdb-tools# cd ...
- VS2013 切换用户
控制面板---------------用户账户---------------凭据管理器----------------Windows凭据,可以修改和删除登录用户:根据TFS服务器的IP地址或网址或服务 ...
- AlphaGo的前世今生(一)Deep Q Network and Game Search Tree:Road to AI Revolution
这一个专题将会是有关AlphaGo的前世今生以及其带来的AI革命,总共分成三节.本人水平有限,如有错误还望指正.如需转载,须征得本人同意. Road to AI Revolution(通往AI革命之路 ...
- FileStream说明
FileStream(String, FileMode) FileStream(String path, FileMode) 文件打开模式:(FileMode)包括6个枚举 Append:追加 ...