1.order by 全局排序
1,a
4,b
5,b
7,c
3,a
2,a
8,e
6,c
create table tb_sort(
id int,
name string
)
row format delimited fields terminated by ',';
load data local inpath '/data/x' into table tb_sort;
select * from tb_sort order by id;
全局排序,查询结果慢
2.distribute by 分区排序(指定分区字段)
类似MR中partition,进行分区,结合sort by使用,否则结果并没有排序。
注意,Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。
select
*
from
tb_sort
distribute by id
sort by name;
3.sort by(对每个mapreduce内部进行排序)
1.设置reduce个数
set mapreduce.job.reduces=3;
2.sort by 排序
select
*
from
tb_sort
sort by
id;
4.cluster by
当分区字段和区内排序字段相同 并且是升序的时候使用cluster by,
相当于distribute by 字段 sort by 字段 合起来,但cluster by 不能用降序排列.
cluster by id 就相当于 distribute by id sort by id