首页 > 其他 > 详细

Hive的基本操作

时间:2020-09-14 12:42:01      阅读:47      评论:0      收藏:0      [点我收藏+]

1、强制删除数据库: 

通常情况下数据库中有表是不能删除的,强制删除数据库使用“cascade”。如强制删除数据库“test”
drop database test cascade;

2、创建表指定分隔符,不指定分隔符默认是TextFile格式,输出时字段内容会挨着。

cmissh@hn0-stg02:~$ hdfs dfs -cat wasb://system@cmidapsystem01.blob.core.chinacloudapi.cn/hive/warehouse/rx801.db/test/000000_0
zhangsan10

创建表时指定分隔符,如使用 , 作为分隔符,使用 row format delimited fields terminated by ‘,‘;
例:创建test表: create table test (name string,age int,address string) row format delimited fields terminated by ‘,‘; insert into test values (‘zhangsan‘,25,‘beijing‘) 通过show create table test; 可以看到文件存放的位置,wasb://system@cmidapsystem01.blob.core.chinacloudapi.cn/hive/warehouse/rx801.db/test
插入操作都以文件的形式记录在Blob中。

查看文件
hdfs dfs -ls wasb://system@cmidapsystem01.blob.core.chinacloudapi.cn/hive/warehouse/rx801.db/test
将会显示:wasb://system@cmidapsystem01.blob.core.chinacloudapi.cn/hive/warehouse/rx801.db/test/000000_0
通过查看可以看到是以分隔符 , 进行分割的。
cmissh@hn0-stg02:~$ hdfs dfs -cat wasb://system@cmidapsystem01.blob.core.chinacloudapi.cn/hive/warehouse/rx801.db/test/000000_0
zhangsan,25,beijing

 3、表中添加字段。

表中添加字段使用:alter table 表名 add columns(新字段 字段类型);
例:test表中添加一个string类型的tel字段。 hive> alter table test add columns(tel string); OK Time taken: 0.312 seconds hive> desc test; OK name string age int address string tel string Time taken: 0.185 seconds, Fetched: 4 row(s)

4、表中修改字段和字段位置移动

修改字段名使用:Alter table 表名 change column 原字段名称 现字段名称 数据类型;
例:将test表中的tel修改为int类型 hive> alter table test change column tel tel int; OK Time taken: 0.467 seconds hive> desc test; OK name string age int address string tel int Time taken: 0.23 seconds, Fetched: 4 row(s)

更改字段的位置使用:Alter table 表名 change column 原字段名称 现字段名称 数据类型 after 字段名;

例:将test表中的tel字段移动到name字段后面。
hive> alter table test change column tel tel int after name;
OK
Time taken: 0.467 seconds
hive> desc test;
OK
name                    string
tel             int age int address string Time taken: 0.255 seconds, Fetched: 4 row(s)

 5、表中删除字段

Hive中不支持使用 alter table 表名 drop columns 这种语法,支持replace
语法为:alter table 表名 replace columns(列名1 类型,列名2 类型,列名3 类型....);
replace的使用是保留括号中的列

例:删除test表中的tel列。
hive> alter table test replace columns(name string,age int,address string);
OK
Time taken: 0.303 seconds
hive> desc test;
OK
name                    string
age                     int
address                 string
Time taken: 0.179 seconds, Fetched: 3 row(s)

  

 

Hive的基本操作

原文:https://www.cnblogs.com/wqs-Time/p/13665802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!