MapReduce在Shuffle阶段按Mapper输出的Value进行排序
ZKe
-----------------
在MapReduce框架中,Mapper的输出在Shuffle阶段,根据Key值分组之后,还将会根据Key值进行排序,因此Reducer的输出我们看到的结果是按Key有序的。
同样我们可以让它按Value有序。通过job.setSortComparatorClass(IntWritableComparator.class);即可(这里的排序规则和类型通过自己定义)
实体类不仅需要实现Comparable接口,同样还要重写readFiles方法和write方法。然后定义一个该实体的比较器。
这里定义一个实体类,由String的id和int的count作为属性,我们根据count进行排序。
static class Record implements Comparable<Record>{
private String personalId;
private int count;
public Record(String id, int count){
this.personalId = id;
this.count = count;
}
public Record(String line){
this.personalId = line.split("\t")[0];
this.count = Integer.parseInt(line.split("\t")[1]);
}
/*
* 反序列化方法
* @author 180512235 ZhaoKe
*/
public void readFields(DataInput arg0) throws IOException {
this.personalId = arg0.readUTF();
this.count = arg0.readInt();
}
// 序列化方法
public void write(DataOutput arg0) throws IOException {
arg0.writeUTF(this.personalId);
arg0.writeInt(this.count);
}
public int compareTo(Record o) {
// TODO Auto-generated method stub
return this.count<o.count?1:-1;
}
public String getPersonalId(){
return this.personalId;
}
public int getCount(){
return this.count;
}
}
它的比较器如下
static class IntWritableComparator extends WritableComparator {
/*
* 重写构造方法,定义比较类 IntWritable
*/
public IntWritableComparator() {
super(IntWritable.class, true);
}
/*
* 重写compare方法,自定义比较规则
*/
@Override
public int compare(WritableComparable a, WritableComparable b) {
//向下转型
IntWritable ia = (IntWritable) a;
IntWritable ib = (IntWritable) b;
return ib.compareTo(ia);
}
}
Mapper和Reducer如下,没有任何操作,因为Shuffle阶段自己会调用比较器进行排序
static class SortMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
private Record r;
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
r = new Record(value.toString());
context.write(new IntWritable(r.getCount()), new Text(r.getPersonalId()));
}
}
static class SortReducer extends Reducer<IntWritable, Text, Text, IntWritable>{
protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
for(Text value:values){
context.write(value, key);
}
}
}
主类如下,大家作为模板即可
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// TODO Auto-generated method stub
String inputFile = "hdfs://master:9000/user/root/finalClassDesign/originData/submitTop10output/";
String outputFile = "hdfs://master:9000/user/root/finalClassDesign/originData/sortedSubmitTop10/";
BasicConfigurator.configure();
Configuration conf = new Configuration();
// String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
// if(otherArgs.length != 2){
// System.err.println("Usage:wordcount<in><out>");
// System.exit(2);
// }
Job job = Job.getInstance(conf, "WordCount");
job.setJarByClass(SortByMapReduce.class);
job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setSortComparatorClass(IntWritableComparator.class); // 此处必须注意设置比较器=======================================
// Path path = new Path(otherArgs[1]);
Path path = new Path(outputFile);
FileSystem fileSystem = path.getFileSystem(conf);
if(fileSystem.exists(path)){
fileSystem.delete(path, true);
}
// FileInputFormat.setInputPaths(job, new Path(args[0]));
// FileOutputFormat.setOutputPath(job, new Path(args[1]));
FileInputFormat.setInputPaths(job, new Path(inputFile));
FileOutputFormat.setOutputPath(job, new Path(outputFile));
boolean res = job.waitForCompletion(true);
if(res)
System.out.println("===========waitForCompletion:"+res+"==========");
System.exit(res?0:1);
}
MapReduce在Shuffle阶段按Mapper输出的Value进行排序的更多相关文章
- MapReduce详解及shuffle阶段
hadoop1.x和hadoop2.x的区别: Hadoop1.x版本: 内核主要由Hdfs和Mapreduce两个系统组成,其中Mapreduce是一个离线分布式计算框架,由一个JobTracker ...
- 【Hadoop】MapReduce笔记(三):MapReduce的Shuffle和Sort阶段详解
一.MapReduce 总体架构 整体的Shuffle过程包含以下几个部分:Map端Shuffle.Sort阶段.Reduce端Shuffle.即是说:Shuffle 过程横跨 map 和 reduc ...
- MapReduce shuffle阶段详解
在Mapreduce中,Shuffle过程是Mapreduce的核心,它分布在Mapreduce的map阶段和reduce阶段,共可分为6个详细的阶段: 1).Collect阶段:将MapTask的结 ...
- MapReduce核心 - - - Shuffle
大数据名词(1) -Shuffle Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每 ...
- MapReduce:Shuffle过程详解
1.Map任务处理 1.1 读取HDFS中的文件.每一行解析成一个<k,v>.每一个键值对调用一次map函数. <0,hello you> & ...
- 大数据技术 - MapReduce的Shuffle及调优
本章内容我们学习一下 MapReduce 中的 Shuffle 过程,Shuffle 发生在 map 输出到 reduce 输入的过程,它的中文解释是 “洗牌”,顾名思义该过程涉及数据的重新分配,主要 ...
- MapReduce的Shuffle过程介绍
MapReduce的Shuffle过程介绍 Shuffle的本义是洗牌.混洗,把一组有一定规则的数据尽量转换成一组无规则的数据,越随机越好.MapReduce中的Shuffle更像是洗牌的逆过程,把一 ...
- Hadoop MapReduce的Shuffle过程
一.概述 理解Hadoop的Shuffle过程是一个大数据工程师必须的,笔者自己将学习笔记记录下来,以便以后方便复习查看. 二. MapReduce确保每个reducer的输入都是按键排序的.系统执行 ...
- MapReduce 的 shuffle 过程中经历了几次 sort ?
shuffle 是从map产生输出到reduce的消化输入的整个过程. 排序贯穿于Map任务和Reduce任务,是MapReduce非常重要的一环,排序操作属于MapReduce计算框架的默认行为,不 ...
随机推荐
- RHSA-2017:2029-中危: openssh 安全和BUG修复更新(存在EXP、代码执行、本地提权)
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...
- git 上传文件到 gitee 码云远程仓库(强制上传)
1.先git init 会出现一个.git的文件夹,有些人可能是隐藏了,工具哪里打开就行了 2.将当前的数据上传到码云,看清楚奥,是当前.git add ./ 这是代表当前的意思 3.将上传的数据备注 ...
- Java防止文件被篡改之文件校验和
Java防止文件被篡改之文件校验和转载:请注明出处,谢谢! 1.为什么要防止文件被篡改? 答案是显然的,为了保证版权,系统安全性等.之前公司开发一个系统,技术核心是一个科学院院士的研究成果,作为一款 ...
- Ubuntu 20.04上通过Wine 安装微信
没有想过会在一个手机软件上花这么多心思,好在今天总算安装成功,觉得可以记录下这个过程,方便他人方便自己. 首先介绍下我使用过的其他方法,希望可以节省大家一些时间: Rambox Pro:因为原理是网页 ...
- 非阻塞I/O和阻塞I/O
1.简介 等待队列实现在事件上的条件等待:希望等待特定事件的进程把自己放进合适的等待队列,并放弃控制权.可用于: - 中断处理 - 进程同步 - 定时 2.等待队列头数据结构 1 typedef st ...
- Linux下clock子系统
常用API: 1.struct clk *clk_get(struct device *dev, const char *id):从一个时钟list链表中以dev或者字符id名称查找一个时钟clk结构 ...
- JS中实现Trim(),TrimStart(),TrimEnd() 的方法
//去除字符串头尾空格或指定字符 String.prototype.Trim = function (c) { if (c == null || c == "") { var st ...
- 微信聊天记录导出为csv,并生成词云图
微信聊天记录生成特定图片图云 首先贴上github地址 https://github.com/ghdefe/WechatRecordToWordCloud 来个效果图 提取聊天记录到csv参考教程 h ...
- 很多人都搞不清楚C语言和C++的关系!今天我们来一探究竟为大家解惑~
最近,身边有许多小伙伴已经开始学习编程了,但是呢,学习又会碰到许多的问题,其中作为新手小白提到最多的问题就是编程语言的选择. 每次遇到这种问题,看起来很简单,但是又有很多小伙伴搞不清编程语言之间的关系 ...
- EfCore3的OwnedType会导致Sql效率问题
最近主导了旗下某核心项目升级到EfCore3 由于之前Core2升级时候也踩过不少的坑很多东西都有规划和准备,整体上还是没出太大问题 但是最近突然发现efcore对于使用了ownedType的生成语句 ...