前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「基础」SQL-Hive中常用的表格操作(下)

「基础」SQL-Hive中常用的表格操作(下)

作者头像
巡山猫说数据
发布2021-05-18 10:04:50
5020
发布2021-05-18 10:04:50
举报
文章被收录于专栏:巡山猫说数据巡山猫说数据

今天我们来讲讲Hive中常用的表格修改的操作。

Hive系列文章预计10-20篇,主要讲数据分析中最基础的SQL技能。

01-表的重命名

执行语句:

代码语言:javascript
复制
alter table oldname rename to new_name;

下面我们将建好的表t_od_use_cnt重命名为t_od_use_cnt_new,然后再改回来。

运行效果如下:

代码语言:javascript
复制
hive> set hive.cli.print.current.db=true;
hive (default)> use app;
OK
Time taken: 0.896 seconds

hive (app)> show tables;
OK
t_od_use_cnt
Time taken: 0.569 seconds, Fetched: 1 row(s)

hive (app)> alter table t_od_use_cnt rename to t_od_use_cnt_new;
OK
Time taken: 0.697 seconds

hive (app)> show tables;
OK
t_od_use_cnt_new
Time taken: 0.039 seconds, Fetched: 1 row(s)

hive (app)> alter table t_od_use_cnt_new rename to t_od_use_cnt;
OK
Time taken: 0.393 seconds

hive (app)> show tables;
OK
t_od_use_cnt
Time taken: 0.043 seconds, Fetched: 1 row(s)

02-重命名字段 & 数据类型 & 注释

执行语句:

代码语言:javascript
复制
alter table tablename change column_old_name column_new_name new_type
[comment 'XXX'];

t_od_use_cnt中的user_id字段本来是bigint类型的,注释为用户id。

下面我们试着将user_id重命名为id,数据类型改为string类型,注释改为ID,然后再改回来。运行效果如下:

代码语言:javascript
复制
hive (app)> desc t_od_use_cnt;
OK
platform              string                平台 android,ios      
app_version           string                app版本               
user_id               bigint                用户id                
use_cnt               int                   当日使用次数              
is_active             tinyint               是否活跃                
date_8                int                   日期                  

# Partition Information      
# col_name              data_type             comment             

date_8                int                   日期                  
Time taken: 0.119 seconds, Fetched: 11 row(s)

hive (app)> alter table t_od_use_cnt change user_id id string          
          > comment 'ID';
OK
Time taken: 0.365 seconds

hive (app)> desc t_od_use_cnt;
OK
platform              string                平台 android,ios      
app_version           string                app版本               
id                    string                ID               
use_cnt               int                   当日使用次数              
is_active             tinyint               是否活跃                
date_8                int                   日期                  

# Partition Information      
# col_name              data_type             comment             

date_8                int                   日期                  
Time taken: 0.173 seconds, Fetched: 11 row(s)

hive (app)> alter table t_od_use_cnt change id user_id bigint 
          > comment '用户id';
OK
Time taken: 0.138 seconds

hive (app)> desc t_od_use_cnt;
OK
platform              string                平台 android,ios      
app_version           string                app版本               
user_id               bigint                用户id                
use_cnt               int                   当日使用次数              
is_active             tinyint               是否活跃                
date_8                int                   日期                  

# Partition Information      
# col_name              data_type             comment             

date_8                int                   日期                  
Time taken: 0.103 seconds, Fetched: 11 row(s)

03-增加列

执行语句:

代码语言:javascript
复制
alter table tablename add columns (
col_name1 datatype comment 'xxx',
col_name2 datatype comment 'xxx',
...) cascade;

通过此命令可以增加任意多个新列,每加一个列用逗号断开,comment子句是可选的。下面来演示一下实际使用,我们对表t_od_use_cnt 加入两个新列test1,test2,数据类型都是string,注释为‘测试’,运行效果如下:

代码语言:javascript
复制
hive (app)> alter table t_od_use_cnt  add columns (
          > test1 string comment '测试',
          > test2 string comment '测试') cascade;
OK
Time taken: 0.742 seconds
hive (app)> desc t_od_use_cnt;
OK
platform              string                平台 android,ios      
app_version           string                app版本               
user_id               bigint                ID                  
use_cnt               int                   当日使用次数              
is_active             tinyint               是否活跃                
test1                 string                测试                  
test2                 string                测试                  
date_8                int                   日期                  

# Partition Information      
# col_name              data_type             comment             

date_8                int                   日期                  
Time taken: 0.214 seconds, Fetched: 13 row(s)

04-删除 & 替换列

Hive中并没有直接删除列的命令,但可以通过replace命令删除之前的所有字段并重新制定新的所有字段,以此达到删除字段的效果,命令如下:

代码语言:javascript
复制
alter table table_name replace columns 
(col_name1 data_type [comment 'xxx'],
col_name2 data_type [comment 'xxx'],
...);

下面我们将上一步新加的两个test列删除掉,实际是将所有字段全部删掉,用新的字段代替旧的字段。不过alter语句只能改变元数据,如果该字段本身有数据存在,那么并不会将该字段下的数据一起删掉,可以理解为只删掉了列名。运行效果如下:

代码语言:javascript
复制
hive (app)> alter table t_od_use_cnt replace columns (
          >       platform string comment '平台 android,ios'
          >       ,app_version string comment 'app版本'
          >       ,user_id bigint comment '用户id'
          >       ,use_cnt int comment '当日使用次数'
          >       ,is_active tinyint comment '是否活跃'
          >       );
OK
Time taken: 0.328 seconds
 
hive (app)> desc t_od_use_cnt;
OK
platform              string                平台 android,ios      
app_version           string                app版本               
user_id               bigint                用户id                
use_cnt               int                   当日使用次数              
is_active             tinyint               是否活跃                
date_8                int                   日期                  
      
# Partition Information      
# col_name              data_type             comment             
      
date_8                int                   日期                  
Time taken: 0.146 seconds, Fetched: 11 row(s)

05-删除分区

有时我们需要删除表中的部分分区数据,而不是删除整个表的数据。

命令如下:

代码语言:javascript
复制
ALTER TABLE table_name DROP IF EXISTS PARTITION (col_name='xxx');
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 巡山猫说数据 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01-表的重命名
  • 02-重命名字段 & 数据类型 & 注释
  • 03-增加列
  • 04-删除 & 替换列
  • 05-删除分区
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档