前言
本文主要介紹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文件夾的子文件夾。