A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

Hive的数据库和表操作

一、Hive数据库操作

1.1 查看数据库

show databases;
1
使用like关键字模糊匹配

# 显示包含db_前缀的数据库名称
show databases like 'db_*';
1
2
1.2 使用数据库

use database名称
1
1.3 创建数据库

create database dbname;
1
通过location指定数据库路径

create database dbname location 'path路径';
1
给数据库添加描述信息

create database dbname comment 'dbname描述信息';
1
1.4 删除数据库

# 删除数据库,这种删除,需要将数据库中的表全部删除,才能删除数据库
drop database dbname;
或者
drop database if exists dbname;
1
2
3
4
cascade强制删除

# 强制删除数据库
drop database dbname cascade;
1
2
1.5 查看数据库的详细描述

desc database dbname;
destribe database dbname;
1
2
结果如图:



二、Hive表操作

2.1 显示数据库中的表

show tables;
1
使用like模糊匹配,查询包含tb_前缀的表

show tables like 'tb_*';
或者
show tables 'tb_*';
1
2
3
2.1.1 显示表的分区

show partitions tb_test;
1
2.2 显示表的详细信息

desc tb_name;
describe tb_name;
1
2
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]
1
2
3
4
5
6
7
8
9
10
2.2.1 简单的表创建

create table tb_test(name string, age int);
1
2.2.2 指定字段分隔符

create table tb_test(name string,age int)
row format delimited fields terminated by ',';
1
2
2.2.3 创建外部表

create external table tb_test(name string,age int)
row format delimited fields terminated by ',';
1
2
2.2.4 创建分区表

create table tb_part(name string,age int)
partitioned by (sex string)
row format delimited fields terminated by ',';
1
2
3
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/';
1
2
3
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 ',';
1
2
3
4
5
2.3 删除表

drop table tb_name;

drop table if exists tb_name;
1
2
3
2.4 修改表

2.4.1 添加分区

# 按照sex='male',sex='female'进行分区
alter table student add partition(sex='male') partition(sex='female');
1
2
2.4.2 删除分区

alter table student drop partition(sex='male');
1
2.4.3 重命名表

alter table table_name rename to new_table_name;
1
2.4.4 增加列

alter table student add columns (rank string);
或者
alter table student replace columns (height string);
1
2
3
补充:hive使用shell命令和dfs命令

hive中使用shell命令
在hive客户端中,可以通过前面添加!可以使用shell命令,如图:



hive中使用dfs命令

具体见博主原博客
---------------------
【转载,仅作分享,侵删】
作者:张行之
来源:CSDN
原文:https://blog.csdn.net/qq_33689414/article/details/80063687
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马