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

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. kali linux之拒绝服务

    Dos不是DOS(利用程序漏洞或一对一资源耗尽的denial of service拒绝服务) DDoS分布式拒绝服务(多对一的攻击汇聚资源能力,重点在于量大,属于资源耗尽型) 历史 以前:欠缺技术能力 ...

  2. 老程序员解Bug的通用套路

    千万不要当程序员面说有bug 对于新手程序员而言,在复杂代码中找BUG是一个难点.下面我们总结下老从程序员解Bug的通用套路,希望对大家有帮助. 1.IDE调试 根据项目特点和语言特点选择一个最合适的 ...

  3. 题解 P1255 【数楼梯】

    题目链接 好吧,承认python 轻松水过 代码奉上: n = int(input()) #定义,输入 a=1 #初始的变量赋值 b=1 n-=1 #我的毒瘤的循环不得不加上这句话 if n > ...

  4. 《Andrew Ng深度学习》笔记2

    神经网络基础 1.图计算 计算时有两种方法:正向传播和反向传播.正向传播是从底层到顶层的计算过程,逐步推出所求公式.反向传播是从顶层到底层,从已知的式子求出因变量的影响关系. 在这里用到的反向传播算法 ...

  5. js 对象 浅拷贝 和 深拷贝

    网上发现一个比较好的博客 阮一峰的感觉很不错推荐大家看看. http://www.ruanyifeng.com/blog/it/javascript/ 接下来看一下这两个拷贝方法 1.浅拷贝 拷贝就是 ...

  6. java基础(多态)_03

    一.多态 1.概念:一个对象的多种形态 2.前提: a:必须有继承 b:必须有重写(只有重写才会有意义,没重写语法没错) 3.体现形式: 父类类型 变量名 = new 子类类型(): 4.注意事项: ...

  7. Spring Boot 例一 实现jsonp接口

    1.新建项目(选择quikstart) 2.增加spring boot 依赖 <dependency> <groupId>org.springframework.boot< ...

  8. 【总结】kali(amd64)中安装nessus

    下载nessus: http://www.tenable.com/products/nessus/select-your-operating-system 注册nessus家庭版 http://www ...

  9. server 2012 R2查询端口

    1. win+r弹出运行对话框,输入cmd,打开cmd窗口 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 ...

  10. EntityFramework 并发处理

    转载自:http://www.cnblogs.com/TianFang/p/4439215.html 什么是并发? 并发分悲观并发和乐观并发. 悲观并发:比如有两个用户A,B,同时登录系统修改一个文档 ...