Hive的数据库和表操作

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;

结果如图:

image

二、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命令,如图:

image

  • hive中使用dfs命令

image

猜你喜欢

转载自blog.csdn.net/qq_33689414/article/details/80063687