hive 分区表

2023-03-11  本文已影响0人  后知不觉1

分区表创建

#单分区
create table test1(userid string, name string, age int) 
partitioned by (inc_day string); 

#多级分区
create table test2(userid string, name string, age int) 
partitioned by (inc_day string, hour string); 

插入数据

#单分区
insert into test partition (inc_day=20230310)  values ("tianzheao","tianzehao",30);

多分区

   insert into test partition (inc_day='20230310', hour='20:47')  values ("tianzheao","tianzehao",30);

查看分区

show partitions $tablename;
show partitions test;
image.png image.png

hdfs路径

正常的分区路径是 "分区字段=分区值",多级分区在目录下再有分区字段,最后是hdfs文件

image.png

修复分区

通过其他方式写入数据hdfs数据到目录,创建分区表后分区元数据是缺失的,这时候需要修复分区

修复分区方法有两个 msck ,alter

msck

使用msck 修复有一个前提,目录信息中包含分区信息,入股这个不满足,则无法使用
比如: test分区表,最后一层的上一层目录有分区字段。


image.png

如果是多级分区目录依次类推


image.png image.png
alter

alter添加分区没有限制

alter table default.testall add
partition(inc_day='20220905');

外部表删除分区

外部表删除分区只会删除元数据不会删除hdfs 数据文件,解决思路是外部表改为内部表,删除分区后在将内部表改外外部表

alter table `ods_blpt`.`year_his` set TBLPROPERTIES('EXTERNAL'='false');
alter table `ods_blpt`.`year_his` drop partition (ds <= '$[time(yyyyMMdd,-30d)]');
alter table `ods_blpt`.`year_his` set TBLPROPERTIES('EXTERNAL'='true');
上一篇 下一篇

猜你喜欢

热点阅读