hive的分区表设计

前言

用了这么久的Hive,而没有认真的学习和使用过Hive的分区,现在学习记录一下。

  • 分区表一般在数据量比较大,且有明确的分区字段时使用,这样用分区字段作为查询条件查询效率会比较高。
  • Hive分区分为静态分区和动态分区

1、建表语句

先用一个有分区字段的分区表进行学习,静态分区和动态分区的建表语句是一样的。

create table test_partition (
id string comment 'ID', 
name string comment '名字'
)
comment '测试分区'
partitioned by (year int comment '')
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;

2、插入语句

静态分区和动态分区的插入数据的语句是不一样的,所以分开

2.1 静态分区

静态分区是在语句中指定分区字段为某个固定值,多次重复插入数据是为了看看数据如何在hdfs上存储的。

2.1.1 insert into

insert into table test_partition partition(year=2018) values ('001','张三');
insert into table test_partition partition(year=2018) values ('001','张三');
insert into table test_partition partition(year=2018) values ('002','李四');
2.1.2 load data

data.txt

002,李四
003,王五
load data local inpath '/root/dkl/data/data.txt' into table test_partition partition (year =2018);
load data local inpath '/root/dkl/data/data.txt' into table test_partition partition (year =2018);
load data local inpath '/root/dkl/data/data.txt' into table test_partition partition (year =2017);

2.1.3 查询及结果

 
image

2.1.4 hdfs存储形式

 
image

分区2018的路径为

/apps/hive/warehouse/dkl.db/test_partition/year=2018
  • /apps/hive/warehouse 为hive的仓库路径
  • dkl.db dkl为数据库名称
  • test_partition为表名
  • year为分区字段名

2.2 动态分区

2.2.1 insert into

set hive.optimize.sort.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert into table test_partition partition(year) values ('001','张三',2016); 

2.2.2 load data

不能使用load data进行动态分区插入
data.txt

002,李四,2015
003,王五,2014
load data local inpath '/root/dkl/data/data.txt' into table test_partition partition (year);

报错:hive> load data local inpath '/root/dkl/data/data.txt' into table test_partition partition (year);
FAILED: NullPointerException null
 
 

可以使用另一种方法解决

首先创建没有分区的表

create table test (
id string comment 'ID', 
name string comment '名字',
year int comment ''
)
comment '测试'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;

先将数据load进test表

load data local inpath '/root/dkl/data/data.txt' into table test;

然后从表test,动态分区插入test_partition中

insert into table test_partition partition(year)  select * from test;

如果后面select具体字段的话,需要保证顺序一致,把分区字段放在最后。

insert into table test_partition partition(year)  select id,name,year from test;

3、查看分区信息

show  partitions test_partition;
hive> show  partitions test_partition;
OK
year=2017
year=2018
Time taken: 0.719 seconds, Fetched: 2 row(s)

4、添加分区字段

查了一下,不能添加新的分区字段

4.1 添加新分区

alter table test_partition add  partition (year=2012);

这样就会新建对应的hdfs路径下一个year=2012的文件夹

当然也可以指定localtion,这样就不会在默认的路径下建立文件夹了

alter table test_partition add  partition (year=2010) location '/tmp/dkl';

这样如果/tmp/dkl文件夹不存在的话就会新建文件夹,如果存在就会把该文件夹下的所有的文件加载到Hive表,有一点需要注意,如果删除该分区的话,对应的文件夹也会删掉,删除语法请参考后面的第6部分。

4.2 添加非分区字段

alter table test_partition add columns(age int);

这样新加的字段是在非分区字段的最后,在分区字段之前

不过这里有一个bug,就是往表里新插入数据后,新增的age字段查询全部显示为NULL(其实数据已经存在):

  • 新增加的分区是不存在这个bug的,比如之前没有year=2011这个分区,那么新增的话不会存在bug
  • 分区在添加age字段之前已存在(即使该分区下没有任何数据),bug存在
    解决方法:
    对已存在的分区执行下面的sql即可,以分区2018为例
alter table test_partition partition(year=2018) add columns(age int);

5、多个分区字段

以两个分区字段为例

5.1 建表

create table test_partition2 (
id string comment 'ID', 
name string comment '名字'
)
comment '测试两个分区'
partitioned by (year int comment '',month int comment '')
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;

5.2 HDFS存储格式

看一下多个分区的的表如何在HDFS上存储的,用静态分区的形式插入一条记录:

insert into table test_partition2 partition(year=2018,month=12) values ('001','张三');
 
  
/apps/hive/warehouse/dkl.db/test_partition2/year=2018/month=12
 
image

6、删除分区

只能删除某个分区,如删除分区2018,而不能删除整个分区year字段。

6.1 单分区表

alter table test_partition drop partition(year=2018);

6.2 多分区表

6.2.1 删除year=2018,month=12

alter table test_partition2 drop partition(year=2018,month=12);

6.2.2 删除year=2018

year=2018所有的月份都会删除

alter table test_partition2 drop partition(year=2018);

6.2.3 删除month=10

所有月份等于10的分区都会删除,无论year=2018,还是year=2017...

alter table test_partition2 drop partition(month=10);

猜你喜欢

转载自www.cnblogs.com/makailong/p/12611128.html