1、数据样本,w1.csv到w5.csv,每个文件数据样本2000条,第一列是年份从1990到2000随机,第二列数据从1-100随机,本例辅助排序目标是找出每年最大值,实际上结果每年最大就是100,但是这里通过mapreduce辅助排序方式来找。

1999,71
1994,57
1995,33
1993,44
1994,99
1994,83
1995,59
... ...

2、核心概念:

1)分区,假设有海量的数据,为了增加并行度,按照hash算法将所有数据分区后,确保同一年的数据进入到同一个分区,也就是同一个reduce里。

2)键比较器,将数据拆分后,年份和数据同时组成一个组合键(本质就是一个对象),因为采用组合键的key,需要一个键排序比较器来对key排序,通过年份升序,数据降序的算法编写核心比较方法。
那么mapper就会安装key比较器将自己所负责的所有数据排序。 3)分组比较器,在reducer阶段,需求实际上只求最大值,那么实际上就是排序后的第一条,如果reducer阶段不做什么变化,那么数据将会安装年份升序和数据降序输出所有数据(重复的已经被reduce过滤)。
为了只得到一条最大的数据,可以采用设置分组器的方式实现。同一年份,我们只需要一条数据,那么分组比较器就可以只按照年份分组,分组后,reducer最终归并数据后,只会得到排第一的那条最大数据。
这种取最大值得方式,实际上是取巧,是依赖mapper和reducer的排序特性而来。

3、IntPair,本例把整行数据解析后,年份和数据都放入key,需要自定义一个IntPair对象,实际生产环境中可根据需求自定义各种类.

public class IntPair implements WritableComparable<IntPair> {
private int first;
private int second; public IntPair() {
} public IntPair(int first, int second) {
this.first = first;
this.second = second;
} public int getFirst() {
return first;
} public void setFirst(int first) {
this.first = first;
} public int getSecond() {
return second;
} public void setSecond(int second) {
this.second = second;
} @Override
public int compareTo(IntPair o) {
int result = Integer.valueOf(first).compareTo(o.getFirst());
if(result==0){
result = Integer.valueOf(second).compareTo(o.getSecond());
}
return result;
} public static int compare(int first1,int first2){
return Integer.valueOf(first1).compareTo(Integer.valueOf(first2));
} @Override
public void write(DataOutput out) throws IOException {
out.writeInt(first);
out.writeInt(second);
} @Override
public void readFields(DataInput in) throws IOException {
first = in.readInt();
second = in.readInt();
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; IntPair intPair = (IntPair) o; if (first != intPair.first) return false;
return second == intPair.second;
} @Override
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
} @Override
public String toString() {
return first+"\t"+second;
}
}

4、RecordParser,记录解析器,用于解析数据,规避错误数据

public class RecordParser {
private int year;
private int data;
private boolean valid; public int getYear() {
return year;
} public int getData() {
return data;
} public boolean isValid() {
return valid;
} public void parse(String value){
String[] sValue = value.split(",");
try {
year = Integer.parseInt(sValue[0]);
data = Integer.parseInt(sValue[1]);
valid = true;
}catch (Exception e){
valid = false;
}
}
}

5、分区器

/**
* @Author: xu.dm
* @Date: 2019/2/21 11:56
* @Description:根据key进行分区,确保同一个key.first进入相同的分区,泛型类型和mapper输出一致
*/
public class FirstPartitioner extends Partitioner<IntPair,IntWritable> {
/**
* Get the partition number for a given key (hence record) given the total
* number of partitions i.e. number of reduce-tasks for the job.
* <p>
* <p>Typically a hash function on a all or a subset of the key.</p>
*
* @param key the key to be partioned.
* @param value the entry value.
* @param numPartitions the total number of partitions.
* @return the partition number for the <code>key</code>.
*/
@Override
public int getPartition(IntPair key, IntWritable value, int numPartitions) {
return Math.abs(key.getFirst() * 127) % numPartitions;
}
}

6、key比较器,map阶段的key排序使用,如果没有分组比较器,则key比较器也会应用在混洗和reduce阶段。

/**
* @Author: xu.dm
* @Date: 2019/2/21 11:59
* @Description: key比较器
* 对IntPair的first升序,second降序,在mapper排序的时候被应用
* 最终同样年份的数据第一条是最大的。
*/
public class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(IntPair.class,true);//需要实例化
} @Override
public int compare(WritableComparable a, WritableComparable b) {
IntPair p1=(IntPair)a;
IntPair p2=(IntPair)b;
int result = IntPair.compare(p1.getFirst(),p2.getFirst());
if(result==0){
result = -IntPair.compare(p1.getSecond(),p2.getSecond()); //前面加一个减号求反
}
return result;
}
}

7、分组比较器,这里最关键,看注释。

/**
* @Author: xu.dm
* @Date: 2019/2/21 12:16
* @Description: 分组比较器,应用在reduce阶段,数据进reduce后,归并之前。
* 本例目标是:确保同一个年份的数据在同一个组里
* 之前key比较器使得key值中的年份升序,数据降序排列。
* 那么这个分组比较器只按年进行比较,意味着,[1990,100]和[1990,00]会被认为是相同的分组,
* 而,reduce阶段,相同的KEY只取第一个,哦也,这个时候,reduce阶段后,年份中最大的数据就被保存下来,其他数据都被kickout
* 所以,用这种方式变相的达到取最大值得效果。
*/
public class GroupComparator extends WritableComparator {
public GroupComparator() {
super(IntPair.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
IntPair p1=(IntPair)a;
IntPair p2=(IntPair)b;
return IntPair.compare(p1.getFirst(),p2.getFirst());
}
}

8、mapper,如果只取年份里的最大数据,Mapper<LongWritable,Text,IntPair,IntWritable> 的IntWritable可以用NullWritable,这里保留IntWritable是因为,程序稍加改动就可以输出所有年份数据的计数

public class DataMapper extends Mapper<LongWritable,Text,IntPair,IntWritable> {
private RecordParser parser = new RecordParser(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
parser.parse(value.toString());
if(parser.isValid()){
context.write(new IntPair(parser.getYear(),parser.getData()),new IntWritable(1));
context.getCounter("MapValidData","dataCounter").increment(1); //做一个计数,总的数据应该是10000条。
}
}
}

9、reducer

public class DataReducer extends Reducer<IntPair,IntWritable,IntPair,IntWritable> {

    @Override
protected void reduce(IntPair key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
//因为分组器,[1990,100]和[1990,00]会被认为是相同的分组
//这里的计数就会混淆。如果需要年份下各数据的正确的计数结果,则需要注销分组器
// for(IntWritable val:values){
// sum+=val.get();
// } context.write(key,new IntWritable(sum));
}
}

10、job

public class DataSecondarySort extends Configured implements Tool {
/**
* Execute the command with the given arguments.
*
* @param args command specific arguments.
* @return exit code.
* @throws Exception
*/
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf(); Job job = Job.getInstance(conf,"Secondary Sort");
// conf.set("mapreduce.job.ubertask.enable","true"); if(conf==null){
return -1;
} job.setJarByClass(DataSecondarySort.class);
job.setMapperClass(DataMapper.class);
job.setPartitionerClass(FirstPartitioner.class);
job.setSortComparatorClass(KeyComparator.class);
// 决定如何分组
job.setGroupingComparatorClass(GroupComparator.class);
job.setReducerClass(DataReducer.class);
// job.setNumReduceTasks(2);//如果数据海量,则可以根据情况设置reduce的数目,也是分区的数量,通过Tool类,也可以在命令行进行设置 job.setOutputKeyClass(IntPair.class);
//如果只求最大数,前面的mapper,reducer和这里的输出都可以设置成NullWritable
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1])); Path outPath = new Path(args[1]);
FileSystem fileSystem = outPath.getFileSystem(conf);
//删除输出路径
if(fileSystem.exists(outPath))
{
fileSystem.delete(outPath,true);
} return job.waitForCompletion(true) ? 0:1;
} public static void main(String[] args) throws Exception{
int exitCode = ToolRunner.run(new DataSecondarySort(),args);
System.exit(exitCode);
}
}

11、如果求最大值,结果会是这样:

[hadoop@bigdata-senior01 ~]$ hadoop fs -cat /output3/part-r-00000 | more
1990 100 0
1991 100 0
1992 100 0
1993 100 0
1994 100 0
1995 100 0
1996 100 0
1997 100 0
1998 100 0
1999 100 0
2000 100 0

如果求最大值和计数则会列出所有数据,当然需要注销分组器的set代码,并打开reducer的sum

[hadoop@bigdata-senior01 ~]$ hadoop fs -cat /output/part-r-00000 | more
1990 100 10
1990 99 15
1990 98 10
1990 97 9
1990 96 6
1990 95 4
1990 94 12
1990 93 9
1990 92 12
1990 91 13
1990 90 8
1990 89 9
... ...

多个分区可以使用job.setNumReduceTasks(n),或者在命令行上指定

[hadoop@bigdata-senior01 ~]$ hadoop jar DataSecondarySort.jar -D mapreduce.job.reduces=3 /sampler /output3

[hadoop@bigdata-senior01 ~]$ hadoop fs -ls /output3
Found 4 items
-rw-r--r-- 1 hadoop supergroup 0 2019-02-21 15:29 /output3/_SUCCESS
-rw-r--r-- 1 hadoop supergroup 2868 2019-02-21 15:29 /output3/part-r-00000
-rw-r--r-- 1 hadoop supergroup 3860 2019-02-21 15:29 /output3/part-r-00001
-rw-r--r-- 1 hadoop supergroup 3850 2019-02-21 15:29 /output3/part-r-00002

总结一下:最容易混淆的概念就是分组,而排序和分组实际上就是MapReduce最核心的地方。

总结步骤:
1、视数据量大小决定是否分区,分几个区输出数据,可以在作业中设置,也可以在命令行中指定
2、规划数据结构,抽象为对象,自定义对象排序规则,实现排序接口,明确key排序比较器
3、自定义分组规则,视情况进行分组归纳,实现分组排序接口
4、作业配置中配置分区类、排序类、分组类以及输出类。
 

hadoop MapReduce辅助排序解析的更多相关文章

  1. 三种方法实现Hadoop(MapReduce)全局排序(1)

    我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...

  2. MapReduce辅助排序

    需求:订单数据 求出每个订单中最贵的商品? 订单id正序,成交金额倒序. 结果文件三个,每个结果文件只要一条数据. 1.Mapper类 package com.css.order.mr; import ...

  3. Hadoop mapreduce自定义排序WritableComparable

    本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...

  4. Hadoop mapreduce自定义分组RawComparator

    本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...

  5. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  6. Hadoop Mapreduce分区、分组、二次排序

    1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2)定制了partitioner以将map的结果送往指定reducer的过程: map - partiti ...

  7. Hadoop案例(八)辅助排序和二次排序案例(GroupingComparator)

    辅助排序和二次排序案例(GroupingComparator) 1.需求 有如下订单数据 订单id 商品id 成交金额 0000001 Pdt_01 222.8 0000001 Pdt_05 25.8 ...

  8. Hadoop Mapreduce分区、分组、二次排序过程详解

    转载:http://blog.tianya.cn/m/post.jsp?postId=53271442 1.MapReduce中数据流动 (1)最简单的过程:  map - reduce (2)定制了 ...

  9. Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

随机推荐

  1. Shr-前端汇总

    F7控件监听 f7控件的监听是指,在点击F7控件时,对控件内的内容进行选中后出发该事件监听:通过参数value可以获取当前F7控件的一些值信息. //人力编制方案监听 回写计划期间 及分录数据 ini ...

  2. Java实现邮件发送

      概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <depende ...

  3. MongoDB-Ubuntu环境下安装

    1.在官网下载安装包,下载后为 mongodb-linux-x86_64-ubuntu1604-3.4.6.tgz 解压:tar -zxvf mongodb-linux-x86_64-ubuntu16 ...

  4. 安装MySQLdb模块遭遇"fatal error: my_config.h: No such file or directory"的处理

    Issue       I encountered an error when I run the python script which need to import the module of & ...

  5. 几个常见移动平台浏览器的User-Agent

    之前介绍的手机站跳转url的一片文稿中提到,依据User Agent判断终端的方法.(文章地址:http://www.cnblogs.com/dereksunok/p/3664169.html ) 若 ...

  6. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  7. 如何在etherscan提交代币官方信息

    https://ethlinkersupport.zendesk.com/hc/zh-cn/articles/360001334992-%E5%A6%82%E4%BD%95%E5%9C%A8ether ...

  8. 在 CentOS 下手工安装 Docker v1.1x

    Docker在 centos 6.x 下面默认最新的版本是1.7, 然而这个并不符合我的实际需求, 尤其我需要 docker-compose 来作为编配工具部署swarm, 所以我只有手工安装了. 首 ...

  9. Linux下安装paramiko

    paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台, ...

  10. LintCode-68.二叉树的后序遍历

    二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [3,2,1] 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code / ...