往table1中新增c1,c2,c3,c4字段,跑完数据后,还需要刷分区(删除原来的分区,再添加分区)
--跑完数据后,没有刷分区的话,新的字段的数据查不到
hive> select
> c1
> ,c2
> ,c3
> ,c4
> from table1
> where dt='2019-02-12' and du ='0'
> limit 5;
OK
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
Time taken: 0.113 seconds, Fetched: 5 row(s)
hive> show partitions table1;
OK
dt=2019-02-12/du=0
dt=2019-02-12/du=1
Time taken: 0.108 seconds, Fetched: 2 row(s)
--删除分区直接写dt条件即可,下面的du会同时删除
hive> alter table table1 drop partition(dt = '2019-02-12');
Dropped the partition dt=2019-02-12/du=0
Dropped the partition dt=2019-02-12/du=1
OK
Time taken: 0.316 seconds
--添加分区时,dt,du需要同时写出来
hive> alter table table1 add partition(dt = '2019-02-12',du='0');
OK
Time taken: 0.253 seconds
hive> alter table table1 add partition(dt = '2019-02-12',du='1');
OK
Time taken: 0.081 seconds
hive> show partitions table1;
OK
dt=2019-02-12/du=0
dt=2019-02-12/du=1
Time taken: 0.075 seconds, Fetched: 2 row(s)
--跑完数据后,刷分区的话,新的字段的数据能查到
hive> select
> c1
> ,c2
> ,c3
> ,c4
> from table1
> where dt='2019-02-12' and du ='0'
> limit 5;
OK
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Time taken: 0.092 seconds, Fetched: 5 row(s)
如果目录多,需要执行多条alter语句,非常麻烦。Hive提供了一个"Recover Partition"的功能。
具体语法如下:
MSCK REPAIR TABLE table_name;
原理相当简单,执行后,Hive会检测如果HDFS目录下存在但表的metastore中不存在的partition元信息,更新到metastore中。
end