[转]hive中order by,distribute by,sort by,cluster by

转至http://my.oschina.net/repine/blog/296562

order by,distribute by,sort by,cluster by  查询使用说明 

1
2
3
4
5
6
7
8
9
10
11
12
13
// 根据年份和气温对气象数据进行排序,以确保所有具有相同年份的行最终都在一个reducer分区中
 
// 一个reduce(海量数据,速度很慢)
select year, temperature
order by year asc, temperature desc
limit 100; 
 
 
// 多个reduce(海量数据,速度很快)
select year, temperature 
distribute by year 
sort by year asc, temperature desc
limit 100;




order by  (全局排序 ) 
order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序) 
只有一个reducer,会导致当输入规模较大时,需要较长的计算时间。 

在hive.mapred.mode=strict模式下,强制必须添加limit限制,这么做的目的是减少reducer数据规模 
例如,当限制limit 100时, 如果map的个数为50, 则reducer的输入规模为100*50 



distribute by  (类似于分桶) 
根据distribute by指定的字段对数据进行划分到不同的输出reduce 文件中。 


sort by   (类似于桶内排序) 
sort by不是全局排序,其在数据进入reducer前完成排序。 
因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1, 则sort by只保证每个reducer的输出有序,不保证全局有序。 



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

因此,常常认为cluster by = distribute by + sort by 

原文地址:https://www.cnblogs.com/rcfeng/p/4502663.html