hive全局排序和局部排序

文章引自:https://blog.csdn.net/weixin_38629422/article/details/109745613

  1. select * from student order by age;
  2.  
    select * from student sort by age;
  3.  
    select * from student distribute by age;
  4.  
    select * from student cluster by age;
  • order by:全局排序!最终使用一个reducetask来完成排序,就算设置了很多个也没用,如果数据量很大使用order by是不明智的
  • sort by:局部排序,假设使用多个reduce task运行,每个reduce task输出的结果是有序的。所有的结果放到一起是无序的

经典需求:数据量大,不能用orderby进行全局排序,但是需求就是要全局排序

思路:参照hbase的设计  范围分区+局部有序  (distribute by sort by +指定范围)

通用技巧:采样就能知道数据的分不规律!能确定界限

如果采样了1G的数据,想分成10个分区

1.从0读到100M的时候,把第100m位置的那条记录,分桶字段拿出来

2.100M-200M的区间范围

...

一定能确定每个区间的分桶字段的起始方位 

如何做采样

  1. 分桶之后采样(100条,100M或者5%)
  2. rand()的值是在0~1之间  select * from student sort by rand()-0.5 limit 100;
  1.  
    set mapreduce.job.reduces=3;
  2.  
    select * from student distribute by (case when age>20 then 0 when age > 18 then 1 else 2 end) sort by age desc;
  • distribute by :分桶查询,条件:必须设置reduce的个数 set mapreduce.job.reduces=4;   查询中必须设置distribute by 设置分桶规则, 默认是hash 散列
  • cluster by : 如果sort by 和distribute by 的字段一致就可以设置cluster by
  1.  
    cluster by id = distribute by age sort by age
  2.  
    set mapreduce.job.reduces=4
  3.  
    select * from student cluster by id;

结果得到了四段有序的结果集,分区是按照数值/分区数,余数相同的为同一个分区

原文地址:https://www.cnblogs.com/zhipeng-wang/p/14215074.html