分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合理 的分区。对于一张表或者分区,Hive 可以进一步组织成桶,也就是更为细粒度的数据范围 划分。
分桶是将数据集分解成更容易管理的若干部分的另一个技术。 分区针对的是数据的存储路径;分桶针对的是数据文件。
100 ss1
1 ss2
100 ss3
2 ss4
100 ss5
3 ss6
100 ss7
4 ss8
100 ss9
5 ss10
100 ss1
hive (default)> create table car_bucket(id int, name string) clustered by(id)
into 4 buckets
row format delimited fields terminated by '\t';
hive (default)> desc formatted car_bucket;
Num Buckets: 4
hive (default)> load data inpath '/car.txt' into table car_bucket;
根据结果可知:Hive 的分桶采用对分桶字段的值进行哈希,然后除以桶的个数求余的方 式决定该条记录存放在哪个桶当中
hive(default)>insert into table car_bucket select * from car_tmp;