Hive的數據庫和表操作
一、Hive數據庫操作
1.1 查看數據庫
show databases;
使用like關鍵字模糊匹配
# 顯示包含db_前綴的數據庫名稱
show databases like 'db_*';
1.2 使用數據庫
use database名稱
1.3 創建數據庫
create database dbname;
通過location指定數據庫路徑
create database dbname location 'path路徑';
給數據庫添加描述信息
create database dbname comment 'dbname描述信息';
1.4 刪除數據庫
# 刪除數據庫,這種刪除,需要將數據庫中的表全部刪除,才能刪除數據庫
drop database dbname;
或者
drop database if exists dbname;
cascade強制刪除
# 強制刪除數據庫
drop database dbname cascade;
1.5 查看數據庫的詳細描述
desc database dbname;
destribe database dbname;
結果如圖:
二、Hive表操作
2.1 顯示數據庫中的表
show tables;
使用like模糊匹配,查詢包含tb_前綴的表
show tables like 'tb_*';
或者
show tables 'tb_*';
2.1.1 顯示錶的分區
show partitions tb_test;
2.2 顯示錶的詳細信息
desc tb_name;
describe tb_name;
2.3 創建表
建表語法:
create [external] table [if not exists] table_name (
col_name data_type [comment '字段描述信息']
col_name data_type [comment '字段描述信息'])
[comment '表的描述信息']
[location '指定表的路徑']
[partitioned by (col_name data_type,...)]
[clustered by (col_name,col_name,...)]
[sorted by (col_name [asc|desc],...) into num_buckets buckets]
[row format row_format]
[location location_path]
2.2.1 簡單的表創建
create table tb_test(name string, age int);
2.2.2 指定字段分隔符
create table tb_test(name string,age int)
row format delimited fields terminated by ',';
2.2.3 創建外部表
create external table tb_test(name string,age int)
row format delimited fields terminated by ',';
2.2.4 創建分區表
create table tb_part(name string,age int)
partitioned by (sex string)
row format delimited fields terminated by ',';
2.2.5 創建表,指定location
create table tb_location(name string,age int)
row format delimited fields terminated by ','
location 'hdfs://192.168.100.11:9000/user/hive/tables/';
2.2.6 創建帶桶的表
create table student(id int,name string,age int)
partitioned by (sex string)
clustered by(id)
sorted by (age) into 2 buckets
row format delimited fields terminated by ',';
2.3 刪除表
drop table tb_name;
drop table if exists tb_name;
2.4 修改表
2.4.1 添加分區
# 按照sex='male',sex='female'進行分區
alter table student add partition(sex='male') partition(sex='female');
2.4.2 刪除分區
alter table student drop partition(sex='male');
2.4.3 重命名錶
alter table table_name rename to new_table_name;
2.4.4 增加列
alter table student add columns (rank string);
或者
alter table student replace columns (height string);
補充:hive使用shell命令和dfs命令
hive中使用shell命令
在hive客戶端中,可以通過前面添加!可以使用shell命令,如圖:
hive中使用dfs命令