一、建表:分區(qū)分桶表,對(duì)日期分區(qū),再對(duì)id分4個(gè)桶
create table t1(id int)
partitioned by (statis_date string)
clustered by(id)
into 4 buckets;
二、設(shè)置強(qiáng)制分桶
set hive.enforce.bucketing=true;
三、執(zhí)行插入語句,插入1到8這幾個(gè)id
insert into t1 partition(statis_date='20211101')
select * from (
select 1 id union all
select 2 id union all
select 3 id union all
select 4 id union all
select 5 id union all
select 6 id union all
select 7 id union all
select 8 id ) tmp
cluster by id;
四、效果


五、表抽樣
-- 語法:
select columns from table tablesample(bucket x out of y on column);
-- x:表示從第幾個(gè)分桶進(jìn)行抽樣
-- y:表示每隔幾個(gè)分桶取一個(gè)分桶,必須為y的整數(shù)倍或者因子
例如下面從對(duì)表從桶1開始查,每次間隔1個(gè)桶,得到桶1和桶3的全部數(shù)據(jù):
select id,statis_date from t1 tablesample(bucket 1 out of 2 on id);

六、作用
1、抽樣查詢
2、map-side join,兩個(gè)對(duì)相同字段做了同樣分桶規(guī)則的表關(guān)聯(lián),可以實(shí)現(xiàn)在map端join,提高效率。
3、控制文件數(shù)量