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,抽取第一桶八分之一的數據