前言
本文主要介绍hive 中操作库和表的语句。
hive 库操作
- 查看数据库:show databases
- 创建数据库:create database if not exists hive_testdb
- 使用某个数据库:use hive_testdb
- 查询数据库:show databases like ‘hive*’
- 显示数据库的信息:describe database hive_testdb
- 添加数据库备注:create database teacherdb comment
- 删除空数据库:drop database hive_testdb
- 强制删除数据库:drop database hive_testdb casecade
hive表操作
- 查看表的列表:show tables
- 创建表:create table if not exists hive_db.table_test(id string, name string, age int, …)
- 删除表:drop table if exists table_test
- 重命名表:alter table table_test rename to table1
- 修改表中列信息:alter table table_test change columns id tb_id int
- 增加列:alter table table_test add columns(class string commet “班级”)
- 删除或者替换列:alter table table_test replace columns(id string commet “备注”,name string commet “备注”);//这里是将所用列全部删除后再新建id和name列
- 复制表及数据:create table table_test1 as select * from table_test
- 导入表数据:load data local inpath ‘xxx(数据目录)/data/xxx.xxx’ overwrite into table hive_db.table_test
- 查询表:show tables in hive_db like “table*”
- 查看表信息:desc table_test
- 内部表与外部表的相互转换:
alter table table_test set tblproperties(“EXTERNAL”=“TRUE”) //内部表转换为外部表
alter table table_test set tblproperties(“EXTERNAL”=“FALSE”) //外部表转换为内部表 - 分区表(物理分区,逻辑上还是整表)
创建分区表(按照月份来分区):create table table_partition(id string,name string) partitioned by (month string) row format delimited fields terminated by ‘\t’
上传数据到分区表:load data local inpath ‘xxx(数据目录)/data/xxx.xxx’ into table table_partition partition(month=“201909”)
查找分区表:select * from table_partition //查找分区表中的所有记录
select * from table_partition where month=“201909” //查找分区表中分区名201909中的所有记录
查看分区:show partitions table_partition
增加分区:alter table table_partition add partition (month=“201910”)
alter table table_partition add partition (month=“201911”), partition (month=“201912”)
删除分区:alter table table_partition drop partition(month=“201909”)
ps:二级分区指的是2个分区字段,按照字段的顺序来设置分区顺序,比如:partition(month=“201909”,day=“01”)就是一个二级分区,目录结构中day是month文件夹的子文件夹。