Hive基础知识(一)
发布时间
阅读量:
阅读量
一、SQL DDL
1、启动hive
首先使用【jps】查看hive是否启动
# 启动hiveserver2
hive --service hiveserver2 &
# 启动metastore
hive --service metastore &
2、进入hive
# 方法一:beeline方式(推荐)
beeline -u jdbc:hive2://node03:10000 -n root
# 方法二:hive cli方式
hive
3、数据库基本操作
- 创建数据库
create database if not exists db_hive
comment '测试数据库建库语句'
with DBPROPERTIES('propertyname'='stan');
- 列出所有数据库
show databases;
- 切换数据库
use db_hive;
- 查看当前数据库
select current_database();
- 查看数据库详情
desc database db_hive;
desc database extended db_hive;
- 删除数据库
drop database if exists db_hive;
4、表的基本操作
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS]
[<database_name>.]<table_name>
[(<col_name> <data_type> [COMMENT '<col_comment>'] [, <col_name> <data_type> ...])]
[COMMENT '<table_comment>']
[PARTITIONED BY (<partition_key> <data_type> [COMMENT '<partition_comment>']
[, <partition_key > <data_type>...])]
[CLUSTERED BY (<col_name> [, <col_name>...])
[SORTED BY (<col_name> [ASC|DESC] [, <col_name> [ASC|DESC]...])]
INTO <num_buckets> BUCKETS]
[
[ROW FORMAT <row_format>]
[STORED AS file_format]
| STORED BY '<storage.handler.class.name>' [WITH SERDEPROPERTIES (<...>)]
]
[LOCATION '<file_path>']
[TBLPROPERTIES ('<property_name>'='<property_value>', ...)];
1)创建表
- 内表
drop table if exists tbl_inner;
create table tbl_inner(
stu_id int comment '学生id',
stu_name string comment '学生姓名'
) comment '学生表';
# 查看表的元数据信息
desc formatted tbl_inner;

- 外表
# tbl_outer.txt 上传到hdfs
hadoop fs -mkdir -p /tmp/db_hive/tbl_outer
hadoop fs -put tbl_outer.txt /tmp/db_hive/tbl_outer/
-- 对这些数据创建外表
drop table if exists tbl_outer;
create external table tbl_outer(
stu_id int comment '学生id',
stu_name string comment '学生姓名',
subject array<string> comment '科目',
address map<string,string> comment '籍贯'
)comment '个人信息表'
row format delimited fields terminated by ','
collection items terminated by '-'
lines terminated by '\n'
location '/tmp/db_hive/tbl_outer/'
;
-- 查看表结构并查看数据
desc tbl_outer;
select * from tbl_outer;
-- 查看表的详细元数据信息
desc formatted tbl_outer;

- 临时表
create temporary table tbl_tmp(
stu_id int,
stu_name string
);

2)复制表结构
create table tbl_like like tbl_inner;
3)复制表结构和数据
create table tbl_as as select * from tbl_inner;
4)删除表
drop table tbl;
5)修改表
/* 表重命名 */
ALTER TABLE <table_name> RENAME TO <new_table_name>;
/* 修改表属性 */
ALTER TABLE <table_name> SET TBLPROPERTIES ('<property_name>' = '<property_value>' ... );
ALTER TABLE <table_name> SET SERDEPROPERTIES ('<property_name>' = '<property_value>' ... );
ALTER TABLE <table_name> SET LOCATION '<new_location>';
/* 增加、删除、修改、替换列 */
ALTER TABLE <table_name> ADD COLUMNS (<col_spec> [, <col_spec> ...])
ALTER TABLE <table_name> DROP [COLUMN] <col_name>
ALTER TABLE <table_name> CHANGE <col_name> <new_col_name> <new_col_type>
ALTER TABLE <table_name> REPLACE COLUMNS (<col_spec> [, <col_spec> ...])
6)清空表
truncate table tbl;
7)查看表详情
desc tbl;
8)查看所有表
show tables;
全部评论 (0)
还没有任何评论哟~
