Order, Sort, Cluster, and Distribute By

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy

1、order by
  1. 如果在strict 模式下,使用order by必须加limit

    **原因:**使用了order by之后,会有一个reducer进行结果排序,如果数据量比较大,则会需要很长时间

  2. 如果order by列中存在null值,且排序方式为asc时,null值会被排在最前面

  3. 在Hive 3.0.0以后的版本,如果在子查询中使用了order by但是没有加limit的话,则优化器会将order by操作进行删除

  4. order by作用时机为reduce的时候,所以能保证最终结果的有序性

2、sort by
  1. sort by的时机为reduce之前,作用与order by类似,但是只能保证分区内数据的有序性。如果sort by作用的列为数字类型的,则根据数字进行排序,否则根据字典顺序进行排序
  2. 在Hive 3.0.0以后的版本,如果在子查询中使用了sort by但是没有加limit的话,则优化器会将sort by操作进行删除
3、distribute by
  1. distribute为分发的意思,即根据dsitribute by的列进行分发,相同值的数据一定在同一个reducer上
4、 cluster by
  1. cluster by = distribute by + sort by,先根据列A进行reduce,再根据列B进行sort
  2. 如果reduce和sort的列为同一列,则可以使用cluster by

3、LanguageManual Transform

标签: order、sort、distribute、cluster、reduce、面试
  • 回复
隐藏