zoukankan      html  css  js  c++  java
  • Hive中order by,sort by,distribute by,cluster by的区别

    一:order by

    order by会对输入做全局排序,因此只有一个Reducer(多个Reducer无法保证全局有序),然而只有一个Reducer,会导致当输入规模较大时,消耗较长的计算时间。关于order by的详细介绍请参考这篇文章:Hive Order by操作

    二:sort by

    sort by不是全局排序,其在数据进入reducer前完成排序,因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1,则sort by只会保证每个reducer的输出有序,并不保证全局有序。sort by不同于order by,它不受Hive.mapred.mode属性的影响,sort by的数据只能保证在同一个reduce中的数据可以按指定字段排序。使用sort by你可以指定执行的reduce个数(通过set mapred.reduce.tasks=n来指定),对输出的数据再执行归并排序,即可得到全部结果。

    三:distribute by

    distribute by是控制在map端如何拆分数据给reduce端的。hive会根据distribute by后面列,对应reduce的个数进行分发,默认是采用hash算法。sort by为每个reduce产生一个排序文件。在有些情况下,你需要控制某个特定行应该到哪个reducer,这通常是为了进行后续的聚集操作。distribute by刚好可以做这件事。因此,distribute by经常和sort by配合使用。

    注:Distribute by和sort by的使用场景

    1.Map输出的文件大小不均。

    2.Reduce输出文件大小不均。

    3.小文件过多。

    4.文件超大。

    四:cluster by

    cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒叙排序,不能指定排序规则为ASC或者DESC。

    示例:

    #sort by

    [java] view plain copy
     
    1. hive (hive)> select * from user;  
    2. OK  
    3. id  name  
    4. 1   lavimer  
    5. 2   liaozhongmin  
    6. 3   liaozemin  

    使用sort by按id降序排列:

    [java] view plain copy
     
    1. hive (hive)> select * from user sort by id desc;  
    2. //MapReduce...  
    3. Execution completed successfully  
    4. Mapred Local Task Succeeded . Convert the Join into MapJoin  
    5. OK  
    6. id  name  
    7. 3   liaozemin  
    8. 2   liaozhongmin  
    9. 1   lavimer  
    10. Time taken: 3.828 seconds  



    #distribute by

    [java] view plain copy
     
    1. hive (hive)> select * from user;  
    2. OK  
    3. id  name  
    4. 1   lavimer  
    5. 2   liaozhongmin  
    6. 3   liaozemin  
    7. 100 hello  
    8. 200 hadoop  

    #设置reduce的个数

    [java] view plain copy
     
    1. hive (hive)> set mapred.reduce.tasks=2;  
    2. hive (hive)> set mapred.reduce.tasks;    
    3. mapred.reduce.tasks=2  


    #使用带distribute by的数据从user表中导出数据

    [java] view plain copy
     
    1. hive (hive)> insert overwrite local directory '/usr/local/src/user.txt' select * from user distribute by id;  
    2. //MapReduce...  
    3. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 2  

    注:从上述语句执行过程可以看到启动了两个Reducer。

    #导出到本地的数据

    [java] view plain copy
     
    1. [root@liaozhongmin5 src]# cd user.txt/  
    2. [root@liaozhongmin5 user.txt]# ll  
    3. 总用量 8  
    4. -rwxrwxrwx. 1 root root 36 1月  30 14:35 000000_0  
    5. -rwxrwxrwx. 1 root root 22 1月  30 14:35 000001_0  
    6. [root@liaozhongmin5 user.txt]# more 000000_0   
    7. 2<span style="white-space:pre">  </span>liaozhongmin  
    8. 100<span style="white-space:pre"> </span>hello  
    9. 200<span style="white-space:pre"> </span>hadoop  
    10. [root@liaozhongmin5 user.txt]# more 000001_0   
    11. 1<span style="white-space:pre">  </span>lavimer  
    12. 3<span style="white-space:pre">  </span>liaozemin  
    13. [root@liaozhongmin5 user.txt]#   

    注:从上述结果中,我们可以看到数据被分发到了两个Reducer中处理。


    #distribute by和sort by结合使用

    [java] view plain copy
     
    1. hive (hive)> select * from temperature;  
    2. OK  
    3. year    tempra  
    4. 2008    30`C  
    5. 2008    35`C  
    6. 2008    32.5`C  
    7. 2008    31.5`C  
    8. 2008    31`C  
    9. 2015    41`C  
    10. 2015    39`C  
    11. 2015    36`C  
    12. 2015    33`C  
    13. 2015    35`C  
    14. 2015    37`C  

    #根据年份和气温对气象数据进行排序,以确保所具有相同年份的行最终都在一个reduce分区中。

    [java] view plain copy
     
      1. hive (hive)> select * from temperature distribute by year sort by year asc,tempra desc;  
      2. //MapReduce...  
      3. Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 2  
      4. //MapReduce...  
      5. OK  
      6. year    tempra  
      7. 2008    35`C  
      8. 2008    32.5`C  
      9. 2008    31`C  
      10. 2008    31.5`C  
      11. 2008    30`C  
      12. 2015    41`C  
      13. 2015    39`C  
      14. 2015    37`C  
      15. 2015    36`C  
      16. 2015    35`C  
      17. 2015    33`C  
      18. Time taken: 17.358 seconds  
  • 相关阅读:
    老树新芽,在ES6下使用Express
    Swift3翻天覆地的改变
    NodeJs回调操作Promise化
    Node的关系型数据库ORM库:bookshelf
    基于Node的PetShop,RESTful API以及认证
    基于Node的PetShop,oauth2认证RESTful API
    Mongoose轻松搞定MongoDB,不要回调!
    Thymeleaf常用语法:表达式语法之运算符
    Thymeleaf对象的使用:日期对象
    Thymeleaf对象的使用:字符串对象
  • 原文地址:https://www.cnblogs.com/yjd_hycf_space/p/6801385.html
Copyright © 2011-2022 走看看