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

来源:互联网 发布:紫金银交易软件 编辑:程序博客网 时间:2024/05/19 09:12

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倒序排列。

1 0
原创粉丝点击