hive之DDL

本文详细介绍了Hive的DDL(数据定义语言)指令,包括创建、查看、修改和删除数据库及表的操作。内容涵盖内外部表、分区表的创建与管理,以及如何使用查询结果创建新表。通过这些操作,可以有效地管理和组织Hive中的数据。
摘要由CSDN通过智能技术生成

一、库的DDL

--建库
CREATE DATABASE [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];

--例
create database test
comment "Just for test"
location '/test.db'
with dbproperties("aaa"="bbb");
--查看所有库
show databases;
--查看库信息
desc database test;
--查看详细信息
desc database extended test;
--修改库
alter database test set dbproperties("aaa"="ccc");
--删库
drop database test;
--强制删库
drop database test cascade;

二、表的DDL

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]
[TBLPROPERTIES (property_name=property_value, ...)]
[AS select_statement]
字段解释说明
(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。
(2)EXTERNAL关键字可以让用户创建一个外部表,在建表的同时可以指定一个指向实际数据的路径(LOCATION),在删除表的时候,
内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
(3)COMMENT:为表和列添加注释。
(4)PARTITIONED BY创建分区表
(5)CLUSTERED BY创建分桶表
(6)SORTED BY不常用,对桶中的一个或多个列另外排序
(7)ROW FORMAT 
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char]
        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] 
   | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
用户在建表的时候可以自定义SerDe或者使用自带的SerDe。如果没有指定ROW FORMAT 或者ROW FORMAT DELIMITED,将会使用自带的SerDe。
在建表的时候,用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的SerDe,Hive通过SerDe确定表的具体的列的数据。
SerDe是Serialize/Deserilize的简称, hive使用Serde进行行对象的序列与反序列化。
(8)STORED AS指定存储文件类型
常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在HDFS上的存储位置。
(10)AS:后跟查询语句,根据查询结果创建表。
(11)LIKE允许用户复制现有的表结构,但是不复制数据。

--创建表
create table test
(id int comment "ID" , name string comment "Name")
comment "Test Table"
row format delimited fields terminated by '\t'
location "/xxx"
tblproperties("aaa"="bbb");
--查表
desc test;
desc formatted test;
--修改一列
alter table test2 change id id string;
--添加一列或多列
alter table test2 add columns(class string comment "class NO.");
--替换多列信息
alter table test2 replace columns(id double comment "ID" , name string);
--删表
drop table test2;
--用查询结果建表
create table stu_result as select * from stu_par where id=1001;

三、内外部表

1. 建立外部表

--建立外部表
create external table test
(id int, name string)
row format delimited fields terminated by '\t';
--插入数据
load data local inpath "/opt/module/datas/student.txt" into table test;
--删表后查看HDFS,数据还在
drop table test;

2. 外部表和内部表转换

--建立错成内部表
create table test
(id int, name string)
row format delimited fields terminated by '\t';
--转换成外部表
alter table test set tblproperties("EXTERNAL"="TRUE");
--转换成内部表
alter table test set tblproperties("EXTERNAL"="FALSE");

四、分区表

1. 一级分区表

--建立一张分区表
create table stu_par
(id int, name string)
partitioned by (class string)
row format delimited fields terminated by '\t';
--向表中插入数据
load data local inpath '/opt/module/datas/student.txt' into table stu_par
partition (class='01');
load data local inpath '/opt/module/datas/student.txt' into table stu_par
partition (class='02');
load data local inpath '/opt/module/datas/student.txt' into table stu_par
partition (class='03');
--查表时,选择分区,可以减小数据扫描量
select * from stu_par where class="01";
select * from stu_par where id=1001;
--查询分区表的分区
show partitions stu_par;
--如果提前准备数据,但是没有元数据,修复方式
--1. 添加分区
alter table stu_par add partition(class="03");
--2. 直接修复
msck repair table stu_par;
--3. 上传时候直接带分区
load data local inpath '/opt/module/datas/student.txt' into table stu_par
partition (class='03');

2. 二级分区表

--建立二级分区表
create table stu_par2
(id int, name string)
partitioned by (grade string, class string)
row format delimited fields terminated by '\t';
--插入数据,指定到二级分区
load data local inpath '/opt/module/datas/student.txt' into table stu_par2
partition (grade='01', class='03');

3. 分区的增删改查

--增加分区
alter table stu_par add partition(class="05");
--一次增加多个分区
alter table stu_par add partition(class="06") partition(class="07");
--删除分区
alter table stu_par drop partition(class="05");
--一次删除多个分区
alter table stu_par drop partition(class="06"), partition(class="07");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值