一、全局排序(Order By)
ASC:升序(默认)
DESC:降序
例如:按照部门和工资升序排序
hive > select ename, deptno, sal from emp order by deptno, sal ;
提示:先按deptno排序,如果一样了按着sal排序
二、内部排序(Sort By)
Sort By:每个MapReduce内部进行排序,对全局结果集来说不是排序。
1.设置reduce的个数
hive > set mapreduce.job.reduces=3;//默认的话reduce个数为1
2.查看reduce的个数
hive > set mapreduce.job.reduces;
3. 根据部门编号降序查看员工信息
hive > select * from emp sort by empno desc;
4. 将查询结果导入到文件中(按照部门编号降序排序)
hive )> insert overwrite local directory '/usr/data/sortby-result'
select * from emp sort by deptno desc;
三、分区排序(Distribute By)
1. Distribute By:类似MR中partition,进行分区,结合sort by使用。
注意,Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。
对于distribute by进行测试,一定要分配多reduce进行处理,否则无法看到distribute by的效果。 所以第一步要先更改reduce的数量
当distribute by和sorts by字段相同时,可以使用cluster by方式。
2.Cluster By
cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。
1)以下两种写法等价
hive > select * from emp cluster by deptno;//只能倒叙排列
hive > select * from emp distribute by deptno sort by deptno;//默认倒叙排列