Hive学习之更改表的属性

1、修改表名
    alter table table_name rename to new_table_name;
2、修改列名
    alter table tablename change column column_orign column_new int(修改后列的属性) comment 'column_name'
    after severity;//可以把该列放到指定列的后面,或者使用‘first’放到第一位
    将表tablename中的列column_orign修改成column_new,同时指定修改后的列名称的属性,comment是这个列的注释
3、增加列
    alter table tablename add columns(column1 string comment 'xxxx',column2 long comment 'yyyy')
4、查看表的属性
    desc formatted tablename;
5、修改表的属性
    (1)alter table table_name set tblproperties('property_name'='new_value');
       将table_name表中的comment属性值修改成'new_value';
    (2)alter table table_name set serdepropertyes('field.delim'=' ');
       将表table_name中的字段分割符修改成' ',注意,这是在表没有分区的情况下
    例1:create table t8(time string,country string,province string,city string)row format delimited fields terminated by '#' lines terminated by ' ' stored as textfile;
       alter table t8 set serdepropertyes('field.delim'=' ');这条语句将t8表中的字段分隔符'#'修改成' ';
    例2:create table t9(time string,country string,province string,city string) partitioned by(dt=string) row foramt delimited fields terminated by ' ' stored as textfile;
       alter table t9 partition(dt='20140901') set serdepropertyes('field.delim= ');
    (3)alter table table_name[partition] set location 'path'
       alter table table_name set TBLPROPERTIES('EXTERNAL'='TRUE');//内部表转化成外部表
       alter table table_name set TBLPROPERTIES('EXTERNAL'='FALSE');//外部表转成内部表
    Tip:首先在Hadoop上新建一个文件夹,'hadoop fs -mkdir /fould_name',然后向文件夹下面上传数据:'hadoop fs copyFromLocal /root/data /location',这句命令
    代表了将'/root/data'目录下的data文件上传到hadoop的location文件夹下,可以通过命令'hadoop fs -ls /location'命令查看location文件夹下的文件。
    在(2)中,t9表的位置在:'/hive/t9'目录下,然后通过命令:'alter table city set location 'hdfs://hadoop:9000/location'可以将表t9的位置更改为:hadoop下的/location文件夹下面;
    同时还必须注意的是,由于t9默认是内表,所以在删除表t9时,它的数据包括文件目录全部被删除,为了防止这种情况发生,可以将表t9修改成外表,通过语句:alter table t9 set tblproperties('EXTERNAL'='TRUE');
    以此类推,同样也可以将外部表修改为内部表,可以通过语句:alter table city set tblproperties('EXTERNAL'='FALSE');
    (4)其他修改表属性的命令:alter table properties; alter serde properties; alter table/partition file format; alter table storage properties; alter table rename partition; alter table set location;
    详细说明可以参考:Hive官网,在官网中找:wiki LanguageManual DDL来查询详细介绍

原文地址:https://www.cnblogs.com/sunfie/p/4375795.html