Hive增加列,调整列顺序,属性名等操作

一、hive添加字段到指定位置

先添加字段到最后位置再移动到指定位置

1、添加字段

ADD COLUMNS允许用户在当前列的末尾,分区列之前添加新的列

alter table table_name add columns (age string comment '当前时间') cascade;

2、移动字段到指定位置

— 移动在某个字段后面

alter table table_name change age age string comment '年龄' after id ;

— 移动到开头位置

alter table table_name change age age string comment '年龄' first;

— 修改字段类型

alter table table_name change column age age string;

— 替换表字段(REPLACE COLUMNS允许用户更新列,更新的过程是先删除当前的列,然后在加入新的列)

注:只有在使用native的SerDE时才可以这么做。

alter table table_name replace columns (age int comment '年龄', uage int comment '你的名字') ;

3、修改表的属性

(1) 将table_name表中的property_name属性值修改成’new_value’;

alter table table_name set tblproperties('property_name'='new_value');

(2)将table_name表中的comment属性值修改成’new_value’;

alter table table_name set tblproperties('comment'='new_value');

(3)将表table_name中的字段分割符修改成’\t’,注意,这是在表没有分区的情况下

alter table emp set serdeproperties ("field.delim"="\t");

例1:create table emp( id int,uname string) row format delimited fields terminated by ‘#’ lines terminated by ‘\n’ stored as textfile;

alter table emp set serdeproperties(‘field.delim’=’\t’);这条语句将t8表中的字段分隔符’#’修改成’\t’;

例2:create table emp(id int,uname string) partitioned by(dt=string) row foramt delimited fields terminated by ‘\n’ stored as textfile;

alter table emp partition(dt='20180108') set serdeproperties('field.delim=\t');

(4)修改存储位置

alter table table_name set location 'path'

(5)内部表转化成外部表

alter table emp set TBLPROPERTIES('EXTERNAL'='TRUE');

外部表转成内部表

 alter table emp set TBLPROPERTIES('EXTERNAL'='FALSE');

二、常见问题

问题描述:

实际应用中,常常存在修改数据表结构的需求,比如:增加一个新字段。如果使用如下语句新增列,可以成功添加列col1。但如果数据表tb已经有旧的分区(例如:dt=20190101),则该旧分区中的col1将为空且无法更新,即便insert overwrite该分区也不会生效。

alter table table_name add columns (c_time string comment ‘当前时间’);

解决方法:

解决方法很简单,就是增加col1时加上cascade关键字。示例如下:

alter table table_name add columns (c_time string comment ‘当前时间’) cascade;

Hive增加列,调整列顺序,属性名等操作

注意:有些hive版本比较低不支持cascade,则需要备份数据,删除该分区后再insert overwrite。

Original: https://blog.csdn.net/qq_36249352/article/details/127494363
Author: IMezZ
Title: Hive增加列,调整列顺序,属性名等操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/817843/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球