distribute by 类似于mr任务中的自定义分区器;distribute by的规则是根据分区字段的hash码对reducer个数进行模除以后,余数相同的进入同一个分区
局部排序
sort by 每个reducer内部的排序,全局无序,常与distribute by搭配使用,distribute by在前sort by在后
cluster by
当distribute by sort by 一起使用且字段相同升序时可替代,cluster by 不能指定排序规则为 ASC 或者 DESC。
分区,分桶
分桶
分桶必须开启配置 set hive.enforce.bucketing=true;
创建分桶表
create table stu_buck(id int, name string)
clustered by(id)
into 4 buckets
row format delimited fields terminated by '\t';
分桶表插入数据跟正常表插入数据无异
分桶规则
分桶字段求hash模除分桶数取余的方式决定数据在哪个桶中
分桶和分区区别
分区针对的是数据的存储路径即文件夹;分桶针对的是数据文件
分桶抽样调查
语法
TABLESAMPLE(BUCKET x OUT OF y) 。
注:tablesample 是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。
y 必须是 table 总 bucket 数的倍数或者因子。hive 根据 y 的大小,决定抽样的比例。例
如,table 总共分了 4 份,当 y=2 时,抽取(4/2=)2 个 bucket 的数据,当 y=8 时,抽取(4/8=)1/2
个 bucket 的数据。
x 表示从哪个 bucket 开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上
y。例如,table 总 bucket 数为 4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2 个
bucket 的数据,抽取第 1(x)个和第 3(x+y)个 bucket 的数据。
注意:x 的值必须小于等于 y 的值,否则
FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator
in sample clause for table stu_buck