hive用户可以通过alter语句更改table属性 alter partitions增加partitions: alter table table_name add [if not exists]
hive用户可以通过alter语句更改table属性
alter partitions
增加partitions:
alter table table_name
add [if not exists]
partition partition_spec [location 'location1']
partition_spec [location 'location2'] ...
partition_spec:
(partition_col = partition_col_value, partition_col = partiton_col_value, ...)
删除partitions:
alter table table_name drop [if exists] partition_spec, partition_spec,...
示例:
hive> create table alter_test(id int, name string)
> partitioned by(dt string)
> row format delimited fields terminated by ',';
ok
time taken: 0.259 seconds
hive> create table alter_tmp(id int, name string,dt string)
> row format delimited fields terminated by ',';
ok
time taken: 2.078 seconds
hive> load data local inpath '/home/work/data/alter_test.txt' into table alter_tmp;
copying data from file:/home/work/data/alter_test.txt
copying file: file:/home/work/data/alter_test.txt
loading data to table default.alter_tmp
ok
time taken: 2.71 seconds
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> set hive.exec.dynamic.partition=true;
hive> insert overwrite table alter_test partition(dt)
> select id,name,dt
> from alter_tmp;
ok
time taken: 25.988 seconds
$ cat alter_test2.txt
1,zxm,2012-08-13
2,ljz,2012-08-13
$ hadoop fs -put alter_test2.txt /data/
hive> alter table alter_test add partition(dt='2012-08-13') location '/data';
ok
time taken: 8.717 seconds
$ hadoop fs -ls /user/hive/warehouse/alter_test/
found 3 items
drwxr-xr-x - work supergroup 0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-10
drwxr-xr-x - work supergroup 0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-11
drwxr-xr-x - work supergroup 0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-12
hive> select * from alter_test where dt='2012-08-13';
ok
1 zxm 2012-08-13
2 ljz 2012-08-13
time taken: 6.064 seconds
$ hadoop fs -rmr /data
hive> select * from alter_test where dt='2012-08-13';
ok
time taken: 1.903 seconds
hive> show partitions alter_test;
ok
dt=2012-08-10
dt=2012-08-11
dt=2012-08-12
dt=2012-08-13
time taken: 0.546 seconds
> alter table alter_test add partition(dt='2012-08-14') partition(dt='2012-08-15');
ok
time taken: 0.57 seconds
hive> alter table alter_test drop partition(dt='2012-08-10');
dropping the partition dt=2012-08-10
ok
time taken: 4.509 seconds
$ hadoop fs -ls /user/hive/warehouse/alter_test/
found 4 items
drwxr-xr-x - work supergroup 0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-11
drwxr-xr-x - work supergroup 0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-12
drwxr-xr-x - work supergroup 0 2012-08-13 02:15 /user/hive/warehouse/alter_test/dt=2012-08-14
drwxr-xr-x - work supergroup 0 2012-08-13 02:15 /user/hive/warehouse/alter_test/dt=2012-08-15
注意:
1. hive可以同时增加或者删除多个partition
2. 使用location关键字时,,增加的partition以类似extend table数据的形式存在外部。