hive修改表模式<转>

留个备份。

hive用户可以通过alter语句更改table属性


Alter Partitions
增加partitions:
[plain]  view plain copy
  1. ALTER TABLE table_name   
  2.       ADD [IF NOT EXISTS]   
  3.       PARTITION partition_spec [LOCATION 'location1']   
  4.                 partition_spec [LOCATION 'location2'] ...  
  5. partition_spec:   
  6.       (partition_col = partition_col_value, partition_col = partiton_col_value, ...)  
删除partitions:
[plain]  view plain copy
  1. ALTER TABLE table_name DROP [IF EXISTS] partition_spec, partition_spec,...  
示例:
[plain]  view plain copy
  1. hive> create table alter_test(id INT, name STRING)    
  2.     > partitioned by(dt STRING)                       
  3.     > row format delimited fields terminated by ',';  
  4. OK  
  5. Time taken: 0.259 seconds  
  6. hive> create table alter_tmp(id INT, name STRING,dt STRING)  
  7.     > row format delimited fields terminated by ',';  
  8. OK  
  9. Time taken: 2.078 seconds  
  10. hive> load data local inpath '/home/work/data/alter_test.txt' into table alter_tmp;  
  11. Copying data from file:/home/work/data/alter_test.txt  
  12. Copying file: file:/home/work/data/alter_test.txt  
  13. Loading data to table default.alter_tmp  
  14. OK  
  15. Time taken: 2.71 seconds  
  16. hive> set hive.exec.dynamic.partition.mode=nonstrict;  
  17. hive> set hive.exec.dynamic.partition=true;  
  18. hive> insert overwrite table alter_test partition(dt)  
  19.     > select id,name,dt                                
  20.     > from alter_tmp;    
  21. OK  
  22. Time taken: 25.988 seconds  
  23. $ cat alter_test2.txt   
  24. 1,zxm,2012-08-13  
  25. 2,ljz,2012-08-13  
  26. $ hadoop fs -put alter_test2.txt /data/  
  27. hive> alter table alter_test add partition(dt='2012-08-13') location '/data';                  
  28. OK  
  29. Time taken: 8.717 seconds  
  30. $ hadoop fs -ls /user/hive/warehouse/alter_test/  
  31. Found 3 items  
  32. drwxr-xr-x   - work supergroup          0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-10  
  33. drwxr-xr-x   - work supergroup          0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-11  
  34. drwxr-xr-x   - work supergroup          0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-12  
  35. hive> select * from alter_test where dt='2012-08-13';    
  36. OK  
  37. 1       zxm     2012-08-13  
  38. 2       ljz     2012-08-13  
  39. Time taken: 6.064 seconds  
  40. $ hadoop fs -rmr  /data  
  41. hive> select * from alter_test where dt='2012-08-13';    
  42. OK  
  43. Time taken: 1.903 seconds  
  44. hive> show partitions alter_test;  
  45. OK  
  46. dt=2012-08-10  
  47. dt=2012-08-11  
  48. dt=2012-08-12  
  49. dt=2012-08-13  
  50. Time taken: 0.546 seconds  
  51. > alter table alter_test add partition(dt='2012-08-14') partition(dt='2012-08-15');        
  52. OK  
  53. Time taken: 0.57 seconds  
  54. hive> alter table alter_test drop partition(dt='2012-08-10');                             
  55. Dropping the partition dt=2012-08-10  
  56. OK  
  57. Time taken: 4.509 seconds  
  58. $ hadoop fs -ls /user/hive/warehouse/alter_test/   
  59. Found 4 items  
  60. drwxr-xr-x   - work supergroup          0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-11  
  61. drwxr-xr-x   - work supergroup          0 2012-08-12 20:50 /user/hive/warehouse/alter_test/dt=2012-08-12  
  62. drwxr-xr-x   - work supergroup          0 2012-08-13 02:15 /user/hive/warehouse/alter_test/dt=2012-08-14  
  63. 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属性(列名,列字段类型,列注释):
[plain]  view plain copy
  1. ALTER TABLE table_name   
  2.       CHANGE [COLUMN] col_old_name col_new_name   
  3.       column_type [COMMENT col_comment] [FIRST|AFTER column_name]  
增加/替换column(可以使用replace来删除不需要的字段):
[plain]  view plain copy
  1. ALTER TABLE table_name   
  2.       ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)  
示例:
[plain]  view plain copy
  1. hive> alter table alter_test add columns(test_columen INT);  
  2. OK  
  3. Time taken: 2.096 seconds  
  4. hive> desc alter_test;                                       
  5. OK  
  6. id      int  
  7. name    string  
  8. test_columen    int  
  9. dt      string  
  10. Time taken: 0.345 seconds  
  11. hive> select * from alter_test;  
  12. OK  
  13. 3       cds     NULL    2012-08-11  
  14. 4       mac     NULL    2012-08-11  
  15. 1       zxm     NULL    2012-08-12  
  16. 2       ljz     NULL    2012-08-12  
  17. 1       zxm     NULL    2012-08-13  
  18. 2       ljz     NULL    2012-08-13  
  19. Time taken: 8.467 seconds  
  20. hive> alter table alter_test replace columns (id int, name string);                    
  21. OK  
  22. Time taken: 0.217 seconds  
  23. hive> desc alter_test;                                               
  24. OK  
  25. id      int  
  26. name    string  
  27. dt      string  
  28. Time taken: 0.181 seconds  
  29. hive> select * from alter_test;                                      
  30. OK  
  31. 3       cds     2012-08-11  
  32. 4       mac     2012-08-11  
  33. 1       zxm     2012-08-12  
  34. 2       ljz     2012-08-12  
  35. 1       zxm     2012-08-13  
  36. 2       ljz     2012-08-13  
  37. Time taken: 0.364 seconds  
  38. hive> alter table alter_test change id myid INT;  
  39. OK  
  40. Time taken: 0.259 seconds  
  41. hive> desc alter_test;  
  42. OK  
  43. myid    int  
  44. name    string  
  45. dt      string  
  46. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值