hive的分桶表
分区表:
分区表是将数据分文件夹管理 , 减少数据扫描的文件范围 直接从对应文件夹中读取数据
分桶表:
对join 对查询的优化 将数据按照指定的字段的规则分文件
------------user表------------
1001 ss1
1002 ss2
1003 ss3
1004 ss4
1005 ss5
1006 ss6
1007 ss7
1008 ss8
1009 ss9
1010 ss10
1011 ss11
1012 ss12
1013 ss13
1014 ss14
1015 ss15
1016 ss16
分桶表步骤:
1 创建普通表 导入数据
2 创建分桶表
create table buck_stu(
id int,
name string)
clustered by(id)
into 3 buckets
row format delimited fields terminated by '\t';
查看表结构
desc formatted buck_demo;
3 开启分桶功能
set hive.enforce.bucketing=true; – 开启分桶 set
mapreduce.job.reduces=-1;
4 使用insert into的方式导入数据 到 分桶表中
create table buck_stu( id int, name string) clustered by(id) into 3
buckets row format delimited fields terminated by ‘\t’;
分桶表抽样查询
select * from buck_stu tablesample(bucket 1 out of 3 on id); ------随机取三分之一数据-----
select * from buck_stu tablesample(bucket 2 out of 3 on id);
select * from buck_stu tablesample(bucket 3 out of 3 on id);