hive 分桶及抽样调查

1、分桶的概述

分区提供了一个隔离数据和优化查询的遍历方式。不是所有的数据集都可形成合力的分区

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

分区针对的是数据的存储路径(分文件夹)

分桶针对的是数据文件

2、创建分桶表,数据通过子查询的方式导入

(1)创建一个普通表

create table stu(id int, name string)
row format delimited fields terminated by '\t';

(2)向普通表中导入数据

load data local inpath '/opt/module/datas/student.txt' into table stu

(3)创建分桶表

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

(3)通过子查询导入数据到分桶表(直接导入数据到分桶表,不能分桶)

insert into table stu_buck
select id,name from stu;

(4)需要设置属性强制分桶

set hive.enforce.bucketing=true;
set mapreduce.job.reduces=-1;

3、分桶规则

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

4、分桶抽样查询

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

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

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


​ 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
posted @ 2019-10-23 20:26  hyunbar  阅读(806)  评论(0编辑  收藏  举报