导航
- hive outline
- 分桶表 分区表 区别与联系
- 创建分桶表
- 分桶表的优点
hive outline
链接
分桶表 分区表 区别与联系
- 分区针对的是
数据的存储路径
(分区是根据表的某一列得到的,分区不同,数据存储的文件夹不同);分桶针对的是数据文件
(分桶是根据表的某一列下数据值,经hash取余得到的) - 分区字段
不能是
表中已经存在的字段;分桶的字段必须是
表中已经存在的字段 - 2者都是hive的一种优化手段,为了提高查询效率
创建分桶表
- 写sql
(1)设置桶的个数为4
个
(2)按照id字段进行分桶
注意:
分桶表数据的填充,要借助其他表来完成(从其它表中查询数据,然后根据分桶规则,将查询过来的数据放置在不同桶中)
- 在建一个普通的stu表
这个普通表的字段必须和分桶表的字段保持一致
- 向普通表中导入数据
stu_buck.txt
- 打开分桶属性
v3.1.2之前需要设置以下参数
- 数据通过子查询的方式导入分桶表
web页面查看数据文件的存储形式
分桶表的优点
- 基于分桶字段查询时,减少全表扫描
- JOIN时可以提高MR程序效率,减少笛卡尔积数量
- 分桶表数据可以进行高效抽样