hive的几种排序比较

迈不过友情╰ 2023-10-04 17:40 120阅读 0赞

内容目录

    • hive的几种排序
      • 准备数据
      • 一、order by
      • 二、sort by
      • 三、distribute by
      • 四、cluster by

hive的几种排序

准备数据

1、创建一个sort.txt文件,输入内容

  1. tom,chinese,100
  2. tom,math,90
  3. tom,english,95
  4. tom,history,88
  5. tom,chirst,98
  6. jery,chinese,90
  7. jery,math,92
  8. jery,english,95
  9. jery,history,90
  10. jery,chirst,89
  11. may,chinese,90
  12. may,math,89
  13. may,english,85
  14. may,history,98
  15. may,chirst,91

2、创建表

  1. create table sort_test(
  2. name string,
  3. major string,
  4. score int
  5. )
  6. row format delimited
  7. fields terminated by ",";

3、加载数据

  1. load data local inpath '/opt/module/datas/sort.txt' into table sort_test;

一、order by

全局排序,只有一个reduce,会把全局数据做个排序

  1. select *
  2. from sort_test
  3. order by score desc;
  4. tom chinese 100
  5. tom chirst 98
  6. may history 98
  7. tom english 95
  8. jery english 95
  9. jery math 92
  10. may chirst 91
  11. tom math 90
  12. jery chinese 90
  13. jery history 90
  14. may chinese 90
  15. jery chirst 89
  16. may math 89
  17. tom history 88
  18. may english 85

二、sort by

对于大规模的数据集 order by 的效率非常低。在很多情况下,并不需要全局排 序,此时可以使用 sort by

注意,是对每一个得reduce得数据进行排序,如果只有一个reduce,那么效果和order by是没有差别得

  1. select *
  2. from sort_test
  3. sort by score desc;
  4. tom chinese 100
  5. tom chirst 98
  6. tom english 95
  7. jery english 95
  8. may chinese 90
  9. jery chirst 89
  10. may math 89
  11. tom history 88
  12. may english 85
  13. may history 98
  14. jery math 92
  15. may chirst 91
  16. tom math 90
  17. jery chinese 90
  18. jery history 90

三、distribute by

在有些情况下,我们需要控制某个特定行应该到哪个 reducer,通常是为 了进行后续的聚集操作。distribute by 子句可以做这件事。distribute by 类似 MR 中 partition (自定义分区),进行分区,结合 sort by 使用。

  1. select *
  2. from sort_test
  3. distribute by name
  4. sort by score desc;
  5. tom chinese 100
  6. tom chirst 98
  7. may history 98
  8. tom english 95
  9. jery english 95
  10. jery math 92
  11. may chirst 91
  12. tom math 90
  13. jery chinese 90
  14. jery history 90
  15. may chinese 90
  16. jery chirst 89
  17. may math 89
  18. tom history 88
  19. may english 85

好像没有什么区别,为啥呢,因为这个分区啊,得设置多个reduce才行

  1. set mapreduce.job.reduces=3;
  2. may history 98
  3. may chirst 91
  4. may chinese 90
  5. may math 89
  6. may english 85
  7. tom chinese 100
  8. tom chirst 98
  9. tom english 95
  10. jery english 95
  11. jery math 92
  12. tom math 90
  13. jery chinese 90
  14. jery history 90
  15. jery chirst 89
  16. tom history 88

写到文件中看一看

  1. insert overwrite local directory
  2. '/opt/module/hive/datas/distribute-sort'
  3. select *
  4. from sort_test
  5. distribute by name
  6. sort by score desc;

四、cluster by

  1. select *
  2. from sort_test
  3. cluster by major;

当你得分区字段和排序字段是一样得,就可以使用cluster by,注意,cluster by只能有正序,不能有倒序

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读

    相关 简单排序比较

    一般来说:冒泡排序用的最少,它的应用场合是只有数据量很小的时候才会有一些引用价值。 选择排序把交换次数降到最低,但是他的比较次数仍然很大,每次轮到的值保存下来,然后需要和右边

    相关 缓存比较

    几种缓存比较 1、memcached 协议简单、基于libevent的事件处理、内置内存存储方式、memcached不互相通信的分布式。 各个memcached不会

    相关 排序算法稳定性比较

    排序算法的稳定性,通俗地讲就是能保证排序前2个相等的数其在序列的前后位置顺序和排序后它们两个的前后位置顺序相同。例如,如果Ai=Aj,原来在位置前,排序后Ai还是要在Aj位置前