今天学习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);