创建数据库
- 创建一个数据库,数据库在hdfs上的默认路径是/user/hive/warehouse/*.db
- 避免要创建的数据库已经存在错误,增加if not exist判断:create database if not exists db_hive
- 创建一个数据库,指定数据库在hdfs上存放的位置:create database db_hive2 location '/db_hive2.db'
说明:在hdfs下创建一个文件夹:hadoop fs -mkdir -p /database/hive3
查询数据库
- 显示数据库:show databases
- 过滤显示查询的数据库:show databases like 'db_hive'
- 显示数据库信息:desc database db_hive
- 显示数据库详细信息:desc database extended db_hive
- 切换当前数据库:use db_hive
- 修改数据库:alter database db_hive set dbproperties('createtime'='20170830'),说明:数据库的其他元数据信息都是不可以更改的,包括数据库名和数据库所在的目录位置
- 删除数据库:drop database db_hive2
- 如果删除的数据库不存在,最好采用if exists判断数据库是否存在:drop database if exists db_hive2
- 如果数据库不为空,可以采用cascade命令,强制删除:drop database db_hive cascade
创建表
- 建表语法
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], ...)] INFO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path]
字段解释说明
- CREATE TABLE 创建一个指定名字的表
- EXTERNAL关键字可以让用户创建一个外部表,在建表的同时指定一个实际路径(LOCATION),hive创建内部表时,会将数据移动到数据仓库指向路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部白哦的元数据和数据会被遗弃删除,而外部表只是删除元数据,不删除数据
- COMMENT为表和列添加注解
- PARTITIONED BY创建分区表
- CLUSTERED BY创建分桶表
- SORTED BY不常用
- STORED AS指定存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件),如果文件数据是纯文本,可以使用STORED AS TEXTFILE,如果数据需要压缩,使用STORED AS SEQUENCEFILE
- LOCATION:指定表在hdfs上的存储位置
- LIKE允许用户复制现有的表结构,但不复制数据
- 管理表
- 理论:默认创建的表都为管理表,有时候也被称为内部表,hive会(或多或少)控制着数据的生命周期。hive默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(如:/user/hive/warehouse)所定义的目录的子目录下,当删除一个管理表时,hive也会删除这个表中数据。管理表不适合和其他工具共享数据
- 创建表、根据查询结果创建表
//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
- 外部表
- 理论:删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉
- 管理表和外部表的使用场景:每天将收集到的网站日志定期流入hdfs文件,在外部表的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表
- 管理表与外部表的互相转换
- 查询表的类型:desc formatted student2
- 修改内部表student2为外部表:alter table student2 set tblproperties('EXTERNAL'='TRUE')
- 修改外部表student2为内部表:alter table student2 set tblproperties('EXTERNAL'='FALSE')
分区表
分区表实际上就是对应一个hdfs文件系统上的独立的文件夹,该文件夹下式该分区所有的数据文件。hive中的分区就是分目录,把一个大的数据集根基业务需要分割成小的数据集。在查询时通WHERE子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多
- 分区表的基本操作
- 引入分区表,需要根据日期对日志进行管理
/user/hive/warehouse/log_partition/20200401/20200401.log /user/hive/warehouse/log_partition/20200402/20200402.log /user/hive/warehouse/log_partition/20200403/20200403.log
- 创建分区表语法
create table dept_partition( deptno int, dname string, loc string ) partitioned by (month string) row format delimited fields terminated by '\t';
加载数据到分区表中
load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200401') load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200402') load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='20200403')
查询分区表中的数据
//1.单分区查询 select * from dept_partition where month='20200401' //2.多分区联合查询 select * from dept_partition where month='20200401' union select * from dept_partition where month='20200402' union select * from dept_partition where month='20200403'
增加分区
//1.创建单个分区 alter table dept_partition add partition(month='20200404') //2.同时创建多个分区 alter table dept_partition add partition(month='20200404') partition(month='20200405')
删除分区
//1.删除单个分区 alter table dept_partition drop partition(month='20200401') //2.同时删除多个分区 alter table dept_partition drop partition(month='20200401'), partition(month='20200402')
查看分区表有多少分区
show partitions dept_partition
查看分区表结构
desc formatted dept_partition
- 分区表注意事项
- 创建二级分区表
create table dept_partition2( deptno int, dname string, loc string ) partitioned by (month string, day string) row format delimited fields terminated by '\t';
- 正常的加载数据
//1.加载数据到二级分区表中 load data local inpath 'opt/module/datas/dept.txt' into table default.dept_partition2 partition(mobth='202004', day='13'); //2.查询分区数据 select * from dept_partition2 where month='202004' and day='13';
- 把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
//1.上传数据后台修复 //a.上传数据 dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13; dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=202004/day=13; //b.查询数据(查询不到刚上传的数据) select * from dept_partition2 where month='202004' and day='13' //c.执行修复命令 msck repair table dept_partition2; //d.再次查询数据 select * from dept_partition2 where month='202004' and day='13' //2.上传数据后添加分区 //a.上传数据 dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13; dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=202004/day=13; //b.执行添加分区 alter table dept_partition2 add partition(month='202004', day='13'); //c.查询数据 select * from dept_partition2 where month='202004' and day='13'; //3.创建文件后load数据到分区 //a.创建目录 dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202004/day=13; //b.上传数据 load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='202004', day='13'); //c.查询数据 select * from dept_partition2 where month='202004' and day='13';
修改表
- 重命名表
ALTER TABLE table_name RENAME TO new_table_name //如: alter table dept_partition2 rename to dept_partition3
- 增加、修改和删除分区:见以上分区表
- 增加/修改/替换列信息
//1.语法 //a.更新列 alter table table_name change [column] col_old_name col_new_name column_type [comment col_comment] [first|alter column_name] //b.增加和替换列 alter table table_name add|replace columns (col_name data_type [comment col_comment], ...) //2.练习 //a.查询表结构 desc dept_partition //b.添加列 alter table dept_partition add columns(deptdesc string) //c.更新列 alter table dept_partition change column deptdesc desc int //d.替换列 alter table dept_partition replace columns(deptno string, dname string, loc string)
说明:ADD是代表新增一字段,字段位置在所有列后面(partition列前),replace则是表示替换表中的所有字段
删除表
drop table dept_partition