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进行排序的更多相关文章

  1. MapReduce详解及shuffle阶段

    hadoop1.x和hadoop2.x的区别: Hadoop1.x版本: 内核主要由Hdfs和Mapreduce两个系统组成,其中Mapreduce是一个离线分布式计算框架,由一个JobTracker ...

  2. 【Hadoop】MapReduce笔记(三):MapReduce的Shuffle和Sort阶段详解

    一.MapReduce 总体架构 整体的Shuffle过程包含以下几个部分:Map端Shuffle.Sort阶段.Reduce端Shuffle.即是说:Shuffle 过程横跨 map 和 reduc ...

  3. MapReduce shuffle阶段详解

    在Mapreduce中,Shuffle过程是Mapreduce的核心,它分布在Mapreduce的map阶段和reduce阶段,共可分为6个详细的阶段: 1).Collect阶段:将MapTask的结 ...

  4. MapReduce核心 - - - Shuffle

    大数据名词(1) -Shuffle     Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是必须要了解的.我看过很多相关的资料,但每 ...

  5. MapReduce:Shuffle过程详解

    1.Map任务处理 1.1 读取HDFS中的文件.每一行解析成一个<k,v>.每一个键值对调用一次map函数.                <0,hello you>   & ...

  6. 大数据技术 - MapReduce的Shuffle及调优

    本章内容我们学习一下 MapReduce 中的 Shuffle 过程,Shuffle 发生在 map 输出到 reduce 输入的过程,它的中文解释是 “洗牌”,顾名思义该过程涉及数据的重新分配,主要 ...

  7. MapReduce的Shuffle过程介绍

    MapReduce的Shuffle过程介绍 Shuffle的本义是洗牌.混洗,把一组有一定规则的数据尽量转换成一组无规则的数据,越随机越好.MapReduce中的Shuffle更像是洗牌的逆过程,把一 ...

  8. Hadoop MapReduce的Shuffle过程

    一.概述 理解Hadoop的Shuffle过程是一个大数据工程师必须的,笔者自己将学习笔记记录下来,以便以后方便复习查看. 二. MapReduce确保每个reducer的输入都是按键排序的.系统执行 ...

  9. MapReduce 的 shuffle 过程中经历了几次 sort ?

    shuffle 是从map产生输出到reduce的消化输入的整个过程. 排序贯穿于Map任务和Reduce任务,是MapReduce非常重要的一环,排序操作属于MapReduce计算框架的默认行为,不 ...

随机推荐

  1. 【题解】[NOI2011]阿狸的打字机

    阿狸的打字机 \(\text{Solution:}\) 首先观察三种操作:一种是插入一个字符,一种是退回上一步(回到父亲节点). 所以,我们可以对操作串进行模拟,并处理出每一个串在树上的位置. 接下来 ...

  2. xshell的下载与使用

    昨天刚刚立下每天一篇原创的宏图,今天就停电,到11:05才来电,没办法,学习也学不了了,就只有发一下学过的东西,才能维持得了立下的flag的那个样子,而且,老铁们,今天就不写什么原创博客了,今天转载, ...

  3. Linux操作系统(第二版)(RHEL 8/CentOS 8)

    Linux操作系统(第二版)(RHEL 8/CentOS 8) http://www.tup.tsinghua.edu.cn/booksCenter/book_08172501.html Linux操 ...

  4. 实验三 HTML表格和表单的制作

    实验三 HTML表格和表单的制作 [实验目的] 1.掌握表格的创建.结构调整与美化方法: 2.熟悉表格与单元格的主要属性及其设置方法: 3.掌握通过表格来进行网页页面的布局方法. [实验环境] 连接互 ...

  5. 发布MeteoInfo 1.2.6

    增加了对AWX卫星数据格式的支持(和C#版本的功能相当).在MeteoInfo中打开AWX数据: 在MeteoInfoLab中打开AWX数据:

  6. 【最大匹配+二分答案】HDU 2236 无题II

    题目内容 这是一个简单的游戏,在一个\(n×n\)的矩阵中,找\(n\)个数使得这\(n\)个数都在不同的行和列里并且要求这\(n\)个数中的最大值和最小值的差值最小. 输入格式 输入一个整数\(T\ ...

  7. BUUCTF-[极客大挑战 2019]HardSQL 1详解

    来到sql注入骚姿势,我们一点一点开始学 我们来到这道题,然后尝试注入,结果发现 拼接'or '1'='1 'or '1'='2如果是字符型注入则会报错,然而并没有而是显示的页面一样, 通过常规注入, ...

  8. 详解Kubernetes微服务自动化发布系统

    实施微服务架构后,原先单一的系统结构统变成了数量众多的微服务应用,开发.测试.运维部署等都会面临不少挑战.在微服务架构下如何提高工程研发效率,确保开发.测试.运维部署等流程上的顺畅,是微服务技术体系能 ...

  9. 某次burp抓包出错的解决办法

    前些日子同事发微信问我一个问题 没听懂他说的没回显是啥意思,于是叫他把站发给我. 浏览器不挂burp代理能正常打开,挂上burp代理以后浏览器显示连接超时 首先测试burp能抓其他的包应不是这个原因 ...

  10. 【API管理 APIM】APIM集成内部VNet后,自我访问出现(Unable to connect to the remote server)问题,而Remote Server正是APIM它自己

    问题描述 在使用APIM配置内部VNET后,如API-1正常配置访问后端服务器的一个接口,而API-2则是通过调用APIM中的API-1来作为backendUrl,会出现500错误. 经过测试,目前这 ...