hive修改表模式

14 篇文章 0 订阅
8 篇文章 13 订阅
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数据的形式存在外部。

Alter Column
修改column属性(列名,列字段类型,列注释):
ALTER TABLE table_name 
      CHANGE [COLUMN] col_old_name col_new_name 
      column_type [COMMENT col_comment] [FIRST|AFTER column_name]
增加/替换column(可以使用replace来删除不需要的字段):
ALTER TABLE table_name 
      ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
示例:
hive> alter table alter_test add columns(test_columen INT);
OK
Time taken: 2.096 seconds
hive> desc alter_test;                                     
OK
id      int
name    string
test_columen    int
dt      string
Time taken: 0.345 seconds
hive> select * from alter_test;
OK
3       cds     NULL    2012-08-11
4       mac     NULL    2012-08-11
1       zxm     NULL    2012-08-12
2       ljz     NULL    2012-08-12
1       zxm     NULL    2012-08-13
2       ljz     NULL    2012-08-13
Time taken: 8.467 seconds
hive> alter table alter_test replace columns (id int, name string);                  
OK
Time taken: 0.217 seconds
hive> desc alter_test;                                             
OK
id      int
name    string
dt      string
Time taken: 0.181 seconds
hive> select * from alter_test;                                    
OK
3       cds     2012-08-11
4       mac     2012-08-11
1       zxm     2012-08-12
2       ljz     2012-08-12
1       zxm     2012-08-13
2       ljz     2012-08-13
Time taken: 0.364 seconds
hive> alter table alter_test change id myid INT;
OK
Time taken: 0.259 seconds
hive> desc alter_test;
OK
myid    int
name    string
dt      string
Time taken: 0.053 seconds
注意:column alter仅仅修改table的元数据,而不会修改数据。

其它:
hive alter语句还支持:
1.Alter Table Properties
2.Alter SerDe Properties
3.Alter Table/Partition File Format
4.Alter Table Storage Properties
5.Alter Table/Partition Location
6.Alter Table Touch
7.Alter Table (Un)Archive
8.Alter Table/Partition Protections
9.Alter Table Rename Partition

reference:
Hive LanguageManualDDL
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值