hive分桶及相关概念

#创建分桶表
create table stu_buck(sno int,sname string,sex string,sage int,sdept string)
clustered by(sno) 
sorted by(sno DESC)
into 4 buckets
row format delimited
fields terminated by ',';


#设置变量,设置分桶为true, 设置reduce数量是分桶的数量个数
set hive.enforce.bucketing = true;
set mapreduce.job.reduces=4;


#开会往创建的分通表插入数据(插入数据需要是已分桶, 且排序的)
#可以使用distribute by(sno) sort by(sno asc)   或是排序和分桶的字段相同的时候使用Cluster by(字段)
#注意使用cluster by  就等同于分桶+排序(sort)
insert into table stu_buck

select sno,sname,sex,sage,sdept from student distribute by(sno) sort by(sno asc);


注:1、order by 会对输入做全局排序,因此只有一个reducer,会导致当输入规模较大时,需要较长的计算时间。

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

3、distribute by(字段)根据指定的字段将数据分到不同的reducer,且分发算法是hash散列。

4、Cluster by(字段)除了具备 distribute by的功能外,还会对该字段进行排序。

 

 

因此,如果分桶和sort字段是同一个时,此时,cluster by = distribute by + sort by

分桶表的作用:最大的作用是用来提高join操作的效率;



猜你喜欢

转载自blog.csdn.net/peng_0129/article/details/80744673