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统计每年中每月温度的前三名的更多相关文章

  1. sort +awk+uniq 统计文件中出现次数最多的前10个单词

    实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

  2. SQL语句统计每天、每月、每年的 数据

    SQL语句统计每天.每月.每年的数据 1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月 ...

  3. 【转】SQL语句统计每天、每月、每年的数据

    原文:https://www.cnblogs.com/Fooo/p/3435687.html SQL语句统计每天.每月.每年的数据 1.每年select year(ordertime) 年,sum(T ...

  4. JAVA实验--统计文章中单词的个数并排序

    分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...

  5. php实现 统计输入中各种字符的个数

    php实现 统计输入中各种字符的个数 一.总结 一句话总结:谋而后动,想清楚,会非常节约编写代码的时间. 1.对结果可能是0的变量,记得初始化? 4 $len=0; 5 $len=strlen($st ...

  6. linux命令统计文件中某个字符串出现的次数

    1.使用grep linux grep命令在我的随笔linux分类里有过简单的介绍,这里就只简单的介绍下使用grep命令统计某个文件这某个字符串出现的次数,首先介绍grep命令的几个参数,详细参数请自 ...

  7. 新增访客数量MR统计之MR数据输出到MySQL

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...

  8. 新增访客数量MR统计之数据库准备

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...

  9. 新增访客数量MR统计之Reduce和Runner相关准备

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...

随机推荐

  1. GIT教程的好文章

    Git 教學(1):Git的基本使用 Git 教學(2):Git Branch 的操作與基本工作流程 Git 情境劇:告訴你使用 Git 時什麼情況該下什麼指令

  2. web 安全:

    XSSXSS 全称“跨站脚本”,是注入攻击的一种. 其特点是不对服务器端造成任何伤害,而是通过一些正常的站内交互途径,例如发布评论,提交含有 JavaScript 的内容文本. 这时服务器端如果没有过 ...

  3. javascript条件语句

    //条件语句 if (false) { console.log("is true") } else { console.log("is false") } // ...

  4. python3下调用系统massagebox对话框

    #python3下调用系统massagebox对话框#先安装pwin32插件https://github.com/mhammond/pywin32/releases import win32apiim ...

  5. java高并发实战(三)——Java内存模型和线程安全

    转自:https://blog.csdn.net/gududedabai/article/details/80816488

  6. JDBC数据库连接参数备忘

    database driver url remark MySql com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/xxx mysql-connect ...

  7. oracle数据库误删的表以及表中记录的恢复

    oracle数据库误删的表以及表中记录的恢复 一.表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: --1.从flash bac ...

  8. CentOS7虚拟机配置ip地址

    首先安装后的虚拟机选NAT模式配置vm的虚拟网络编辑器(vmware中的编辑),NAT模式中查看DHCP的范围,配置子网(写成和电脑一样),在linux中进入/etc/sysconfig/networ ...

  9. linux查看指定时间段的日志

    不需要tail命令,直接 grep '2018-04-22 12:3[2-9]' tesl.log: 这就是查询指定文件的2018-04-22 12点32分到39分的日志

  10. [Sw] Swoole-4.2.9 可以尝试愉快应用 Swoole 协程

    大家知道 Swoole 提供了方便于服务器.网络编程的模式,简化了多进程编程. 这直接让 PHP 的运行很容易变成常驻内存的 Server 程序,执行效率上有了数倍的提升. 但是这一切还没有让人足够兴 ...