Hive之分桶表

1. Hive分桶表

简介

桶是比表或分区更为细粒度的数据范围划分。针对某一列进行桶的组织,对列值哈希,然后除以桶的个数求余,决定将该条记录存放到哪个桶中。

  • 获得更高的查询处理效果
  • 抽样调查

创建分桶表

create table bucketed_user
(id int ,name string) clustered by (id)  into 4 buckets
stored as orc;

添加数据前需要先开启分桶

set hive.enforce.bucketing=true;

 导入数据

insert into table bucketed_user select user_id,order_id from orders;

查询数据

select * from bucketed_user tablesample(bucket 1 out of 16 on id) limit 50;
tablesample(bucket x out of y)
x:表示从第几桶开始抽数据(1,2,3,4)
y:表示抽数据的比例,是抽数据的分母
比如: 有4个分桶
tablesample(bucket 1 out of 16)  表示从第一桶开始抽数据,抽取第一桶数据的比例为(4(桶数)/16(分母))=1/4,抽取第一桶四分之一的数据
tablesample(bucket 2 out of 32)  表示从第二桶开始抽数据,抽取第二桶数据的比例为(4(桶数)/32(分母))=1/8,抽取第一桶八分之一的数据
posted @ 2018-06-19 12:02  雪山过客  阅读(3007)  评论(0编辑  收藏  举报