对下面一组气温数据进行处理,得到每个月份最高的两个气温值

2018-12-12 14:30 25c
2018-12-12 15:30 26c
2017-12-12 12:30 36c
2019-01-01 14:30 22c
2018-05-05 15:30 26c
2018-05-26 15:30 37c
2018-05-06 15:30 36c
2018-07-05 15:30 36c
2018-07-05 12:30 40c
2017-12-15 12:30 16c

输出格式如下:

2019-1 22
2018-12 26
2018-12 25
2018-7 40
2018-7 36
2018-5 37
2018-5 36
2017-12 36
2017-12 16

public class App {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration(true);
conf.set("fs.defaultFS","hdfs://hadoop01:9000");
     //windows下面运行添加一下两个配置
conf.set("mapreduce.app-submission.cross-platform","true");
conf.set("mapreduce.framework.name","local"); Job job = Job.getInstance(conf); //设置jobName
job.setJobName("myJob");
job.setJarByClass(App.class);
//配置map
//mapper类
job.setMapperClass(MyMapperClass.class);
//输出的key类型
job.setMapOutputKeyClass(TQ.class);
//输出的value类型
job.setMapOutputValueClass(IntWritable.class); //将输出的(K,V)=>(K,V,P)
//job.setPartitionerClass(MyPartitioner.class);
//数据在内存spill(溢写)之前先排序,注:继承WritableComparator
job.setSortComparatorClass(MySortComparator.class); //配置reduce
//根据需求确定分组的维度,继承自WritableComparator
job.setGroupingComparatorClass(MyGrouping.class);
//如map阶段根据年、月、温度三个维度排序,而reduce只根据年、月两个维度
job.setReducerClass(MyReduce.class); Path input=new Path("/input/weather.txt");
Path out=new Path("/output/weather");
if(out.getFileSystem(conf).exists(out)){
out.getFileSystem(conf).delete(out,true);
} //数据来源 HDFS路径
FileInputFormat.addInputPath(job,input);
//计算结果的输出目录
FileOutputFormat.setOutputPath(job,out);
//job.setNumReduceTasks(2); job.waitForCompletion(true);
}
}
public class TQ implements WritableComparable<TQ> {

    private int year;

    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 int getDay() {
return day;
} public void setDay(int day) {
this.day = day;
} public int getTemp() {
return temp;
} public void setTemp(int temp) {
this.temp = temp;
} private int month;
private int day;
/**
温度
*/
private int temp; @Override
public int compareTo(TQ other) { int c1= Integer.compare(this.getYear(),other.getYear());
if(c1==){
return Integer.compare(this.getMonth(),other.getMonth());
}
return c1;
} @Override
public void write(DataOutput out) throws IOException {
out.writeInt(this.year);
out.writeInt(this.month);
out.writeInt(this.day);
out.writeInt(this.temp);
} @Override
public void readFields(DataInput in) throws IOException {
this.year=in.readInt();
this.month=in.readInt();
this.day=in.readInt();
this.temp=in.readInt();
}
}
/**
* 根据年-月对map输出进行分组
*/
public class MyGrouping extends WritableComparator {
public MyGrouping(){
super(TQ.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
TQ tq1 = (TQ) a;
TQ tq2 = (TQ) b;
if (tq1.getYear() == tq2.getYear() && tq1.getMonth() == tq2.getMonth()) {
return ;
}
return ;
}
}
public class MyMapperClass extends Mapper<LongWritable,Text,TQ, IntWritable> {
TQ tq=new TQ();
IntWritable outVal=new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[]splits= value.toString().split(" ");
String[]date=splits[].split("-");
tq.setYear(Integer.parseInt(date[]));
tq.setMonth(Integer.parseInt(date[]));
tq.setDay(Integer.parseInt(date[])); tq.setTemp(Integer.parseInt(splits[].replace("c","")));
outVal.set(tq.getTemp());
context.write(tq,outVal); }
}
public class MyReduce extends Reducer<TQ, IntWritable, Text,IntWritable> {
Text txtKey=new Text(); @Override
protected void reduce(TQ key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
Iterator<IntWritable> iterator = values.iterator();
int flag=;
while (iterator.hasNext()) {
if (flag == ) {
break;
}
txtKey.set(String.format("%s-%s",key.getYear(),key.getMonth())); IntWritable next = iterator.next();
context.write(txtKey,next);
flag++;
}
}
}
/**
数据在内存spill(溢写)之前先排序,根据年月温度
*/
public class MySortComparator extends WritableComparator {
public MySortComparator(){
super(TQ.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
TQ tq1=(TQ)a;
TQ tq2=(TQ)b; int c1= Integer.compare(tq1.getYear(),tq2.getYear());
if(c1==){
int c2=Integer.compare(tq1.getMonth(),tq2.getMonth());
if (c2 == ) {
return -Integer.compare(tq1.getTemp(),tq2.getTemp());
}
return -c2;
}
return -c1;
}
}

hadoop 天气案例的更多相关文章

  1. hadoop经典案例

    hadoop经典案例http://blog.csdn.net/column/details/sparkhadoopdemo.html

  2. python + hadoop (案例)

    python如何链接hadoop,并且使用hadoop的资源,这篇文章介绍了一个简单的案例! 一.python的map/reduce代码 首先认为大家已经对haoop已经有了很多的了解,那么需要建立m ...

  3. Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况

    mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...

  4. jsonp全国天气案例

    案例1: 1.获取跨域数据 2.将数据按照下面的效果放到body里面     key: f49570d39b02b3c203526b5d8255aa61 079179afb105ce2bae9f5d0 ...

  5. Hadoop序列化案例实操

    需求 统计每一个手机号耗费的总上行流量.下行流量.总流量. 输入数据: 1 13736230513 192.196.100.1 www.atguigu.com 2481 24681 200 2 138 ...

  6. Hadoop经典案例(排序&Join&topk&小文件合并)

    ①自定义按某列排序,二次排序 writablecomparable中的compareto方法 ②topk a利用treemap,缺点:map中的key不允许重复:https://blog.csdn.n ...

  7. Hadoop ConnectException: Connection refused的一种解决办法

    跟着视频学习天气案例,把代码敲好,准备提交运行时才发现集群没启动.然后在node02.node03.node04使用zkServer.sh start启动ZooKeeper,然后在node01使用st ...

  8. about云资源汇总指引V1.4:包括hadoop,openstack,nosql,虚拟化

    hadoop资料 云端云计算2G基础课程 (Hadoop简介.安装与范例) 炼数成金3G视频分享下载 虚拟机三种网络模式该如何上网指导此为视频 Hadoop传智播客七天hadoop(3800元)视频, ...

  9. Hadoop基础概念介绍

    基于YARN的配置信息, 参见: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ hadoop入门 - 基础概念 ...

随机推荐

  1. 解决org.hibernate.NonUniqueObjectException的问题

    不知道是不是之前处理懒加载的问题对session工厂进行了处理,导致了之前没有问题的地方出现了错误. 当修改班级操作时出现了错误 前端错误信息 后台处理以及报错信息 16:37:36,034 ERRO ...

  2. Windows bat脚步同步时间

    @echo onnet stop w32timew32tm /unregisterw32tm /registernet start w32timew32tm /config /manualpeerli ...

  3. maven项目运行找不到类的错误

    Maven项目 eclipse工具 错误: [INFO] -------------------------------------------------------------[ERROR] CO ...

  4. [HAOI2007]理想的正方形 BZOJ1047 二维RMQ

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  5. ARKit的使用

    //创建场景 let scene = SCNScene() /* //1.几何 let box = SCNBox.init(width: 0.1, height: 0.1, length: 0.1, ...

  6. javascript的代码块

    a block of code 注意到这个问题是在看书的时候,中文版中出现“代码片段”这样的词语,于是就去翻看了英文版的原书.书中的用了a block of code,难道不应该翻译成代码块吗?(作为 ...

  7. kotlin 注意的地方

    1 . kotlin let 用法: let(val -> ) 注意:这  -> 后面不能有 花括号!!!! 2 . kotlin 中 如果使用了 @Transactional 注解.请让 ...

  8. mfix添加文件后重新生成configure文件

    mfix给了一些程序接口,大部分时候只用修改现有程序即可满足要求,这种情况不用修改configure文件,但是如果添加了新文件就需要做一些修改. 我用了Jian Cai的程序尝试了一下编译,该学者在2 ...

  9. 洛谷 [TJOI2010]中位数

    题目链接 题解 比较水.. 常见套路,维护两个堆 Code #include<bits/stdc++.h> #define LL long long #define RG register ...

  10. 第二次 Scrum Meeting

    第二次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/4/4 19:00 20min 微信群 附Github仓库:WEDO 例会照片 清明假期期间 线上小组例会 工作情 ...