HiveSQL DDL

HiveSQL

按数据操作分类,来分别说明脚本的使用。

1.DDL

1.1建表说明
元数据:描述数据的数据
表分类:主要分内表和外表
内表:元数据和数据本身均被 hive 管理。删除表则全部删除。
外表:元数据被 hive 管理,数据本身存储在 hdfs,不受 hive 管理。删除表则只删除元数据,数据本身不变。

1.2建表模板

CREATE [external] TABLE [IF NOT EXISTS] table_name [(col_name data_type [comment col_comment], ...)] [comment table_comment]
[partitioned by (col_name data_type [comment col_comment], ...)]
[clustered by (col_name, col_name, ...)
[sorted by (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[row format row_format] 
[stored as file_format] 
[location hdfs_path]

关键词解释
external: 创建内部表还是外部表,此为内外表的唯一区分关键字。
【有外部表;没有内部表】
comment col_comment: 给字段添加注释
comment table_comment: 给表本身添加注释
partitioned by: 按哪些字段分区,可以是一个,也可以是多个
clustered by col_name… into num_buckets BUCKETS:按哪几个字段做 hash 后分桶存储
row format:用于设定行、列、集合的分隔符等设置
row format delimited
fields terminated by ‘\t’
stored as : 用于指定存储的文件类型,如 text,rcfile 等
location : 设定该表存储的 hdfs 目录,如果不手动设定,则采用 hive 默认的存储路径

1.3示例

创建学生表student,包括 id,name,classid,classname 及分区和注释信息。

CREATE	TABLE student( 
id string comment '学号',
username string comment ' 姓 名 ', 
classid int comment ' 班 级 id', 
classname string comment '班级名称’)comment '学生信息主表'
partitioned by (come_date string comment '按入学年份分区') 
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\001' 
LINES TERMINATED BY '\n'
STORED AS	textfile;

1.4命令

1.4.1查看已存在表的详细信息:

show create table
 或者 
desc tablename/desc formatted tablename

1.4.2显示所有表:

show tables;

1.4.3更改表:

alter table student rename to student2;

1.4.4增加字段:

alter table student2 add columns (age int comment "我是新增加的列");

1.4.5确认表结构。

show create table student2;

1.4.6创建视图(虚表)
视图:本身不存储实际数据,只存储表关系,使用时再去通过关系查找数据。

create view student2_view as select id,username from student2;

1.4.7删除视图

drop view student2_view;

猜你喜欢

转载自blog.csdn.net/weixin_43400357/article/details/85145270
DDL