|NO.Z.00028|——————————|BigDataEnd|——|Hadoop&Hive.V28|——|Hive.v28|Hive优化策略|实战.v04|
一、优化实战
### --- 数据说明
~~~ 学生信息表(student_txt)定义如下:
~~~ # 创建数据库
hive (default)> create database tuning;
hive (default)> use tuning;
~~~ # 创建表
hive (tuning)> create table if not exists tuning.student_txt(
s_no string comment '学号',
s_name string comment '姓名',
s_birth string comment '出生日期',
s_age int comment '年龄',
s_sex string comment '性别',
s_score int comment '综合得分',
s_desc string comment '自我介绍'
)
row format delimited
fields terminated by '\t';
~~~ # 数据加载
hive (tuning)> load data local inpath '/home/hadoop/data/student/*.txt' into table
tuning.student_txt;
~~~ # 查询数据
~~~ 数据文件位置:/root/hive/student,50个文件,每个文件平均大小 40M 左右,包含4W条左右的信息;
hive (tuning)> select count(*) from tuning.student_txt;
Hadoop job information for Stage-1: number of mappers: 9; number of reducers: 1 //有9个mappers,1个reducers
~~~~~~~~~~
Stage-Stage-1: Map: 9 Reduce: 1 Cumulative CPU: 71.32 sec HDFS Read: 2193236480 HDFS Write: 107 SUCCESS
Total MapReduce CPU Time Spent: 1 minutes 11 seconds 320 msec
~~~~~~~~~~
2000000
二、SQL案例
### --- 查询 student_txt 表,每个年龄最晚出生和最早出生的人的出生日期,
~~~ 并将其存入表student_stat 中。student_stat 表结构如下:
hive (tuning)> create table student_stat
(age int, brith string)
partitioned by (tp string);
### --- 需要执行的SQL如下:
~~~ 开启动态分区
hive (tuning)> set hive.exec.dynamic.partition=true;
hive (tuning)> set hive.exec.dynamic.partition.mode=nonstrict;
~~~ # 插入数据
~~~ 备注:union all 重复的行不会被删除
hive (tuning)> insert overwrite table student_stat partition(tp)
select s_age, max(s_birth) stat, 'max' tp
from student_txt
group by s_age
union all
select s_age, min(s_birth) stat, 'min' tp
from student_txt
group by s_age;
~~输出参数
Hadoop job information for Stage-1: number of mappers: 9; number of reducers: 9
~~
_u1.s_age _u1.stat _u1.tp
~~~ # 静态分区:
~~~ 若分区的值是确定的,新增分区或者是加载分区数据时,指定分区名
~~~ # 动态分区:
~~~ 分区的值是非确定的,由输入数据来确定
~~~ hive.exec.dynamic.partition(默认值true),是否开启动态分区功能,默认开启
~~~ hive.exec.dynamic.partition.mode(默认值strict),动态分区的模式
~~~ strict 至少一个分区为静态分区
~~~ nonstrict 允许所有的分区字段都可以使用动态分区
~~~ # 问题1:SQL执行过程中有多少个Stage(job)
~~~ # 问题2:为什么在Stage-1、Stage-9中有9个Map Task、9个Reduce Task
~~~ # 问题3:SQL语句是否能优化,如何优化
三、执行计划
### --- 执行计划
~~~ SQL => MR
~~~ 解决上面这些问题,需要对 Hive 执行计划有一个基本的了解。
~~~ 有越来越多的 Hive 经验后,掌握 Hive 背后的理论知识以及底层实现细节,
~~~ 会让更加高效地使用Hive。要了解Hive 是如何工作的,需要了解EXPLAIN的功能,
~~~ 它能帮助我们了解 Hive 如何将查询转化成 Mapreduce 任务。
### --- Hive的执行计划不是最终真实的执行计划,但是对了解其中的细节仍然有帮助。
hive (tuning)> explain select * from student_txt;
~~输出参数
Explain
STAGE DEPENDENCIES:
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
TableScan
alias: student_txt
Statistics: Num rows: 4317304 Data size: 2193190912 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: s_no (type: string), s_name (type: string), s_birth (type: string), s_age (type: int), s_sex (type: string), s_score (type: int), s_desc (type: string)
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6
Statistics: Num rows: 4317304 Data size: 2193190912 Basic stats: COMPLETE Column stats: NONE
ListSink
hive (tuning)> explain select * from student_txt limit 10;
~~输出参数
Explain
STAGE DEPENDENCIES:
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: 10
Processor Tree:
TableScan
alias: student_txt
Statistics: Num rows: 4317304 Data size: 2193190912 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: s_no (type: string), s_name (type: string), s_birth (type: string), s_age (type: int), s_sex (type: string), s_score (type: int), s_desc (type: string)
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6
Statistics: Num rows: 4317304 Data size: 2193190912 Basic stats: COMPLETE Column stats: NONE
Limit
Number of rows: 10
Statistics: Num rows: 10 Data size: 5080 Basic stats: COMPLETE Column stats: NONE
ListSink
### --- 执行计划比较简单,只有一个Stage,这个Stage中只有Fetch Operator,读取数据。
hive (tuning)> explain select count(*) from student_txt limit 10;
~~输出参数
Explain
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-0 depends on stages: Stage-1
STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: student_txt
Statistics: Num rows: 1 Data size: 2193190912 Basic stats: COMPLETE Column stats: COMPLETE
Select Operator
Statistics: Num rows: 1 Data size: 2193190912 Basic stats: COMPLETE Column stats: COMPLETE
Group By Operator
aggregations: count()
mode: hash
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: COMPLETE
Reduce Output Operator
sort order:
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: COMPLETE
TopN Hash Memory Usage: 0.1
value expressions: _col0 (type: bigint)
Reduce Operator Tree:
Group By Operator
aggregations: count(VALUE._col0)
mode: mergepartial
outputColumnNames: _col0
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: COMPLETE
Limit
Number of rows: 10
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: COMPLETE
File Output Operator
compressed: false
Statistics: Num rows: 1 Data size: 8 Basic stats: COMPLETE Column stats: COMPLETE
table:
input format: org.apache.hadoop.mapred.SequenceFileInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
Stage: Stage-0
Fetch Operator
limit: 10
Processor Tree:
ListSink
### --- SQL语句select count(*) from student_txt limit 10 :
~~~ Stage-1、Stage-0,Stage-0 依赖 Stage-1
~~~ Stage-0 在结果集中取10条数据,显示在屏幕上
~~~ Stage-1 包含Map Task 和 Reduce Task
### --- 再看一个稍微复杂一点SQL语句的执行计划:
hive (tuning)> explain
select s_score, max(s_birth)
from student_txt
where s_age > 22
group by s_score
limit 88;
### --- 以下SQL语句的执行计划:
hive (tuning)> explain
select s_age, max(s_birth) stat, 'max' tp
from student_txt
group by s_age
union all
select s_age, min(s_birth) stat, 'min' tp
from student_txt
group by s_age;
### --- SQL有4个Stage,关系如下:
~~~ 其中:
~~~ Stage-1、Stage-3是root Stage,包含 Map Task、Reduce Task
~~~ Stage-2 合并前两个Stage的数据集
~~~ Stage-0将数据显示在屏幕上

五、执行计划小结:
### --- 执行计划小结:
~~~ 一条 Hive SQL 语句会包含一个或多个Stage,不同的 Stage 间会存在着依赖关系。
~~~ 越复杂的查询有越多的Stage,Stage越多就需要越多的时间时间来完成。
~~~ 一个Stage可以是:Mapreduce任务(最耗费资源)、Move Operator(数据移动)、
~~~ Stats-Aggr Operator(搜集统计数据)、Fetch Operator(读取数据)等;
~~~ 默认情况下,Hive一次只执行一个stage。
### --- 执行计划关键词信息说明:
~~~ Map Reduce:表示当前任务所用的计算引擎是 MapReduce
~~~ Map Operator Tree:表示当前描述的 Map 阶段执行的操作信息
~~~ Reduce Operator Tree:表示当前描述的 Reduce 阶段执行的操作信息
### --- Map/Reduce Operator Tree 关键信息说明:
~~~ TableScan:表示对关键字 alias 声明的结果集进行扫描
~~~ Statistics:表示当前 Stage 的统计信息,这个信息通常是预估值
~~~ Filter Operator:表示在数据集上进行过滤
~~~ predicate:表示在 Filter Operator 进行过滤时,所用的谓词
~~~ Select Operator:表示对结果集上对列进行投影,即筛选列
~~~ expressions:表示需要投影的列,即筛选的列
~~~ outputColumnNames:表示输出的列名
~~~ Group By Operator:表示在结果集上分组聚合
~~~ aggregations:表示分组聚合使用的算法
~~~ keys:分组的列
~~~ Reduce Output Operator:表示当前描述的是对之前结果聚合后的信息
~~~ key expressions / value expressions:Map阶段输出key、value所用的数据列
~~~ sort order:是否进行排序,+ 正序,- 倒序
~~~ Map-reduce partition columns:Map 阶段输出到 Reduce 阶段的分区列
~~~ compressed:文件输出的结果是否进行压缩
~~~ input format / output format:输入输出的文件类型
~~~ serde:数据序列化、反序列化的方式
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通