您的位置:首页 > 其它

hive中order by、distribute by、sort by和cluster by的区别和联系

2016-10-10 18:33 495 查看

order by

order by 会对数据进行全局排序,和oracle和mysql等数据库中的order by 效果一样,

它只在一个reduce中进行所以数据量特别大的时候效率非常低。

而且当设置 :set hive.mapred.mode=strict的时候不指定limit,执行select会报错,如下:

LIMIT must also be specified.

sort by

sort by 是单独在各自的reduce中进行排序,所以并不能保证全局有序,一般和distribute by 一起执行,而且distribute by 要写在sort by前面

如果mapred.reduce.tasks=1和order by效果一样,如果大于1会分成几个文件输出每个文件会按照指定的字段排序,而不保证全局有序。

sort by 不受 hive.mapred.mode 是否为strict ,nostrict 的影响

distribute by

用distribute by 会对指定的字段按照hashCode值对reduce的个数取模,然后将任务分配到对应的reduce中去执行

就是在mapreduce程序中的patition分区过程,默认根据指定key.hashCode()&Integer.MAX_VALUE%numReduce 确定处理该任务的reduce

示例如下:

public class myPartitioner extends Partitioner<TextPair, Text>{

@Override
public int getPartition(TextPair key, Text value, int num) {
// TODO Auto-generated method stub

if(num == 0 ){
return 0;
}
int a = (key.getFirst().hashCode()&Integer.MAX_VALUE)%num;
return a;
}

}


Cluster By

distribute by 和 sort by 合用就相当于cluster by,但是cluster by 不能指定排序为asc或 desc 的规则,只能是desc倒序排列。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: