hive中的四种排序类型

1、Order by 全局排序

ASC(ascend): 升序(默认)
DESC(descend): 降序

**注意 :**只有一个 Reducer,即使我们在设置set reducer的数量为多个,但是在执行了order by语句之后,当前此次的运算还是只有1个reducer,因为order by要做的是全局分区。(开窗函数内是分区内排序,不在此问题)

验证:

sql 复制代码
set mapreduce.job.reduces=3;
select * from stu_scores order by math;

**总结:**order by 语句的运行效率较低,一般要配合limit 使用。

2、Sort By 在每一个Reduce的job中进行排序

设置reduce 的job数为3

sql 复制代码
set mapreduce.job.reduces=3;

查看设置的reduce 的job数量

sql 复制代码
set mapreduce.job.reduces

测试sort by:

sql 复制代码
select * from stu_scores sort by math;

**备注:**在3个reduce job中分别进行排序。

3、Distribute By 分区(结合 sort by 使用)

有些场景我们需要控制某些特定行应该到同一reducer,做一些聚集操作。

distribute by 类似 MR 中 partition(自定义分区),进行分区,结合 sort by 使用。

设置reduce 的job数为3

sql 复制代码
set mapreduce.job.reduces=3;

查看设置的reduce 的job数量

sql 复制代码
set mapreduce.job.reduces

测试 distribute by....sort by

按照stu_id分区,分区内使用math排序

sql 复制代码
select * from stu_scores distribute by stu_id sort by math;

分区逻辑:根据distribute by 后的字段hash码与reduce 的个数进行模数后,决定分区路由。

4、cluster by

当 distribute by 和 sort by 字段相同时,可以使用 cluster by 方式。但是排序只能是升序排序,不能指定排序规则为 ASC 或者 DESC。

sql 复制代码
select * from stu_scores cluster by math;
select * from stu_scores distribute by math sort by math;

总结: cluster by 等价于distribute by 和 sort by 字段的升序排序。

相关推荐
一张假钞5 小时前
Spark SQL读写Hive Table部署
hive·sql·spark
想做富婆6 小时前
Hive:窗口函数[ntile, first_value,row_number() ,rank(),dens_rank()]和自定义函数
数据仓库·hive·hadoop
好记性+烂笔头13 小时前
4 Hadoop 面试真题
大数据·hadoop·面试
B站计算机毕业设计超人1 天前
计算机毕业设计Python+CNN卷积神经网络考研院校推荐系统 考研分数线预测 考研推荐系统 考研爬虫 考研大数据 Hadoop 大数据毕设 机器学习
hadoop·python·机器学习·spark·网络爬虫·课程设计·数据可视化
字节全栈_rJF2 天前
Hive 整合 Spark 全教程 (Hive on Spark)
hive·hadoop·spark
好记性+烂笔头2 天前
2 MapReduce
大数据·hadoop·mapreduce
字节全栈_ZKt2 天前
Hadoop集群中Hbase的介绍、安装、使用_root@master001 hadoop]# start-hbase
大数据·hadoop·hbase
L_M_TY2 天前
E. Correct Placement
算法·贪心·排序·双指针
一张假钞2 天前
Sqoop源码修改:增加落地HDFS文件数与MapTask数量一致性检查
java·hadoop·hdfs·sqoop
weixin_307779132 天前
设计转换Apache Hive的HQL语句为Snowflake SQL语句的Python程序方法
数据仓库·hive·python·sql