Hadoop-5、排序(Combiner泛谈)

一、Combiner作用

1、combiner最基本是实现本地key的聚合,对map输出的key排序,value进行迭代。如下所示:

map: (K1, V1) → list(K2,
V2) 
combine: (K2, list(V2)) →
list(K2, V2) 
reduce: (K2,
list(V2)) → list(K3, V3)

2、combiner还具有类似本地的reduce功能.

例如hadoop自带的wordcount的例子和找出value的最大值的程序,combiner和reduce完全一致。如下所示:
map: (K1, V1) → list(K2, V2) 
combine: (K2, list(V2)) → list(K3,
V3) 
reduce: (K3, list(V3)) →
list(K4, V4)

3、如果不用combiner,那么,所有的结果都是reduce完成,效率会相对低下。使用combiner,先完成的map会在本地聚合,提升速度。

4、对于hadoop自带的wordcount的例子,value就是一个叠加的数字,所以map一结束就可以进行reduce的value叠加,而不必要等到所有的map结束再去进行reduce的value叠加。

二、总结

1、combiner使用的合适,可以在满足业务的情况下提升job的速度,如果不合适,则将导致输出的结果不正确。

本程序不能是用combiner,不然出错。


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class Sort {
public static class Map extends Mapper<Object,Text,IntWritable,IntWritable>{
private static IntWritable num = new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line = value.toString();
num.set(Integer.parseInt(line));
context.write(num, new IntWritable(1));
}
}

public static class Reduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
private static IntWritable count = new IntWritable(0);
public void reduce(IntWritable key,Iterable<IntWritable> value,Context context) throws IOException, InterruptedException{
for(IntWritable val : value){
count = new IntWritable(count.get()+1);
context.write(count,key);
}
}
}

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf = new Configuration();
conf.addResource(new Path("/usr/hadoop-1.0.3/conf/core-site.xml"));

String[] arg = new GenericOptionsParser(conf,args).getRemainingArgs();

Job job = new Job(conf,"Sort");

job.setJarByClass(Sort.class);

job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(arg[0]));
FileOutputFormat.setOutputPath(job, new Path(arg[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

File1

?





1

2

3

4

5

6

7

2

32

654

32

15

756

65223

File2

?





1

2

3

4

5956

22

650

92

File3

?





1

2

3

26

54

6

结果:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

1   2

2   6

3   15

4   22

5   26

6   32

7   32

8   54

9   92

10  650

11  654

12  756

13  5956

14  65223

  

时间: 2024-07-30 21:35:40

Hadoop-5、排序(Combiner泛谈)的相关文章

hadoop mapreduce排序原理

 hadoop  mapreduce排序原理 Hadoop 案例3----数据排序  简单问题  (入门级别) "数据排序"是许多实际任务执行时要完成的第一项工作, 比如学生成绩评比.数据建立索引等.这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础.下面进入这个示例. 1.需求描述 对输入文件中数据进行排序.输入文件中的每行内容均为一个数字,即一个数据. 要求在输出中每行有两个间隔的数字,其中,第一个代表原始数据在原始数据集中的位次,第二个代表原始数据.

Hadoop 默认排序

Hadoop  默认排序 1       3 1       2 1       1 3       3 3       2 2       2 2       1 3       1 排序后:左右前面一列排序 后面一列不排序  要想第二列也排序  请看  Hadoop二次排序 1 3 1 2 1 1 2 2 2 1 3 3 3 2 3 1 代码为: package com.hadoop.test.defaultsort; import java.io.IOException; import o

Hadoop学习之Combiner

在Hadoop中,有一种处理过程叫Combiner,与Mapper和Reducer在处于同等地位,但其执行的时间介于Mapper和Reducer之间,其实就是Mapper和Reducer的中间处理过程,Mapper的输出是Combiner的输入,Combiner的输出是Reducer的输入. 例如获取历年的最高温度例子,以书中所说的1950年为例,在两个不同分区上的Mapper计算获得的结果分别如下: 第一个Mapper结果:(1950, [0, 10, 20]) 第二个Mapper结果:(19

一起学Hadoop——使用自定义Partition实现hadoop部分排序

排序在很多业务场景都要用到,今天本文介绍如何借助于自定义Partition类实现hadoop部分排序.本文还是使用java和python实现排序代码. 1.部分排序. 部分排序就是在每个文件中都是有序的,和其他文件没有关系,其实很多业务场景就需要到部分排序,而不需要全局排序.例如,有个水果电商网站,要对每个月的水果的销量进行排序,我们可以把reduce进程之后的文件分成12份,对应1到12月份.每个文件按照水果的销量从高到底排序,1月份的排序和其他月份的排序没有任何关系. 原始数据如下,有三个字

Hadoop辅助排序样例一

1. 样例数据 011990-99999 SIHCCAJAVRI 012650-99999 TYNSET-HANSMOEN 012650-99999 194903241200 111 012650-99999 194903241800 78 011990-99999 195005150700 0 011990-99999 195005151200 22 011990-99999 195005151800 -11 2. 需求 3. 思路.代码 将气象站ID相同的气象站信息和天气信息交由同一个 Re

hadoop +streaming 排序总结

参考http://blog.csdn.net/baidu_zhongce/article/details/49210787 hadoop用于对key的排序和分桶的设置选项比较多,在公司中主要以KeyFieldBasePartitioner和KeyFieldBaseComparator被hadoop用户广泛使用. 基本概念: partition:分桶过程,用户输出的key经过partition分发到不同的reduce里,因而partitioner就是分桶器,一般使用平台默认的hash分桶,也可以用

Hadoop辅助排序样例二

1. 需求 求每年的最高温度 2. 样例数据 1995 10 1996 11 1995 16 1995 22 1996 26 1995 3 1996 7 1996 10 1996 20 1996 33 1995 21 1996 9 1995 31 1995 -13 1995 22 1997 -2 1997 28 1997 15 1995 8 3. 思路.代码 将记录按年份分组并按温度降序排序,然后才将同一年份的所有记录送到一个 reducer 组,则各组的首条记录就是这一年的最高温度.实现此方案

Hadoop中的Combiner实践

Combiner作用是合并Mapper的输出,Combiner的输出作为Reducer的输入,这样可以减少map任务和reducer任务之间的数据传输. 1.在Job中设置Combiner和不设置Combiner,观察Reducer输入情况 使用如下代码设置Combiner job.setCombinerClass(MaxTemperatureReducer.class); @Override public int run(String[] args) throws Exception { Jo

9.2.2 hadoop全排序实例详解

1.1.1         全排序 (1)全排序概述 指的是让所有的输出结果都是有序的,最简单的方法就是用一个reduce任务,但是这样处理大型文件时效率极低,失去的并行架构的意义.所以可以采用分组排序的方法来实现全局排序,例如现在要实现按键的全局的排序,可以将键值按照取值范围分为n个分组,<-10℃,-10℃~0℃, 0℃~10℃,>10℃.实现partitioner类,创建4个分区,将温度按照取值范围分类到四个分区中,每个分区进行排序,然后将4个分区结果合并成一个,既是一个全局有序的输出.