Hive 实用指令集锦
数据定义
- 创建数据库:
CREATE DATABASE IF NOT EXISTS database_name;
- 删除数据库:
DROP DATABASE IF EXISTS database_name;
- 查看数据库:
SHOW DATABASES;
- 使用数据库:
USE database_name;
- 创建表:
CREATE TABLE table_name (column1 data_type, column2 data_type, ...);
- 查看表:
SHOW TABLES;
- 查看表结构:
DESCRIBE table_name;
- 删除表:
DROP TABLE table_name;
数据操作
- 加载数据:
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE table_name;
- 插入数据:
INSERT INTO TABLE table_name VALUES (value1, value2, ...);
- 查询数据:
SELECT column1, column2, ... FROM table_name [WHERE conditions];
- 更新数据:
UPDATE table_name SET column1 = value1, column2 = value2, ... [WHERE conditions];
- 删除数据:
DELETE FROM table_name [WHERE conditions];
数据查询
- 过滤数据:
SELECT * FROM table_name WHERE column1 > 10;
- 排序数据:
SELECT * FROM table_name ORDER BY column1 ASC/DESC;
- 分组数据:
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
- 连接查询:
SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column1;
其他实用指令
- 查看分区:
SHOW PARTITIONS table_name;
- 添加分区:
ALTER TABLE table_name ADD PARTITION (partition_spec);
- 查看函数:
SHOW FUNCTIONS;
- 查看设置:
SET param_name;
日常操作示例
- 统计表中数据量:
SELECT COUNT(*) FROM table_name;
- 查询特定日期分区的数据:
SELECT * FROM table_name WHERE day='20231115';
- 将查询结果保存到新表:
CREATE TABLE new_table AS SELECT * FROM table_name;