1. order by(全局排序)
1. 全局排序, 无论设置多少个 reduce个数,只会产生一个reduce
2. 对大规模的数据集,全局排序效率非常低
1. 设置分区个数为 3
set mapreduce.job.reduces=3;
2. 运行sql
select name
,num
from (
select '张飞' as name,1 as num
union all
select '张飞' as name,2 as num
union all
select '张飞' as name,3 as num
union all
select '赵云' as name,1 as num
union all
select '赵云' as name,2 as num
union all
select '赵云' as name,3 as num
) as t1
order by num desc ;
-- 查看结果(无论 reduceNum 设置为多少,只会产生一个 reduce)
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
name num
赵云 3
张飞 3
赵云 2
张飞 2
赵云 1
张飞 1
View Code
2. sort by (分区内排序)
1. 为每个 reducer 内产生的文件排序
2. 不指定 distribute by 时,按照 行的所有字段的hash值 % numReduces 进行分区
-- 使用步骤
-- 1. 设置 reduce 个数
set mapreduce.job.reduces=3;
insert overwrite local directory '/root/sortby'
select name
,num
from (
select '张飞' as name,1 as num
union all
select '张飞' as name,2 as num
union all
select '张飞' as name,3 as num
union all
select '赵云' as name,1 as num
union all
select '赵云' as name,2 as num
union all
select '赵云' as name,3 as num
) as t1
sort by num desc ;
--结果
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 3
--产生3个 reduce, 且按照 行的所有字段进行分区
-- 查看生产文件(三个分区文件 都有数据)
-rw-r--r-- 1 root root 18 1月 29 11:06 000000_1
-rw-r--r-- 1 root root 27 1月 29 11:06 000001_2
-rw-r--r-- 1 root root 9 1月 29 11:06 000002_1
name num
赵云 3
赵云 2
张飞 3
张飞 2
赵云 1
张飞 1
View Code
3. distribute by(指定 分区字段)
1. 分区编号 = hash(指定分区字段) % reduceNub
insert overwrite local directory '/root/sortbykey'
select name
,num
from (
select '张飞' as name,1 as num
union all
select '张飞' as name,2 as num
union all
select '张飞' as name,3 as num
union all
select '赵云' as name,1 as num
union all
select '赵云' as name,2 as num
union all
select '赵云' as name,3 as num
) as t1
distribute by name
sort by num desc ;
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 3
-rw-r--r-- 1 root root 27 1月 29 11:12 000000_1
-rw-r--r-- 1 root root 0 1月 29 11:12 000001_2 (空文件)
-rw-r--r-- 1 root root 27 1月 29 11:12 000002_0
赵云3
赵云2
赵云1
张飞3
张飞2
张飞1
View Code
4. cluster by(分区内 排序)
1. 当 distribute by 和 sort by 字段相同时, 可以使用cluster by
默认为 升序排序, 不能指定 排序规则 desc 、asc
2. cluster by name = distribute by name sort by name asc ;
insert overwrite local directory '/root/clusterby2'
select name
,num
from (
select '张飞' as name,1 as num
union all
select '张飞' as name,2 as num
union all
select '张飞' as name,3 as num
union all
select '赵云' as name,1 as num
union all
select '赵云' as name,2 as num
union all
select '赵云' as name,3 as num
) as t1
cluster by name ;
name num
赵云 3
赵云 2
赵云 1
张飞 3
张飞 2
张飞 1
View Code
5. mr排序连接: https://www.cnblogs.com/bajiaotai/p/15734910.html