Hadoop基础-Map端链式编程之MapReduce统计TopN示例
Hadoop基础-Map端链式编程之MapReduce统计TopN示例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.项目需求
对“temp.txt”中的数据进行分析,统计出各个年份(第15~19列)总排行前十的最高气温(第87~92列),由于博客园无法上传大文件的文本,因此我把该文本的内容放在博客园的另一个链接了(需要的戳我)。,如果网页打不开的话也就可以去百度云盘里下载副本,链接:链接:https://pan.baidu.com/s/12aZFcO2XoegUGMAbS--n6Q 密码:7n91。

二.代码实现
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.WritableComparable; import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; public class CompKey implements WritableComparable<CompKey> { private String year;
private int temp;
/**
* 重写CompKey对年份和气温排序
*/
public int compareTo(CompKey o) {
if(this.getYear().equals(o.getYear())){
return o.getTemp() - this.getTemp();
}
return this.getYear().compareTo(o.getYear()); } public void write(DataOutput out) throws IOException {
out.writeUTF(year);
out.writeInt(temp); } public void readFields(DataInput in) throws IOException {
year = in.readUTF();
temp = in.readInt(); } public String getYear() {
return year;
} public void setYear(String year) {
this.year = year;
} public int getTemp() {
return temp;
} public void setTemp(int temp) {
this.temp = temp;
} @Override
public String toString() {
return year + '\t' +temp ;
}
}
CompKey.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class MyGroupComparator extends WritableComparator { public MyGroupComparator() {
super(CompKey.class,true);
} public int compare(WritableComparable a, WritableComparable b) {
CompKey ck1 = (CompKey) a;
CompKey ck2 = (CompKey) b;
return ck1.getYear().compareTo(ck2.getYear());
}
}
MyGroupComparator.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class ChainMapper1 extends Mapper<LongWritable, Text, Text, IntWritable> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); //得到年份
String year = line.substring(15, 19); //得到气温
int temp = Integer.parseInt(line.substring(87, 92)); context.write(new Text(year), new IntWritable(temp)); }
}
ChainMapper1.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class ChainMapper2 extends Mapper<Text,IntWritable,CompKey,NullWritable> { @Override
protected void map(Text key, IntWritable value, Context context) throws IOException, InterruptedException { int i = value.get(); if( i != 9999){
CompKey ck = new CompKey();
ck.setYear(key.toString());
ck.setTemp(i);
context.write(ck,NullWritable.get());
}
}
}
ChainMapper2.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException;
import java.util.Iterator; public class ChainReducer1 extends Reducer<CompKey, NullWritable, Text, IntWritable> { //由于分组对比器设定,相同的year放在一个分组,因此,在一个reduce循环中,得到的数据均为同一年份的数据
protected void reduce(CompKey key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
String year = key.getYear();
Iterator<NullWritable> it = values.iterator();
int i = 0;
while (it.hasNext()){
System.out.println(key.toString());
int temp = key.getTemp();
context.write(new Text(year), new IntWritable(temp));
it.next();
i++;
if(i >= 10){
break;
}
}
}
}
ChainReducer1.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class ChainReducer2 extends Mapper<Text, IntWritable, Text,IntWritable> {
protected void map(Text key, IntWritable value, Context context) throws IOException, InterruptedException {
int temp = value.get();
//取得奇数气温
if( temp % 2 == 1 ){
context.write(key, new IntWritable(temp));
} }
}
ChainReducer2.java 文件内容
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.mrchain; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class ChainApp { public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
conf.set("fs.defaultFS","file:///"); Job job = Job.getInstance(conf); FileSystem fs = FileSystem.get(conf); job.setJobName("Chain"); job.setJarByClass(ChainApp.class);
//在MapChain中,一个Map后面可以跟n多Map
ChainMapper.addMapper(job,ChainMapper1.class,LongWritable.class, Text.class,
Text.class, IntWritable.class,conf); ChainMapper.addMapper(job,ChainMapper2.class,Text.class,IntWritable.class,
CompKey.class,NullWritable.class,conf); //在ReduceChain中,一个Reduce中不能跟reduce,只能跟map
ChainReducer.setReducer(job,ChainReducer1.class,CompKey.class,NullWritable.class,
Text.class,IntWritable.class,conf); ChainReducer.addMapper(job,ChainReducer2.class, Text.class, IntWritable.class,
Text.class,IntWritable.class, conf); job.setGroupingComparatorClass(MyGroupComparator.class); //判断是否存在,如果存在则删除
Path outPath = new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\out");
if(fs.exists(outPath)){
fs.delete(outPath,true);
} //输入路径
FileInputFormat.addInputPath(job,new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\temp")); //输出路径
FileOutputFormat.setOutputPath(job,outPath); job.waitForCompletion(true);
}
}
Hadoop基础-Map端链式编程之MapReduce统计TopN示例的更多相关文章
- Android框架式编程之Android Architecture Components
1. 当前Android开发面临的问题 Android开发不同于传统的桌面程序开发,桌面程序一般都有唯一的快捷方式入口,并且常作为单进程存在:而一个典型的Android应用通常由多个应用组件构成,包括 ...
- Android框架式编程之RxJava(一):HelloWorld
Hello World 源码: import android.graphics.Bitmap; import android.graphics.BitmapFactory; import androi ...
- Jquery | 基础 | 事件的链式写法
$(".title").click(function () { $(this).addClass("curcol").next(".content&q ...
- Android框架式编程之LiveData
一.LiveData 介绍 LiveData是 Google 推荐的 Android 架构组件之一,是一个基于观察者模式的数据容器,但与一般的被观察者不同的是,它是有生命周期感知功能,解决了Andro ...
- 应聘复习基础笔记1:网络编程之TCP与UDP的优缺点,TCP三次握手、四次挥手、传输窗口控制、存在问题
重要性:必考 一.TCP与UDP的优缺点 ①TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供 ...
- Android框架式编程之BufferKnife
配置 compile 'com.jakewharton:butterknife:(insert latest version)' annotationProcessor 'com.jakewharto ...
- Android框架式编程之Room
Room是Google官方出品的ORM(Object-relational mapping) 框架.当前我们也知道当前还有很多的ORM框架,例如GreenDao.OrmLite.Litepal等.目前 ...
- Android框架式编程之Retrofit
一.Retrofit 简介 Retrofit 官网地址: https://github.com/square/retrofit Retrofit(即Retrofit,目前最新版本为2.6.0版本),是 ...
- Android框架式编程之MVP架构
MVP(Model-View-Presenter)模式.是将APP的结构分为三层:View - Presenter - Model. View 1. 提供UI交互 2. 在presenter的控制下修 ...
随机推荐
- html点击链接打开新窗口
html标记中格式为<a href="url"> text </a> 此时,内容在原来窗口呈现,如果想新开窗口,可以采用下列方式. 1. <a hre ...
- vue 监听页面宽度变化 和 键盘事件
vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { scr ...
- Neo4j 第四篇:使用C#更新和查询Neo4j
本文使用的IDE是Visual Studio 2015 ,驱动程序是Neo4j官方的最新版本:Neo4j Driver 1.3.0 ,创建的类库工程(Project)要求安装 .NET Framewo ...
- redis见解
http://blog.csdn.net/zhiguozhu/article/details/50517527Redis原生session与redis中的session区别原生session在服务器上 ...
- cf 1029D
题面 题目描述 给定含n个整数的数组a. 规定数x,y的合并为xy.如:数12与数3456的合并为数123456. 有数组中的位置对(i,j)(i≠j),计算使ai,aj的合并能被k整除的位置对数量. ...
- Nuxeo 认证绕过和RCE漏洞分析(CVE-2018-16341)
简介 Nuxeo Platform是一款跨平台开源的企业级内容管理系统(CMS).nuxeo-jsf-ui组件处理facelet模板不当,当访问的facelet模板不存在时,相关的文件名会输出到错误页 ...
- linux gcc编译多个源文件的方法
http://blog.csdn.net/yinjiabin/article/details/7731817
- 第一个sprint冲刺第一阶段
会议地址:男生宿舍1栋B4014 会议内容:讨论如何完成产品 成员:李新,朱浩龙,陈俊金,叶煜稳,林德麟 困难:对于做成一个手机APP,尚未掌握:成员尚在学习中 master:陈俊金
- 第一个Sprint冲刺总结(事后诸葛亮及团队贡献分)
第一个Sprint冲刺总结(事后诸葛亮及团队贡献分) 组员:欧其锋 廖焯燊 林海信 何武鹏 第一阶段的最终燃尽图如下: 2.事后诸葛亮: 3.团队贡献分: 欧其锋:22 林海信:21 何武鹏:19 ...
- Java运算符、switch、数组、排序
1.Java的运算符,分为四类:算数运算符.关系运算符.逻辑运算符.位运算符 运算符例子:22.25(十进制转化为二进制,8421码)0010 0010 (22)0010 0101 (25) 位运算符 ...