Hive-桶排序

1、概念

对于一张表或者分区,Hive 可以进一步组织成桶,也就是更为细粒度的数据范围划分。

分桶是将数据集分解成更容易管理的若干部分的另一个技术。

分区针对的是数据的存储路径;分桶针对的是数据文件。

2、案例操作

设置强制分桶和reduces任务数为默认

hive (default)> set hive.enforce.bucketing=true;
hive (default)> set mapreduce.job.reduces=-1;

创建分桶表

create table stu_buck(id int, name string)
clustered by(id) 
into 4 buckets
row format delimited fields terminated by '\t';

数据通过子查询的方式导入

//先建一个普通的表
create table stu(id int, name string)
row format delimited fields terminated by '\t';
//向这个表中导入数据
load data local inpath '/opt/module/datas/student.txt' into table stu;

//数据通过子查询的方式导入
insert into table stu_buck
select id, name from stu;

3、分桶规则

Hive的分桶采用对分桶字段的值进行哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中.

4、分桶抽样查询

对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。

查询表stu_buck中的数据:

hive (default)> select * from stu_buck tablesample(bucket 1 out of 4 on id);

tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y)

其中y决定抽样比例,x决定从那个桶开始抽取

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的数据。

扫描二维码关注公众号,回复: 12453095 查看本文章

注意:x的值必须小于等于y的值,否则

FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck

猜你喜欢

转载自blog.csdn.net/QJQJLOVE/article/details/107005657