10-8每日博客
今天学习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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具