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

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. BumpMap、NormalMap的区别

    原文:http://linuxtest.blog.163.com/blog/static/199927088201275102145354/   一种是Emboss Bump Map(浮雕凹凸贴图), ...

  2. kali linux之edb--CrossFire缓冲区溢出

    漏洞的罪恶根源------变量,数据与代码边界不清,开发人员对用户输入没做过滤,或者过滤不严 如这个脚本,写什么,显示什么,但是加上:,|,&&,后面加上系统命令,就执行命令了 缓冲区 ...

  3. 单机,伪分布式,完全分布式-----搭建Hadoop大数据平台

    Hadoop大数据——随着计算机技术的发展,互联网的普及,信息的积累已经到了一个非常庞大的地步,信息的增长也在不断的加快.信息更是爆炸性增长,收集,检索,统计这些信息越发困难,必须使用新的技术来解决这 ...

  4. 【spring源码】bean的实例化(转载)

    首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. 上一节介绍了Sp ...

  5. 【Thread】线程工厂-ThreadFactory

    ThreadFactory---线程工厂 在apollo源码中有这么一段代码 ExecutorService m_longPollingService = Executors.newSingleThr ...

  6. UML之用例图详解

    原文链接:https://blog.csdn.net/mj_ww/article/details/53020080 UML,即Unified Model Language,统一建模语言.百度百科对他的 ...

  7. django部署ubuntu数据库MYSQL时区问题

    SELECT * FROM mysql.time_zone; SELECT * FROM mysql.time_zone_name; mysql_tzinfo_to_sql /usr/share/zo ...

  8. Tarjan 点双+割点+DFS【洛谷P3225】 [HNOI2012]矿场搭建

    P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...

  9. Android SharedPreferences应用实例(记录App的使用次数)

    1.介绍 2.使用方法 3.java后台 package com.lucky.test46sharedpreferences_apply; import android.content.SharedP ...

  10. C++_类和动态内存分配3-构造函数中使用new的注意事项

    如果在构造函数中使用new来初始化对象的指针成员时必须特别小心. 1 如果在构造函数中使用new来初始化指针成员,则应在析构函数中使用delete. 2 new和delete必须相互兼容.new对应于 ...