Hive企业级优化

一、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>

2、本地模式

大多数的Hadoop Job 是需要 Hadoop 提供的完整的可扩展性来处理大数据集的。不过, 有时 Hive 的输入数据量是非常小的。在这种情况下,为查询触发执行任务时消耗可能会比实际 job 的执行时间要多的多。对于大多数这种情况,Hive 可以通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间可以明显被缩短。
用户可以通过设置 hive.exec.mode.local.auto 的值为 true,来让 Hive 在适当的时候自动启动这个优化。

	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;

3、表的优化

将 key 相对分散,并且数据量小的表放在 join 的左边,这样可以有效减少内存溢出错误发生的几率;再进一步,可以使用 Group 让小的维度表(1000 条以下的记录条数)先进内存。在 map 端完成 reduce。
实际测试发现:新版的 hive 已经对小表JOIN 大表和大表JOIN 小表进行了优化。小表放在左边和右边已经没有明显区别。


3.1、MapJoin

如果不指定 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 工作机制
在这里插入图片描述
案例实操:

(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
可以看到时间差不多,所以在新版本的HIve 大表join小表,跟小表join大表,差不多了

3.2、Group by

默认情况下,Map 阶段同一 Key 数据分发给一个 reduce,当一个 key 数据过大时就倾斜了。
并不是所有的聚合操作都需要在 Reduce 端完成,很多聚合操作都可以先在 Map 端进行部分聚合,最后在 Reduce 端得出最终结果。

1)	开启 Map 端聚合参数设置
(1)	是否在Map 端进行聚合,默认为 True hive.map.aggr = true
(2)	在 Map 端进行聚合操作的条目数目
hive.groupby.mapaggr.checkinterval = 100000

(3)	有数据倾斜的时候进行负载均衡(默认是 false) 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 中),最后完成最终的聚合操作。

3.3、Count(Distinct) 去重统计

数据量小的时候无所谓,数据量大的情况下,由于 COUNT DISTINCT 操作需要用一个Reduce Task 来完成,这一个 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 take**n: 23.607 seconds, Fetched 1 row(s) Time taken: 34.941 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: 3	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.4、笛卡尔积

尽量避免笛卡尔积,join 的时候不加 on 条件,或者无效的 on 条件,Hive 只能使用 1个 reducer 来完成笛卡尔积

3.5、动态分区调整

关系型数据库中,对分区表 Insert 数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive 中也提供了类似的机制,即动态分区(Dynamic Partition),只不过,使用Hive 的动态分区,需要进行相应的配置。
1)	开启动态分区参数设置
(1)	开启动态分区功能(默认 true,开启) hive.exec.dynamic.partition=true
(2)	设置为非严格模式(动态分区的模式,默认 strict,表示必须指定至少一个分区为
 
静态分区,nonstrict 模式表示允许所有的分区字段都可以使用动态分区。)

hive.exec.dynamic.partition.mode=nonstrict

(3)	在所有执行 MR 的节点上,最大一共可以创建多少个动态分区。

hive.exec.max.dynamic.partitions=1000

(4)	在每个执行 MR 的节点上,最大可以创建多少个动态分区。该参数需要根据实际的数据来设定。比如:源数据中包含了一年的数据,即 day 字段有 365 个值,那么该参数就需要设置成大于 365,如果使用默认值 100,则会报错。
hive.exec.max.dynamic.partitions.pernode=100

(5)	整个 MR Job 中,最大可以创建多少个HDFS 文件。

hive.exec.max.created.files=100000

(6)	当有空分区生成时,是否抛出异常。一般不需要设置。

hive.error.on.empty.partition=false 
案例实操
(1)	创建分区表
		create table ori_partitioned(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string)
		partitioned by (p_time bigint)

row format delimited fields terminated by '\t';
(2)	加载数据到分区表中
		hive (default)> load data local inpath '/opt/module/datas/ds1' into table ori_partitioned partition(p_time='20111230000010') ;
		hive (default)> load data local inpath '/opt/module/datas/ds2' into table ori_partitioned
		
		partition(p_time='20111230000011') ;
(3)	创建目标分区表
		create table ori_partitioned_target(id bigint, time bigint, uid string, keyword string, url_rank int, click_num int, click_url string) PARTITIONED BY (p_time STRING) row
		format delimited fields terminated by '\t';
(4)	设置动态分区
		set hive.exec.dynamic.partition = true;
		set hive.exec.dynamic.partition.mode = nonstrict; set hive.exec.max.dynamic.partitions = 1000;
		set hive.exec.max.dynamic.partitions.pernode = 100; set hive.exec.max.created.files = 100000;
		set hive.error.on.empty.partition = false;
		
		hive (default)> insert overwrite table ori_partitioned_target partition (p_time)
		select id, time, uid, keyword, url_rank, click_num, click_url, p_time from ori_partitioned;
(5)	查看目标分区表的分区情况

hive (default)> show partitions ori_partitioned_target;

3.6、分区跟分桶

4、数据倾斜问题

4.1、合理设置Map数量

1) 通常情况下,作业会通过 input 的目录产生一个或者多个 map 任务。
主要的决定因素有:input 的文件总个数,input 的文件大小,集群设置的文件块大小。
2) 是不是 map 数越多越好?
答案是否定的。如果一个任务有很多小文件(远远小于块大小 128m),则每个小文件也会被当做一个块,用一个 map 任务来完成,而一个 map 任务启动和初始化的时间远远大于逻辑处理的时间,就会造成很大的资源浪费。而且,同时可执行的 map 数是受限的。
3) 是不是保证每个 map 处理接近 128m 的文件块,就高枕无忧了?
答案也是不一定。比如有一个 127m 的文件,正常会用一个 map 去完成,但这个文件只有一个或者两个小字段,却有几千万的记录,如果 map 处理的逻辑比较复杂,用一个 map 任务去做,肯定也比较耗时。
针对上面的问题 2 和 3,我们需要采取两种方式来解决:即减少 map 数和增加 map 数;

4.2、小文件进行合并

在 map 执行前合并小文件,减少 map 数:CombineHiveInputFormat 具有对小文件进行合并的功能(系统默认的格式)。HiveInputFormat 没有对小文件合并功能。

set hive.input.format= org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;

4.3、复杂文件增加 Map 数

当 input 的文件都很大,任务逻辑复杂,map 执行非常慢的时候,可以考虑增加 Map
数,来使得每个 map 处理的数据量减少,从而提高任务的执行效率。增加 map 的方法为:根据
computeSliteSize(Math.max(minSize,Math.min(maxSize,blocksize)))=blocksize=128M 公式,调整 maxSize 最大值。让 maxSize 最大值低于 blocksize 就可以增加 map 的个数。
在这里插入图片描述

4.4、合理设置reduce的个数

1)	调整 reduce 个数方法一
(1)	每个 Reduce 处理的数据量默认是 256MB hive.exec.reducers.bytes.per.reducer=256000000
(2)	每个任务最大的 reduce 数,默认为 1009 hive.exec.reducers.max=1009
(3)	计算reducer 数的公式

N=min(参数 2,总输入数据量/参数 1)

2)	调整 reduce 个数方法二
在 hadoop 的 mapred-default.xml 文件中修改
 
设置每个 job 的 Reduce 个数

set mapreduce.job.reduces = 15;
3)	reduce 个数并不是越多越好
1)	过多的启动和初始化 reduce 也会消耗时间和资源;
2)	另外,有多少个reduce,就会有多少个输出文件,如果生成了很多个小文件,那么如果这些小文件作为下一个任务的输入,则也会出现小文件过多的问题;
在设置 reduce 个数的时候也需要考虑这两个原则:处理大数据量利用合适的 reduce 数; 使单个 reduce 任务处理数据量大小要合适;

还有一些其他零碎的东西

1、并行执行
2、JVM重用
3、推测执行
4、压缩
5、执行计划

猜你喜欢

转载自blog.csdn.net/qq_32736999/article/details/84771622