DDL数据定义
创建数据库
1)创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db。
hive (default)> create database db_hive;

2)避免要创建的数据库已经存在错误,增加if not exists判断。(标准写法)
hive (default)> create database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exists
hive (default)> create database if not exists db_hive;
3)创建一个数据库,指定数据库在HDFS上存放的位置
hive (default)> create database db_hive2 location '/db_hive2.db';

查询数据库
显示数据库
1.显示数据库
hive> show databases;
2.过滤显示查询的数据库
hive (default)> show databases like "db_hive*";
OK
database_name
db_hive
db_hive2
Time taken: 0.014 seconds, Fetched: 2 row(s)
查看数据库详情
1.显示数据库信息
hive (default)> desc database db_hive;
OK
db_name comment location owner_name owner_type parameters
db_hive hdfs://0.0.0.0:9000/user/hive/warehouse/db_hive.db root USER
Time taken: 0.027 seconds, Fetched: 1 row(s)
2.显示数据库详细信息,extended(不加额外属性时,查出来的东西跟不加extended一样)
hive (default)> desc database extended db_hive;
OK
db_name comment location owner_name owner_type parameters
db_hive hdfs://0.0.0.0:9000/user/hive/warehouse/db_hive.db root USER
Time taken: 0.031 seconds, Fetched: 1 row(s)
切换当前数据库
hive (default)> use db_hive;
修改数据库
说是这么说,但实际上只能增加属性
用户可以使用ALTER DATABASE命令为某个数据库的dbproperties设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。
hive (default)> alter database db_hive set dbproperties('createtime'='20200130');
在hive中查看修改结果
hive (default)> desc database extended db_hive;
OK
db_name comment location owner_name owner_type parameters
db_hive hdfs://0.0.0.0:9000/user/hive/warehouse/db_hive.db root USER {createtime=20200130}
Time taken: 0.025 seconds, Fetched: 1 row(s)
删除数据库
1.删除空数据库
hive>drop database db_hive2;
2.如果删除的数据库不存在,最好采用 if exists判断数据库是否存在
hive> drop database db_hive;
FAILED: SemanticException [Error 10072]: Database does not exist: db_hive
hive> drop database if exists db_hive2;
3.如果数据库不为空,可以采用cascade(层叠,,差不多跟递归的意思一样)命令,强制删除
hive> drop database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database db_hive is not empty. One or more tables exist.)
hive> drop database db_hive cascade;
创建表
1.建表语法
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]
2.字段解释说明
(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;用户可以用 IF NOT EXISTS 选项来忽略这个异常。
(2)EXTERNAL关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),Hive创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
(3)COMMENT:为表和列添加注释。
(4)PARTITIONED BY创建分区表
(5)CLUSTERED BY创建分桶表
(6)SORTED BY不常用
(7)ROW FORMAT指定分隔符
(8)STORED AS指定存储文件类型
常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)
如果文件数据是纯文本,可以使用STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在HDFS上的存储位置。
(10)LIKE允许用户复制现有的表结构,但是不复制数据。
管理表
1.理论
默认创建的表都是所谓的管理表,有时也被称为内部表。因为这种表,Hive会(或多或少地)控制着数据的生命周期。Hive默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(例如,/user/hive/warehouse)所定义的目录的子目录下。 当我们删除一个管理表时,Hive也会删除这个表中数据。管理表不适合和其他工具共享数据。
2.案例实操
(1)普通创建表
create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t' //指定分隔符
stored as textfile //指定存储文件类型
location '/user/hive/warehouse/student2';
(2)根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student3 as select id, name from student;
(3)根据已经存在的表结构创建表
create table if not exists student4 like student;
(4)查询表的格式化信息
hive (default)> desc formatted student2;
OK
col_name data_type comment
# col_name data_type comment
id int
name string
# Detailed Table Information
Database: default
Owner: root
CreateTime: Sat Apr 20 14:15:53 CST 2019
LastAccessTime: UNKNOWN
Protect Mode: None
Retention: 0
Location: hdfs://0.0.0.0:9000/user/hive/warehouse/student2
Table Type: MANAGED_TABLE
Table Parameters:
transient_lastDdlTime 1555740953
# Storage Information
SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat: org.apache.hadoop.mapred.TextInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed: No
Num Buckets: -1
Bucket Columns: []
Sort Columns: []
Storage Desc Params:
field.delim \t
serialization.format \t
Time taken: 0.217 seconds, Fetched: 28 row(s)
外部表
1.理论
因为表是外部表,所以Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。
2.管理表和外部表的使用场景
每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。
3.案例实操
分别创建部门和员工外部表,并向表中导入数据。
(1)原始数据,先准备这两个文件
dept.txt
10 ACCOUNTING 1700
20 RESEARCH 1800
30 SALES 1900
40 OPERATIONS 1700
emp.txt
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
(2)建表语句
创建部门表
create external table if not exists dept(
deptid int,
dname string,
loc int
)
row format delimited fields terminated by '\t';
创建员工表
create external table if not exists emp(
empid int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by '\t';
(3)查看创建的表
hive (default)> show tables;
OK
tab_name
dept
emp
student2
test
Time taken: 0.031 seconds, Fetched: 4 row(s)
(4)向外部表中导入数据
导入数据
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept;
hive (default)> load data local inpath '/root/datas/emp.txt' into table emp;
查询结果
hive (default)> select * from emp;
hive (default)> select * from dept;
管理表与外部表的互相转换
1 查询表的类型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
2 修改内部表student2为外部表
alter table student2 set tblproperties('EXTERNAL'='TRUE');
3 查询表的类型
hive (default)> desc formatted student2;
Table Type: EXTERNAL_TABLE
4 修改外部表student2为内部表
alter table student2 set tblproperties('EXTERNAL'='FALSE');
5 查询表的类型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
注意:(‘EXTERNAL’=‘TRUE’)和(‘EXTERNAL’=‘FALSE’)为固定写法,区分大小写!
分区表
1.创建分区表语法
hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
2.加载数据到分区表中
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept_partition partition(month='20190420');
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept_partition partition(month='20190421');
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept_partition partition(month='20190422');
3.查看此时hdfs中表的目录结构

可以看到目录下面加了三个分区
4.查询分区表中数据
单分区查询
hive (default)> select * from dept_partition where month='20190420';
多分区联合查询
hive (default)> select * from dept_partition where month='20190420'
union
select * from dept_partition where month='20190421'
union
select * from dept_partition where month='20190422';
这种方式会跑MapReduce程序来查询
还可以使用or来连接查询,这样不用跑mapreduce程序,效率更高
select * from dept_partition where month=20190420
or month=20190421 or month=20190422;
查询结果为:
dept_partition.deptno dept_partition.dname dept_partition.loc dept_partition.month
10 ACCOUNTING 1700 20190420
20 RESEARCH 1800 20190420
30 SALES 1900 20190420
40 OPERATIONS 1700 20190420
10 ACCOUNTING 1700 20190421
20 RESEARCH 1800 20190421
30 SALES 1900 20190421
40 OPERATIONS 1700 20190421
10 ACCOUNTING 1700 20190422
20 RESEARCH 1800 20190422
30 SALES 1900 20190422
40 OPERATIONS 1700 20190422
5.增加分区
创建单个分区
hive (default)> alter table dept_partition add partition(month='20190423') ;
同时创建多个分区
hive (default)> alter table dept_partition add partition(month='20190424') partition(month='20190425');

6.删除分区
删除单个分区
hive (default)> alter table dept_partition drop partition (month='20190423');
Dropped the partition month=20190423
OK
Time taken: 0.358 seconds
同时删除多个分区
hive (default)> alter table dept_partition drop partition (month='20190424'), partition (month='20190425');
Dropped the partition month=20190424
Dropped the partition month=20190425
OK
Time taken: 0.775 seconds

7.查看分区表有多少分区
hive (default)> show partitions dept_partition;
OK
partition
month=20190420
month=20190421
month=20190422
Time taken: 0.088 seconds, Fetched: 3 row(s)
8.查看分区表结构
hive (default)> desc dept_partition;
OK
col_name data_type comment
deptno int
dname string
loc string
month string
# Partition Information
# col_name data_type comment
month string
Time taken: 0.116 seconds, Fetched: 9 row(s)
分区表注意事项
1.创建二级分区表
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
2.正常的加载数据
(1)加载数据到二级分区表中
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept_partition2 partition(month='201904',day='20');

(2)查询分区数据
hive (default)> select * from dept_partition2 where month='201904' and day='20';
OK
dept_partition2.deptno dept_partition2.dname dept_partition2.locdept_partition2.month dept_partition2.day
10 ACCOUNTING 1700 201904 20
20 RESEARCH 1800 201904 20
30 SALES 1900 201904 20
40 OPERATIONS 1700 201904 20
Time taken: 0.128 seconds, Fetched: 4 row(s)
3.把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
(1)方式一:上传数据后修复
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201904/day=21;
hive (default)> dfs -put /root/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201904/day=21;

查询数据(查询不到刚上传的数据)
hive (default)> select * from dept_partition2 where month='201904' and day='21';
执行修复命令(因为只是上传了数据,但是hive中没有存储相关的元数据,修复之后就可以了)
hive> msck repair table dept_partition2;
再次查询数据
hive (default)> select * from dept_partition2 where month='201904' and day='21';
(2)方式二:上传数据后添加分区
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201904/day=22;
hive (default)> dfs -put /root/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201904/day=22;
执行添加分区
hive (default)> alter table dept_partition2 add partition(month='201904',day='22');
查询数据
hive (default)> select * from dept_partition2 where month='201904' and day='22';
(3)方式三:创建文件夹后load数据到分区
创建目录
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201904/day=23;
上传数据
hive (default)> load data local inpath '/root/datas/dept.txt' into table dept_partition2 partition(month='201904',day='23');
查询数据
hive (default)> select * from dept_partition2 where month='201904' and day='23';
修改表
重命名表
1.语法
ALTER TABLE table_name RENAME TO new_table_name
2.实操案例
hive (default)> alter table dept_partition2 rename to dept_partition3;
增加/修改/替换列信息
1.语法
更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
增加和替换列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
注:ADD是代表新增一字段,字段位置在所有列后面(partition列前),REPLACE则是表示替换表中所有字段。
2.实操案例
(1)查询表结构

(2)添加列
hive (default)> alter table dept_partition add columns(deptdesc string);
(3)查询表结构

(4)更新列
hive (default)> alter table dept_partition change column deptdesc desc int;
(5)查询表结构

(6)替换列(将列全部替换,相当于抛弃原来的结构)
hive (default)> alter table dept_partition replace columns(deptno string, dname string, loc string);
(7)查询表结构

删除表
hive (default)> drop table dept_partition;
本文详细介绍了Hive中的数据定义语言(DDL),包括创建、查询、修改和删除数据库及表的操作。重点讨论了创建表的语法,如内部表、外部表、分区表的创建,以及管理表和外部表之间的转换。同时,还涵盖了表的重命名、添加/修改/替换列信息等修改操作。
317

被折叠的 条评论
为什么被折叠?



