Hive - SQL
事务控制语言(TCL)
数据控制语言(DCL)
BEELINE 访问 Hive CLI
# 使用beeline访问Hive的CLI
beeline -u jdbc:hive2://localhost:10000 -n hive -p 123456 --maxWidth=10000
数据定义语言(DDL)
database
-- 删除数据库
-- CASCADE是一个关键字,表示在删除对象(如表)时,如果该对象依赖于其他对象(如外键),则也会删除这些依赖的对象。
drop database if exists dbName CASCADE;
-- 创建数据库
create database if not exists dbName;
-- 修改数据库owner
alter database dbName set owner user harley;
-- 切换数据库
use dbName;
table
-- 创建内部表
create table test(id int,name string);
-- 创建textfile表,并指定字段分隔符
create table default.text_test(id int,name string) row format delimited fields terminated by ',' stored as textfile;
-- 创建orc表
create table default.orc_test(id int,name string) stored as orc;
-- 创建torc表
create table default.torc_test(id int,name string) clustered by (id) into 3 buckets stored as orc tblproperties("transactional"="true");
-- 创建torc分区表
create table default.torc_test(id int,name string) partitioned by (level string) clustered by (id) into 3 buckets stored as orc tblproperties("transactional"="true");
-- 创建ElasticSearch表
create table default.es_test (id string,name string) STORED AS ES WITH SHARD NUMBER 3 REPLICATION 1;
-- 创建Hyperdrive表
create table default.hyper_test(id int,name string) STORED AS HYPERDRIVE;
数据操作语言(DML)
-- 将本地数据into到表中
load data local inpath '/home/tblName.txt' [overwrite] into table tblName;
-- 将HDFS上的数据into到表中
load data inpath '/source/data/tblName.txt' [overwrite] into table tblName;
insert into table tblNameA select xxx from tblNameB;
-- 向表中插入数据并覆盖原数据
insert overwrite table tblA select * from tblB;
-- 写入本地文件系统
insert overwrite local directory '/home/halrey/' select * from user_info;
-- 写入HDFS
insert overwrite directory '/source/data2023/' select * from user_info;
-- 获取表中所有行以及所有列(查全部)
select * from tblName;
-- 指定某列去重查询
select distinct a1 from tblName;
事务控制语言(TCL)
数据控制语言(DCL)
-- 显示当前数据库
select current_database();
-- 设置hive属性在命令行显示当前数据库
set hive.cli.print.current.db=true;
-- 查看某张分区表的所有分区
show partitions tblName;
-- 删除某张分区表的某个分区
alter table tblName drop partition(<partition_column> = "partition_value");
DESC
0: jdbc:hive2://localhost:10000> desc tblName;
0: jdbc:hive2://localhost:10000> desc formatted tblName;
0: jdbc:hive2://localhost:10000> desc extended tblName;
SET
-- 查看参数agent.host的值
0: jdbc:hive2://localhost:10000> set agent.host;
-- 设置值:设置hive属性在命令行显示当前数据库
0: jdbc:hive2://localhost:10000> set hive.cli.print.current.db=true;
hive自带数据库
system
-- 查看数据库system中的表/视图
show tables from system;
-- 可以查看hive中所有库表信息
select * from system.tables_v;
-- 可以查看hive中所有库表的大小GB
select * from system.tables_stat_v;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南