大数据平台和Hivesql
Hive 简介与实战教学:应用场景、语法操作与最佳实践
Hive 是什么?
Apache Hive 是构建在 Hadoop 上的数据仓库系统,最初由 Facebook 设计开发,现由 Apache 软件基金会维护。它的主要目标是让用户用类 SQL 的语言(HQL)来处理大规模数据集,即便没有 Java、Scala 编程经验,也可以轻松对大数据进行分析。
Hive 能将 HQL(Hive Query Language)转译为底层的 MapReduce 或 Spark 任务,在 Hadoop 的分布式架构中运行,具备良好的可扩展性和兼容性。
Hive 核心功能
- 将 HDFS(Hadoop 分布式文件系统)中的结构化数据映射为数据库表
- 用类似 SQL 的 HQL 编写查询语句,自动转换为底层 MapReduce 或 Spark 执行计划
这意味着开发者可以用熟悉的 SQL 技术栈在 Hadoop 上进行复杂的数据分析,无需深入底层代码。
Hive 的应用场景
✅ 适合做什么?
- 日志数据分析(如:用户行为日志、点击流)
- 数据仓库构建(维度建模、报表分析)
- 批量数据离线处理(结合定时调度系统)
- 数据导入导出(例如和 Flume、Kafka、Sqoop 结合)
❌ 不适合做什么?
- 实时查询或低延迟响应(Hive 执行延迟高)
- 高并发小查询(如 OLTP 系统)
- 行级别的数据修改(不支持 Update、Delete)
- 强事务需求
Hive 与传统 RDBMS 的对比
特性 | Hive | 关系型数据库(RDBMS) |
---|---|---|
查询语言 | HQL(Hive SQL) | SQL |
数据存储 | HDFS(分布式) | 本地文件系统 |
执行引擎 | MapReduce / Spark / Tez | 数据库执行器 |
执行延迟 | 高 | 低 |
数据规模 | 大数据(PB 级) | 中小数据(GB\~TB) |
编程语言依赖 | 无需编程(类 SQL) | 通用 |
Hive 使用准备:数据平台搭建与数据准备
步骤一:构建 Hadoop + Hive 平台
- 安装 Hadoop、HDFS、Hive、YARN 等组件。
- 搭建客户端跳板机(Client)远程连接集群。
步骤二:数据准备
- 创建 Hadoop 用户目录:
bash
hdfs dfs -mkdir /user/root
- 上传本地数据至 HDFS:
bash
hdfs dfs -put /home/ubuntu/data/* /user/root
- 启动 Hive 命令行:
bash
hive
Hive 建库建表操作教学
1️⃣ 数据库操作
create database eshop; -- 创建数据库
drop database eshop cascade; -- 删除数据库
use eshop; -- 切换数据库
2️⃣ 数据类型(字段设计)
基本类型
类型 | 描述 |
---|---|
int |
整数 |
double |
浮点 |
string |
字符串 |
timestamp |
时间戳 |
boolean |
布尔值 |
复合类型
类型 | 描述 |
---|---|
array |
数组,如 array<int> |
map |
键值对,如 map<string,string> |
struct |
结构体,如 struct<name:string, age:int> |
3️⃣ 表创建与数据加载
创建内部表
create table if not exists dim_user_info
(
user_id string,
user_name string,
sex string,
age int,
city string,
firstactivetime string,
level int,
extra1 string,
extra2 map<string,string>
)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile;
加载数据(从本地或 HDFS)
-- 本地加载
load data local inpath '/home/ubuntu/data/user_info/user_info.txt' overwrite into table dim_user_info;
-- HDFS 加载
load data inpath '/user/root/user_info.txt' overwrite into table dim_user_info;
4️⃣ 创建分区表与动态分区
分区建表
create table if not exists fact_user_trade
(
user_name string,
piece int,
price double,
pay_amount double,
goods_category string,
pay_time bigint
)
partitioned by (dt string)
row format delimited fields terminated by '\t';
加载分区数据
hdfs dfs -put /home/ubuntu/data/user_trade/* /user/hive/warehouse/eshop.db/fact_user_trade
启用动态分区
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions=10000;
set hive.exec.max.dynamic.partitions.pernode=10000;
修复分区表
msck repair table fact_user_trade;
Hive 查询实操教学
基本查询语法
-- 查询北京的女性用户(取前 10)
select user_name from dim_user_info
where city='beijing' and sex='female'
limit 10;
-- 查询2019年3月24日 food 类购买记录
select user_name, piece, pay_amount
from fact_user_trade
where dt='2019-03-24' and goods_category='food';
分组与聚合查询(GROUP BY)
-- 查询2019年1-4月每类商品购买人数与总金额
select goods_category,
count(distinct user_name) as total_user,
sum(pay_amount) as total_pay
from fact_user_trade
where dt between '2019-01-01' and '2019-04-30'
group by goods_category;
-- 查询 4 月支付金额 > 5 万的用户
select user_name, sum(pay_amount) as total
from fact_user_trade
where dt between '2019-04-01' and '2019-04-30'
group by user_name
having sum(pay_amount) > 50000;
排序(ORDER BY)
-- 查询2019年4月支付金额最高的前5用户
select user_name, sum(pay_amount) as total
from fact_user_trade
where dt between '2019-04-01' and '2019-04-30'
group by user_name
order by total desc
limit 5;
常用函数与分析用例
时间处理函数
from_unixtime(pay_time, 'yyyy-MM-dd')
: 时间戳转日期datediff(date1, date2)
: 日期差值
-- 计算用户 ELLA 在2018年的消费间隔天数
select sum(pay_amount) as total,
datediff(max(from_unixtime(pay_time, 'yyyy-MM-dd')), min(from_unixtime(pay_time, 'yyyy-MM-dd')))
from fact_user_trade
where year(dt)='2018' and user_name='ELLA';
条件函数(IF / CASE)
-- 按年龄段统计用户数
select case
when age < 20 then '20岁以下'
when age < 30 then '30岁以下'
when age < 40 then '40岁以下'
else '40岁以上'
end as age_seg,
count(distinct user_id) as total
from dim_user_info
group by case
when age < 20 then '20岁以下'
when age < 30 then '30岁以下'
when age < 40 then '40岁以下'
else '40岁以上'
end;
字符串函数与 JSON 解析
-- 从 JSON 中提取手机品牌
select get_json_object(extra1, '$.phonebrand') as phone,
count(distinct user_id) as total
from user_info
group by get_json_object(extra1, '$.phonebrand');
-- 或者使用 map 提取
select extra2['phonebrand'] as phone,
count(distinct user_id) as total
from user_info
group by extra2['phonebrand'];
Hive 最佳实践建议
场景 | 建议做法 | 理由 |
---|---|---|
数据入湖 | 使用分区表 + 动态分区 | 提升查询效率 |
日志处理 | 使用 TEXTFILE 或 ORC 存储 | ORC 压缩好,适合分析型任务 |
查询性能 | 使用 LIMIT + WHERE 限制查询量 | 避免扫描全表 |
用户查询 | 使用 COUNT DISTINCT 限定唯一性 | 支持人群画像分析 |
JSON字段 | 建议使用 map 类型 |
查询更快、语法更简洁 |
定时任务 | Hive SQL + Oozie/Azkaban/Scheduler | 实现离线数仓调度 |
可视化 | 接入 Superset、Tableau、Airflow | 输出可解释图表报表 |
总结
Hive 是大数据生态中连接 SQL 开发者与 Hadoop 的桥梁。它不仅降低了数据处理门槛,还提供了结构化建模与分析的强大能力。通过上述内容,我们已覆盖:
- Hive 的原理与定位
- 核心功能与使用场景
- 建库建表到分区数据的完整流程
- 查询语法、函数与数据分析技巧
- 实用最佳实践与项目建议
如果你想进一步构建离线数仓体系、实时数仓架构,或连接 BI 工具构建报表,Hive 是不可或缺的基础工具之一。欢迎继续深入学习如 ORC 文件优化、Hive on Tez/Spark 等高级内容。