大数据之Hive:DML数据操作(八)

1 .分区排序(Distribute By)
Distribute By: 控制某个特定行到哪个reducer。distribute by类似MR中partition(自定义分区),进行分区,结合sort by使用。
案例实操:
(1)先按照部门编号分区,再按照员工编号降序排序。

hive (default)> set mapreduce.job.reduces=3;
hive (default)> insert overwrite local directory '/opt/module/datas/distribute-result' select * from emp distribute by deptno sort by empno desc;

注意:
1).distribute by的分区规则是根据分区字段的hash码与reduce的个数进行模除后,余数相同的分到一个区。
2).Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。
2. Cluster By
当distribute by和sorts by字段相同时,可以使用cluster by方式。
cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是升序排序,不能指定排序规则为ASC或者DESC。
1)以下两种写法等价

hive (default)> select * from emp cluster by deptno;
hive (default)> select * from emp distribute by deptno sort by deptno;

猜你喜欢

转载自blog.csdn.net/weixin_43597208/article/details/112596432