首页 > 其他 > 详细

Hadoop基础(五十一):HIVE企业级调优(一)

时间:2020-07-24 22:15:58      阅读:85      评论:0      收藏:0      [点我收藏+]

1 Fetch 抓取

Fetch 抓取是指,Hive 中对某些情况的查询可以不必使用 MapReduce 计算。例如:
SELECT * FROM employees;在这种情况下,Hive 可以简单地读取 employee 对应的存储目录下的文件,然后输出查询结果到控制台。
在 hive-default.xml.template 文件中 hive.fetch.task.conversion 默认是 more,老版本 hive默认是 minimal,该属性修改为 more 以后,在全局查找、字段查找、limit 查找等都不走mapreduce。
<property>
 <name>hive.fetch.task.conversion</name>
 <value>more</value>
 <description>
 Expects one of [none, minimal, more].
 Some select queries can be converted to single FETCH task minimizing 
latency.
 Currently the query should be single sourced not having any subquery and 
should not have
 any aggregations or distincts (which incurs RS), lateral views and joins.
 0. none : disable hive.fetch.task.conversion
 1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
 2. more : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual 
columns)
 </description>
 </property>
案例实操:
1)把 hive.fetch.task.conversion 设置成 none,然后执行查询语句,都会执行 mapreduce程序。
hive (default)> set hive.fetch.task.conversion=none;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;
2)把 hive.fetch.task.conversion 设置成 more,然后执行查询语句,如下查询方式都不会执行 mapreduce 程序。
hive (default)> set hive.fetch.task.conversion=more;
hive (default)> select * from emp;
hive (default)> select ename from emp;
hive (default)> select ename from emp limit 3;

2 本地模式

大多数的 Hadoop Job 是需要 Hadoop 提供的完整的可扩展性来处理大数据集的。不过,有时 Hive 的输入数据量是非常小的。在这种情况下,为查询触发执行任务消耗的时间可能会比实际 job 的执行时间要多的多。对于大多数这种情况,Hive 可以通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间可以明显被缩短
       用户可以通过设置 hive.exec.mode.local.auto 的值为 true,来让 Hive 在适当的时候自动启动这个优化,默认是 false。
set hive.exec.mode.local.auto=true; //开启本地 mr
//设置 local mr 的最大输入数据量,当输入数据量小于这个值时采用 local mr 的方式,默认134217728,即 128M
set hive.exec.mode.local.auto.inputbytes.max=50000000;
//设置 local mr 的最大输入文件个数,当输入文件个数小于这个值时采用 local mr 的方式,默
认为 4
set hive.exec.mode.local.auto.input.files.max=10;
案例实操:
1)开启本地模式,并执行查询语句
hive (default)> set hive.exec.mode.local.auto=true;
hive (default)> select * from emp cluster by deptno;
Time taken: 1.328 seconds, Fetched: 14 row(s)
2)关闭本地模式,并执行查询语句
hive (default)> set hive.exec.mode.local.auto=false;
hive (default)> select * from emp cluster by deptno;
Time taken: 20.09 seconds, Fetched: 14 row(s)

3 表的优化

3.1 小表、大表 Join
将 key 相对分散,并且数据量小的表放在 join 的左边,这样可以有效减少内存溢出错误发生的几率;再进一步,可以使用 map join 让小的维度表(1000 条以下的记录条数)先进内存。在 map 端完成 reduce。
实际测试发现:新版的 hive 已经对小表 JOIN 大表和大表 JOIN 小表进行了优化。小表放在左边和右边已经没有明显区别。
案例实操
1.需求
测试大表 JOIN 小表和小表 JOIN 大表的效率
2.建大表、小表和 JOIN 后表的语句
// 创建大表
create table bigtable(id bigint, time bigint, uid string, keyword string, 
url_rank int, click_num int, click_url string) row format delimited fields 
terminated by \t;
// 创建小表
create table smalltable(id bigint, time bigint, uid string, keyword 
string, url_rank int, click_num int, click_url string) row format delimited 
fields terminated by \t;
// 创建 join 后表的语句
create table jointable(id bigint, time bigint, uid string, keyword 
string, url_rank int, click_num int, click_url string) row format delimited 
fields terminated by \t;
3.分别向大表和小表中导入数据
hive (default)> load data local inpath /opt/module/datas/bigtable into table 
bigtable;
hive (default)>load data local inpath /opt/module/datas/smalltable into table 
smalltable;
4.关闭 mapjoin 功能(默认是打开的)
set hive.auto.convert.join = false;
5.执行小表 JOIN 大表语句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
left join bigtable b
on b.id = s.id;
Time taken: 35.921 seconds
No rows affected (44.456 seconds)
6.执行大表 JOIN 小表语句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable b
left join smalltable s
on s.id = b.id;
Time taken: 34.196 seconds
No rows affected (26.287 seconds)
3.2 大表 Join 大表
1.空 KEY 过滤
有时 join 超时是因为某些 key 对应的数据太多,而相同 key 对应的数据都会发送到相同的 reducer 上,从而导致内存不够。此时我们应该仔细分析这些异常的 key,很多情况
下,这些 key 对应的数据是异常数据,我们需要在 SQL 语句中进行过滤。例如 key 对应的字段为空,操作如下:
案例实操
(1)配置历史服务器
配置 mapred-site.xml
<property>
<name>mapreduce.jobhistory.address</name>
<value>hadoop102:10020</value>
</property>
<property>
 <name>mapreduce.jobhistory.webapp.address</name>
 <value>hadoop102:19888</value>
</property>
启动历史服务器
sbin/mr-jobhistory-daemon.sh start historyserver
查看 jobhistory
http://hadoop102:19888/jobhistory
(2)创建原始数据表、空 id 表、合并后数据表
// 创建原始表
create table ori(id bigint, time bigint, uid string, keyword string, url_rank 
int, click_num int, click_url string) row format delimited fields terminated by 
\t;
// 创建空 id 表
create table nullidtable(id bigint, time bigint, uid string, keyword string,
url_rank int, click_num int, click_url string) row format delimited fields 
terminated by \t;
// 创建 join 后表的语句
create table jointable(id bigint, time bigint, uid string, keyword string, 
url_rank int, click_num int, click_url string) row format delimited fields 
terminated by \t;
(3)分别加载原始数据和空 id 数据到对应表中
hive (default)> load data local inpath /opt/module/datas/ori into table ori;
  hive (default)> load data local inpath ‘/opt/module/datas/nullid‘ into table nullidtable;
(4)测试不过滤空 id
 hive (default)> insert overwrite table jointable select n.* from nullidtable n left join ori o on n.id = o.id; 
Time taken: 42.038 seconds
Time taken: 37.284 seconds
(5)测试过滤空 id
hive (default)> insert overwrite table jointable select n.* from (select * from
nullidtable where id is not null ) n left join ori o on n.id = o.id;
Time taken: 31.725 seconds
Time taken: 28.876 seconds
2.空 key 转换
有时虽然某个 key 为空对应的数据很多,但是相应的数据不是异常数据,必须要包含在join 的结果中,此时我们可以表 a 中 key 为空的字段赋一个随机的值,使得数据随机均匀地
分不到不同的 reducer 上。例如:
案例实操:
不随机分布空 null 值:
(1)设置 5 个 reduce 个数
set mapreduce.job.reduces = 5;
(2)JOIN 两张表
insert overwrite table jointable
select n.* from nullidtable n left join ori b on n.id = b.id;
结果:如图 6-13 所示,可以看出来,出现了数据倾斜,某些 reducer 的资源消耗远大于其他 reducer。
 
技术分享图片
图 6-13 空 key 转换
随机分布空 null 值
(1)设置 5 个 reduce 个数
set mapreduce.job.reduces = 5;
(2)JOIN 两张表
insert overwrite table jointable
select n.* from nullidtable n full join ori o on 
case when n.id is null then concat(hive, rand()) else n.id end = o.id;
结果:如图 6-14 所示,可以看出来,消除了数据倾斜,负载均衡 reducer 的资源消耗
技术分享图片
3.3 MapJoin(小表 join 大表)
 
如果不指定 MapJoin 或者不符合 MapJoin 的条件,那么 Hive 解析器会将 Join 操作转换成 Common Join,即:在 Reduce 阶段完成 join。容易发生数据倾斜。可以用 MapJoin 把小
表全部加载到内存在 map 端进行 join,避免 reducer 处理。
1.开启 MapJoin 参数设置
(1)设置自动选择 Mapjoin
set hive.auto.convert.join = true; 默认为 true
(2)大表小表的阈值设置(默认 25M 一下认为是小表):
set hive.mapjoin.smalltable.filesize=25000000;
2.MapJoin 工作机制,如图 6-15 所示
 
技术分享图片
图 6-15 MapJoin 工作机制
 
案例实操:
(1)开启 Mapjoin 功能
set hive.auto.convert.join = true; 默认为 true
(2)执行小表 JOIN 大表语句
 
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from smalltable s
join bigtable b
on s.id = b.id;
Time taken: 24.594 seconds
(3)执行大表 JOIN 小表语句
insert overwrite table jointable
select b.id, b.time, b.uid, b.keyword, b.url_rank, b.click_num, b.click_url
from bigtable b
join smalltable s
on s.id = b.id;
Time taken: 24.315 seconds
3.4 Group By
 
默认情况下,Map 阶段同一 Key 数据分发给一个 reduce,当一个 key 数据过大时就倾斜了。
技术分享图片

 

 

并不是所有的聚合操作都需要在 Reduce 端完成,很多聚合操作都可以先在 Map 端进行部分聚合,最后在 Reduce 端得出最终结果。
1.开启 Map 端聚合参数设置
(1)是否在 Map 端进行聚合,默认为 True
set hive.map.aggr = true
(2)在 Map 端进行聚合操作的条目数目
set hive.groupby.mapaggr.checkinterval = 100000
(3)有数据倾斜的时候进行负载均衡(默认是 false)
set hive.groupby.skewindata = true
当选项设定为 true,生成的查询计划会有两个 MR Job。 第一个 MR Job 中,Map 的输出结果会随机分布到 Reduce 中,每个 Reduce 做部分聚合操作,并输出结果,这样处理的结
是相同的 Group By Key 有可能被分发到不同的 Reduce 中,从而达到负载均衡的目的;第二个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中(这个过程可以保证相同的 Group By Key 被分布到同一个 Reduce 中),最后完成最终的聚合操作。
 
hive (default)> select deptno from emp group by deptno;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 23.68 sec HDFS Read: 19987 
HDFS Write: 9 SUCCESS
Total MapReduce CPU Time Spent: 23 seconds 680 msec
OK
deptno
10
20
30
优化以后
hive (default)> set hive.groupby.skewindata = true;
hive (default)> select deptno from emp group by deptno;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 28.53 sec HDFS Read: 18209 
HDFS Write: 534 SUCCESS
Stage-Stage-2: Map: 1 Reduce: 5 Cumulative CPU: 38.32 sec HDFS Read: 15014 
HDFS Write: 9 SUCCESS
Total MapReduce CPU Time Spent: 1 minutes 6 seconds 850 msec
OK
deptno
10
20
30
3.5 Count(Distinct) 去重统计
数据量小的时候无所谓,数据量大的情况下,由于 COUNT DISTINCT 的全聚合操作,即使设定了 reduce task 个数,set mapred.reduce.tasks=100;hive 也只会启动一个 reducer。,
这就造成一个 Reduce 处理的数据量太大,导致整个 Job 很难完成,一般 COUNT DISTINCT使用先 GROUP BY 再 COUNT 的方式替换:
案例实操
1.创建一张大表
hive (default)> create table bigtable(id bigint, time bigint, uid string, keyword
string, url_rank int, click_num int, click_url string) row format delimited
fields terminated by \t;
2.加载数据
hive (default)> load data local inpath /opt/module/datas/bigtable into table
bigtable;
3.设置 5 个 reduce 个数
 
set mapreduce.job.reduces = 5;
4.执行去重 id 查询
hive (default)> select count(distinct id) from bigtable;
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 7.12 sec HDFS Read: 120741990 
HDFS Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 7 seconds 120 msec
OK
c0
100001
Time taken: 23.607 seconds, Fetched: 1 row(s)
5.采用 GROUP by 去重 id
hive (default)> select count(id) from (select id from bigtable group by id) a;
Stage-Stage-1: Map: 1 Reduce: 5 Cumulative CPU: 17.53 sec HDFS Read: 120752703 
HDFS Write: 580 SUCCESS
Stage-Stage-2: Map: 1 Reduce: 1 Cumulative CPU: 4.29 sec HDFS Read: 9409 HDFS 
Write: 7 SUCCESS
Total MapReduce CPU Time Spent: 21 seconds 820 msec
OK
_c0
100001
Time taken: 50.795 seconds, Fetched: 1 row(s)
虽然会多用一个 Job 来完成,但在数据量大的情况下,这个绝对是值得的。
 
3.6 笛卡尔积
尽量避免笛卡尔积,join 的时候不加 on 条件,或者无效的 on 条件,Hive 只能使用 1 个reducer 来完成笛卡尔积。
 
3.7 行列过滤
列处理:在 SELECT 中,只拿需要的列,如果有,尽量使用分区过滤,少用 SELECT *。行处理:在分区剪裁中,当使用外关联时,如果将副表的过滤条件写在 Where 后面,那
么就会先全表关联,之后再过滤,比如:
案例实操:
1.测试先关联两张表,再用 where 条件过滤
hive (default)> select o.id from bigtable b
join ori o on o.id = b.id
where o.id <= 10;
Time taken: 34.406 seconds, Fetched: 100 row(s)
2.通过子查询后,再关联表
hive (default)> select b.id from bigtable b
join (select id from ori where id <= 10 ) o on b.id = o.id;
Time taken: 30.058 seconds, Fetched: 100 row(s)
 
3.8 动态分区调整
关系型数据库中,对分区表 Insert 数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive 中也提供了类似的机制,即动态分区(Dynamic Partition),只不
过,使用 Hive 的动态分区,需要进行相应的配置。
 
1.开启动态分区参数设置
(1)开启动态分区功能(默认 true,开启)
hive.exec.dynamic.partition=true
(2)设置为非严格模式(动态分区的模式,默认 strict,表示必须指定至少一个分区为静态分区,nonstrict 模式表示允许所有的分区字段都可以使用动态分区。)
hive.exec.dynamic.partition.mode=nonstrict
(3)在所有执行 MR 的节点上,最大一共可以创建多少个动态分区。默认 1000
 
hive.exec.max.dynamic.partitions=1000
(4)在每个执行 MR 的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如:源数据中包含了一年的数据,即 day 字段有 365 个值,那么该参数就
需要设置成大于 365,如果使用默认值 100,则会报错。
 
hive.exec.max.dynamic.partitions.pernode=100
(5)整个 MR Job 中,最大可以创建多少个 HDFS 文件。默认 100000
 
hive.exec.max.created.files=100000
(6)当有空分区生成时,是否抛出异常。一般不需要设置。默认 false
hive.error.on.empty.partition=false
2.案例实操
需求:将 dept 表中的数据按照地区(loc 字段),插入到目标表 dept_partition 的相应分区中。
(1)创建目标分区表
hive (default)> create table dept_partition(id int, name string) partitioned
by (location int) row format delimited fields terminated by \t;
(2)设置动态分区
set hive.exec.dynamic.partition.mode = nonstrict;
hive (default)> insert into table dept_partition partition(location) select deptno, dname, loc from dept;
(3)查看目标分区表的分区情况
 
hive (default)> show partitions dept_partition;
思考:目标分区表是如何匹配到分区字段的?
3.9 分桶
详见 6.6 章。
3.10 分区
详见 4.6 章
 
 

Hadoop基础(五十一):HIVE企业级调优(一)

原文:https://www.cnblogs.com/qiu-hua/p/13373782.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!