导航

  • ​​hive outline​​
  • ​​分桶表 分区表 区别与联系​​
  • ​​创建分桶表​​
  • ​​分桶表的优点​​

hive outline

​​链接​​

分桶表 分区表 区别与联系

  1. 分区针对的是​​数据的存储路径​​(分区是根据表的某一列得到的,分区不同,数据存储的文件夹不同);分桶针对的是​​数据文件​​(分桶是根据表的某一列下数据值,经hash取余得到的)
  2. 分区字段​​不能是​​表中已经存在的字段;分桶的字段​​必须是​​表中已经存在的字段
  3. 2者都是hive的一种优化手段,为了提高查询效率

创建分桶表

  1. 写sql

(1)设置桶的个数为​​4​​​个
(2)按照id字段进行分桶

create table stu_buck(id int, name string)
clustered by(id) into 4 buckets
row format delimited fields terminated by '\t';

​注意:​​ 分桶表数据的填充,要借助其他表来完成(从其它表中查询数据,然后根据分桶规则,将查询过来的数据放置在不同桶中)

  1. 在建一个普通的stu表

​这个普通表的字段必须和分桶表的字段保持一致​

create table stu(id int, name string)
row format delimited fields terminated by '\t';
  1. 向普通表中导入数据
    ​​​stu_buck.txt​
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
load data local inpath '/opt/modules/input/stu_buck.txt' into table stu;
  1. 打开分桶属性

​v3.1.2之前需要设置以下参数​

#开启分桶
hive (default)> set hive.enforce.bucketing=true;
  1. 数据通过子查询的方式导入分桶表
insert into table stu_buck
select id, name from stu;

​web页面查看数据文件的存储形式​

hive 分桶表_hive

分桶表的优点

  1. 基于分桶字段查询时,减少全表扫描
  2. JOIN时可以提高MR程序效率,减少笛卡尔积数量
  3. hive 分桶表_hive_02

  4. 分桶表数据可以进行高效抽样