HIVE排序(全局排序、内部排序、分区排序)

本文介绍了Hive中的三种排序方法:OrderBy用于全局排序,SortBy在MapReduce内部排序,DistributeBy与SortBy结合用于分区排序,而ClusterBy则提供额外的倒序排序功能。
摘要由CSDN通过智能技术生成

一、全局排序(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;//默认倒叙排列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值