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;

結果得到了四段有序的結果集,分區是按照數值/分區數,余數相同的為同一個分區


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM