启动hive服务端程序: hive --service metastore
启动hive客户端程序: hive --service hiveserver2 (java客户端可以调用此hive服务)
进入命令模式:
./hive
查看库
show databases;
创建库
create database hive;
创建表
use hive;
create table bus_receiver(a string,b int);
show tables; --查看表
desc 表名; # 显示表结构
show partitions 表名; # 显示表名的分区
show create table_name; # 显示创建表的结构
drop table table_name; # 删除表
# 建表语句
# 内部表
use xxdb; create table xxx;
# 创建一个表,结构与其他一样
create table xxx like xxx;
# 外部表
use xxdb; create external table xxx;
# 分区表
use xxdb; create external table xxx (l int) partitoned by (d string)
# 内外部表转化
alter table table_name set TBLPROPROTIES ('EXTERNAL'='TRUE'); # 内部表转外部表
alter table table_name set TBLPROPROTIES ('EXTERNAL'='FALSE');# 外部表转内部表
# 表结构修改
# 重命名表
use xxxdb; alter table table_name rename to new_table_name;
# 增加字段
alter table table_name add columns (newcol1 int comment ‘新增’);
# 修改字段
alter table table_name change col_name new_col_name new_type;
# 删除字段(COLUMNS中只放保留的字段)
alter table table_name replace columns (col1 int,col2 string,col3 string);
# 删除表
use xxxdb; drop table table_name;
# 删除分区
# 注意:若是外部表,则还需要删除文件(hadoop fs -rm -r -f hdfspath)
alter table table_name drop if exists partitions (d='2016-07-01');
查询分区信息: show partitions 表名