Hive表的相关操作

创建表

1.完整语法:

create [temporary] [external] table [if not exists]
数据库名.表名
(
	字段名1 字段类型 comment '注释内容',
	字段名2 字段类型 comment '注释内容',
	.....
)comment '注释内容'
row format row_format
stored as file_format
location hdfs_path
tblproperties(property_name=property_value,...);

2.关键字说明:

(1)temporary:临时表,当前会话可见,会话结束,表被删除
(2)external:外部表,与之相对应的是内部表(管理表),管理表也就是hive完全负责该表,包括元数据信息和hdfs中的数据,而外部表hive只负责元数据信息

注意:不加external关键字创建的就是内部表
(3)数据类型:

Hive说明定义
int4字节有符号整数
bigint8字节有符号整数
double双精度浮点数
decimal十进制精准数字类型decimal(5,2)
varchar字符序列varchar(10)
string字符串

array:数组是一组相同类型的值的集合
定义:

array<string>

取值:array[0
]
map:map是一组相同类型的键-值对

map<string,int>

取值:map[‘key’]

struct:结构体由多个属性组成,每个属性都有自己的属性名和数据类型

定义:

struct<id:int,name:string>

取值:struct.id

(4)数据类型转换:
hive隐式转换:任何整数类型都可以隐式地转换为一个范围更广的类型。例如:int可以转换成bigint

hive显示转换:语法:cast(expr as 数据类型>)
例子:

select cast('1' as int);

(5)row format

  • 第一种语法

row format delimited fields terminated by ‘\t’

delimited关键字表示对文件中的每个字段按照特定分隔符进行分割,使用默认的serde对每行数据进行序列化和反序列化。

具体语法:

row format delimited
[fields terminated by char]
[collection items terminated by char]
[map keys terminated by char]
[lines terminated by char]
[null defined as char]

fields terminated by:列分隔符

collection items terminated by:map、struct和array中每个元素之间的分隔符

map keys terminated by:map中的key与value的分隔符

lines terminated by:行分隔符

第二种语法
serde关键字可用于指定其内置的serde或者用户自定一的serde
json的serde,用于处理json字符串

row format serde 'org.apache.hadoop.hive.serde2.JsonSerDe'

(6)location:指定表的路径位置

3.根据查询的内容进行建表

create [temporary] table [if not exists] 表名字
[comment '表的注释']
[row format row_format]
[stored as file_format]
[location 路径]
[tblproperties(property_name=property_value,...)]
as 查询内容

例子:

# 根据从表teacher查出的内容进行创建表
create table teache1 as select * from teacher;

4.根据已有的表结构进行建表

语法:

create [temporary] [external] table [if not exists]
[数据库.数据库表]
[like 存在的表名]
[row format row_format]
[stored as file_format]
[location 路径位置]
[tblproperties](property_name=property_value,...)

例子

# 建好表后teacher2表中没有teacher表中的数据
create table teacher2 like teacher;

分析内部表和外部表

外部表:有关键字external

-- 外部表
create  external table if not exists student(
    id int,
    name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student';

内部表:没有关键字

create table if not exists student(
    id int,
    name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student';

对于外部表,Hive负责管理元数据,不负责hdfs实际的数据,所以删除外部表后,只会删除掉元数据信息,而Hdfs上的数据是不会被删除的

对于内部表,Hive负责管理元数据和hdfs中的实际数据,所以删除内部表后,元数据和Hdfs中的实际数据都会被删除掉

查看表

(1)展示所有表
语法

show tables [in 数据库名] like [''];

注意:like通配符表达式说明,*表示任意个字符,_表示单个字符

例子:

# 展示db_hive1数据库的所有表
show tables in db_hive1 like '*';

(2)查看表信息
语法:

desc [extended|formatted] [数据库名.表名]

注意:extended展示详细信息,formatted对详细信息进行一个格式化操作

例子:

# 查看基本信息
desc student;

# 查看更多信息
desc formatted student;

修改表

重命名表

(1)语法:

alter table 表名 rename to 新的表名;

例子:

alter table student1 rename to stu1;

修改列信息

(1)语法:

  • 增加列
alter table 表名 add columns (列名 数据类型) comment '注释内容';
  • 更新列
alter table 表名 change 列名 新列名 数据类型 comment '注释内容' [first|after 列名]
  • 替换列
alter table 表名 replace name (列名 数据类型....) comment '注释内容';

例子:

# 首先创建一张表student
create table student(
    name string,
    age int
)
row format delimited fields terminated by '\t'
location  '/my_db/student';

# 然后添加列id
alter table student add columns (id int);

# 查询表的结构
desc student;

# 更新列(修改列名id为stu_id,类型修改为double)
alter table student change  column id stu_id double;

# 替换列
alter table student replace columns (id int,name string);

删除表

(1)语法:

drop table [if exists] 表名;

(2)例子:

drop table student;

清空表

(1)语法:

truncate table 表名;

(2)例子:

# 清空表studnet的数据
truncate table student;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值