Hive - SQL

 

 

BEELINE 访问 Hive CLI

数据定义语言(DDL)

数据操作语言(DML)

事务控制语言(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;

 

posted @ 2023-05-31 10:22  HOUHUILIN  阅读(17)  评论(0编辑  收藏  举报