Hive之——表属性操作

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/80550869

1、修改表名

alter table table_name rename to new_table_name;

2、修改列表

alter table table_name change column c1 c2 int comment 'xxxx';
alter severity; //可以把改列放到指定列的后面,或者使用'first'放到第一位

3、增加列

alter table table_name add columns(c1 string comment 'xxx', c2 long comment 'yyy');
修改表名、列表、增加列实例
#创建表
create table testchange(name string, value string);
#修改表名称
alter table testchange rename to test;
#添加字段
alter table test add columns (type string, col int comment 'XXX');
#将type字段移动到name字段后面
alter table test change column type type string alter name;
#将type字段放到第一列
alter table test change column type type string first;

4、修改tblproperties

alter table table_name set tblproperties(property_name=property_value, property_name = property_value, ...);

5、无分区表修改tblproperties

alter table table_name set serdeproperties('field.delim'='\t');

无分区表修改tblproperties实例:

#创建表
create table city(
time string,
country string,
province string,
city string
)
row format delimited
fields terminated by '#'
lines terminated by '\n'
stored as textfile;

#导入数据
load data local inpath '/usr/local/src/city' into table city;

#导入数据如下:
2673627367236   中国    河北    石家庄
6236732647473   中国    四川    成都
6723647274434   中国    河南    洛阳
2374832436476   中国    河北    廊坊

#查询数据(含有NULL)
hive> select * from city;
OK
2673627367236   中国    河北    石家庄  NULL    NULL    NULL
6236732647473   中国    四川    成都    NULL    NULL    NULL
6723647274434   中国    河南    洛阳    NULL    NULL    NULL
2374832436476   中国    河北    廊坊    NULL    NULL    NULL

#查看建表信息
desc formatted city;
...
Storage Desc Params:             
        field.delim             #                   
        line.delim              \n                  
        serialization.format    # 

#修改tblproperties
alter table city set serdeproperties('field.delim'='\t');

#查询数据(不含NULL了)
hive> select * from city;
OK
2673627367236   中国    河北    石家庄
6236732647473   中国    四川    成都
6723647274434   中国    河南    洛阳
2374832436476   中国    河北    廊坊

#查看建表信息
desc formatted city;
...
Storage Desc Params:             
        field.delim             \t                  
        line.delim              \n                  
        serialization.format    #

6、有分区表修改tblproperties

alter table test partition(dt='xxxxx') set serdeproperties('field.delim'='\t');
有分区修改tblproperties实例:
#创建分区表
drop table city;
create table city(
time string,
country string,
province string,
city string
)
partitioned by (dt string)
row format delimited
fields terminated by '#'
lines terminated by '\n'
stored as textfile;

#导入数据:
load data local inpath '/usr/local/src/city' into table city partition (dt='20180602');

#导入数据如下:
2673627367236   中国    河北    石家庄
6236732647473   中国    四川    成都
6723647274434   中国    河南    洛阳
2374832436476   中国    河北    廊坊

#查询数据(含有NULL)
hive> select * from city;
OK
2673627367236   中国    河北    石家庄  NULL    NULL    NULL    20180602
6236732647473   中国    四川    成都    NULL    NULL    NULL    20180602
6723647274434   中国    河南    洛阳    NULL    NULL    NULL    20180602
2374832436476   中国    河北    廊坊    NULL    NULL    NULL    20180602

#查看建表信息
desc formatted city;
...
Storage Desc Params:             
        field.delim             #                   
        line.delim              \n                  
        serialization.format    # 
		
#修改tblproperties
alter table city set serdeproperties('field.delim'='\t');

#查看建表信息
desc formatted city;
...
Storage Desc Params:             
        field.delim             \t                  
        line.delim              \n                  
        serialization.format    # 
		
#查询数据(仍然含有NULL)
    > select * from city;
OK
2673627367236   中国    河北    石家庄  NULL    NULL    NULL    20180602
6236732647473   中国    四川    成都    NULL    NULL    NULL    20180602
6723647274434   中国    河南    洛阳    NULL    NULL    NULL    20180602
2374832436476   中国    河北    廊坊    NULL    NULL    NULL    20180602
Time taken: 0.184 seconds, Fetched: 4 row(s)

#加上分区信息修改tblproperties
alter table city partition(dt='20180602') set serdeproperties('field.delim'='\t');

#查询数据(不含NULL了)
hive> select * from city;
OK
2673627367236   中国    河北    石家庄  20180602
6236732647473   中国    四川    成都    20180602
6723647274434   中国    河南    洛阳    20180602
2374832436476   中国    河北    廊坊    20180602
Time taken: 0.17 seconds, Fetched: 4 row(s)
注意:实例有查询的结果有NULL是因为建表中是以"#"为分隔符,导入数据的时候却是以"\t"为分隔符,分隔符不一致导致的。

7、修改location

alter table table_name [partition(...)] set location 'path';
实例:
#创建表
drop table city;
create table city(
time string,
country string,
province string,
city string
)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;

#查看建表信息
desc formatted city;
...
Location:               hdfs://liuyazhuang121:9000/user/hive/warehouse/lyz.db/city
....

#新建Hadoop下的目录(Linux shell)
hadoop fs -mkdir /location
#上传文件(Linux shell)
hadoop fs -put city /location
#上传文件内容
2673627367236   中国    河北    石家庄
6236732647473   中国    四川    成都
6723647274434   中国    河南    洛阳
2374832436476   中国    河北    廊坊

#修改location
alter table city set location '/location';
#查看建表信息
desc formatted city;
...
Location:               hdfs://liuyazhuang121:9000/location
...

#查询数据
hive> select * from city;
OK
2673627367236   中国    河北    石家庄
6236732647473   中国    四川    成都
6723647274434   中国    河南    洛阳
2374832436476   中国    河北    廊坊

8、内部表转外部表

alter table table_name set tblproperties('EXTERNAL'='TRUE');   #注意大写的单词

实例:

#创建表
drop table city;
create table city(
time string,
country string,
province string,
city string
)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;

#查看建表信息
desc formatted city;
...
Table Type:             MANAGED_TABLE 
...

#内部表转外部表
alter table city set tblproperties('EXTERNAL'='TRUE');	   #注意大写的单词

#查看建表信息
desc formatted city;
...
Table Type:             EXTERNAL_TABLE
...

9、外部表转内部表

alter table table_name set tblproperties('EXTERNAL'='FALSE');    #注意大写的单词

实例

#查看建表信息
desc formatted city;
...
Table Type:             EXTERNAL_TABLE
...

#内部表转外部表
alter table city set tblproperties('EXTERNAL'='FALSE');    #注意大写的单词

#查看建表信息
desc formatted city;
...
Table Type:             MANAGED_TABLE 
...

10、其他表属性操作

alter table properties
alter serde properties
alter table/partition file format
alter table storage properties
alter table rename partition
alter table set location

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/80550869