Hive分桶操作(Bucket)一图掌握核心内容

什么是分桶?:

Hive基于hash值对数据进行分桶,按照分桶字段的hash值除以分桶的个数进行取余(bucket_id = column.hashcode % bucket.num)。

分桶的作用:

1、有更高的查询处理效率
2、使得抽样更高效

如何分桶?:

1、分桶之前需要执行命令set hive.enforce.bucketing=true;

2、创建分桶表
首先先创建一个普通表用于给分桶表传数据

create table employee_id(
name string,
employee_id int,
work_place array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,array<string>>
)
row format delimited fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

分桶表创建:

create table employee_id_buckets(
name string,
employee_id int,
work_place array<string>,
gender_age struct<gender:string,age:int>,
skills_score map<string,int>,
depart_title map<string,array<string>>
)
#创建两个桶
clustered by(employee_id) into 2 buckets
row format delimited fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

执行命令set map.reduce.tasks=2;

将employee_id数据写入到分桶表employee_id_buckets。
insert overwrite table employee_id_buckets select * from employee_id;
分桶表创建完成。

一图看懂分桶核心操作:

分完桶之后,要去查看数据,命令为(注意关键字是tablesample)
select * from employee_id_buckets tablesample(bucket 1 out of 4 on employee_id)s;

核心代码:bucket X out of Y on employee_id
上面我们创建分桶时是创建了2个桶,这边的Y必须是创建的桶数的因子或者是整数倍,也就是说Y%2==0。
X指的是查询Y中第几个的桶的数据。接下来上图片!!

在这里插入图片描述
图片些许有点潦草,但是大概能看懂意思。
这边Y是4,也就是2的2倍,但是我们分桶是分了2个桶,所以我们这边一块数据就是1/2,按照顺序排列开来就是A1,B1,A2,B2,这边X是1,所以查询的是A1的数据,也就是A桶的一半的数据。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万家林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值