Distribute By: 在有些情況下,我們需要控制某個特定行應該到哪個reducer,通常是為了進行后續的聚集操作。distribute by子句可以做這件事。distribute by類似MR中partition(自定義分區),進行分區,結合sort by使用。
對於distribute by進行測試,一定要分配多reduce進行處理,否則無法看到distribute 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語句之前。