Hive中的排序

全局排序Order By

​ 全局排序,只有一个reducer

​ 查看有多少个reducer的命令:set mapreduce.job.reduces;发现他的之值是-1.-1是动态变化的,当知道使用的身世orderby的时候,会把-1置为1.

每个Reducer内部的排序 Sort By

​ 使用sort by需要指定reduce的个数 set mapreduce.job.reduces = 3;

Hive中的排序(order by,sort by,distribute by,cluster by)

我们在来看看是不是生成了三个文件

insert overwrite local directory ‘/opt/module/hive/datas/sortby-result’

select * from emp sort by sal;

这样看起来很乱,自动挑选的字段进行分区,想要让分区有意义,我们指定分区字段进行分区.引入了distribute by

Distribute By:控制某个特定行应该到reducer,hive粒度比较粗值得就是这个

distribute By类似于mr中的partitioner,但是没有mr中的partition灵活,因为在mr中可以自定义分区,而在这里只能按照默认的分区器,hash partitioner

select * from emp distribute by deptno sort by sal;

Hive中的排序(order by,sort by,distribute by,cluster by)

验证是不是,同样的方式,还是要去看一看有多少个文件

insert overwrite local directory ‘/opt/module/hive/datas/sortby-result’

select * from emp distribute by deptno sort by sal;
Hive中的排序(order by,sort by,distribute by,cluster by)

支持使用别名:

select *,deptno ddd from emp distribute by ddd sort by sal;

Cluster By(鸡肋,因为不支持正序倒叙排序)

当distributeby字段相同的时候,可以使用clusterby的方式

clusterby除了具有distributeby的功能之外还有sortby的功能.但是排序只能是升序排序,不能指定排序规则为asc或者desc

select * from emp distribute by deptno sort by deptno;

等同于上面的结果

select * from emp cluster by deptno;

Hive中的排序(order by,sort by,distribute by,cluster by)

相关文章: