码迷,mamicode.com
首页 > 其他好文 > 详细

hive优化----控制hive中的map数

时间:2015-05-06 17:05:48      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

1. Hive自己如何确定reduce数:
reduce个数的设定极大影响任务执行效率,不指定reduce个数的情况下,Hive会猜测确定一个reduce个数,基于以下两个设定:
hive.exec.reducers.bytes.per.reducer(每个reduce任务处理的数据量,默认为1000^3=1G) 
hive.exec.reducers.max(每个任务最大的reduce数,默认为999)
计算reducer数的公式很简单N=min(参数2,总输入数据量/参数1)
即,如果reduce的输入(map的输出)总大小不超过1G,那么只会有一个reduce任务;
如:select pt,count(1) from popt_tbaccountcopy_mes wherept = ‘2012-07-04‘ group by pt; 
/group/p_sdo_data/p_sdo_data_etl/pt/popt_tbaccountcopy_mes/pt=2012-07-04 总大小为9G多,因此这句有10个reduce

2. 调整reduce个数方法一:
调整hive.exec.reducers.bytes.per.reducer参数的值;
set hive.exec.reducers.bytes.per.reducer=500000000; (500M)
select pt,count(1) from popt_tbaccountcopy_mes where pt = ‘2012-07-04‘ group bypt; 这次有20个reduce

3. 调整reduce个数方法二;
set mapred.reduce.tasks = 15;
select pt,count(1) from popt_tbaccountcopy_mes where pt = ‘2012-07-04‘ group bypt;这次有15个reduce

4. reduce个数并不是越多越好;
同map一样,启动和初始化reduce也会消耗时间和资源;
另外,有多少个reduce,就会有多少个输出文件,如果生成了很多个小文件,那么如果这些小文件作为下一个任务的输入,则也会出现小文件过多的问题;

5. 什么情况下只有一个reduce;
很多时候你会发现任务中不管数据量多大,不管你有没有设置调整reduce个数的参数,任务中一直都只有一个reduce任务;
其实只有一个reduce任务的情况,除了数据量小于hive.exec.reducers.bytes.per.reducer参数值的情况外,还有以下原因:
a) 没有group by的汇总,比如把selectpt,count(1) from popt_tbaccountcopy_mes where pt = ‘2012-07-04‘ group by pt; 写成 select count(1) from popt_tbaccountcopy_mes where pt =‘2012-07-04‘;
这点非常常见,希望大家尽量改写。
b) 用了Order by
c) 有笛卡尔积
通常这些情况下,除了找办法来变通和避免,我暂时没有什么好的办法,因为这些操作都是全局的,所以hadoop不得不用一个reduce去完成;

同样的,在设置reduce个数的时候也需要考虑这两个原则:使大数据量利用合适的reduce数;使单个reduce任务处理合适的数据量;
Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具。
使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别,
所以需要去掉原有关系型数据库下开发的一些固有思维。
基本原则:
1:尽量尽早地过滤数据,减少每个阶段的数据量,对于分区表要加分区,同时只选择需要使用到的字段
select ... fromA
join B
on A.key =B.key
whereA.userid>10
andB.userid<10
andA.dt=‘20120417‘
andB.dt=‘20120417‘;
应该改写为:
select ....from (select .... from A
wheredt=‘201200417‘
anduserid>10
) a
join ( select.... from B
wheredt=‘201200417‘
and userid <10
) b
on a.key =b.key;
2:尽量原子化操作,尽量避免一个SQL包含复杂逻辑
可以使用中间表来完成复杂的逻辑
drop table ifexists tmp_table_1;
create table ifnot exists tmp_table_1 as
select ......;
drop table ifexists tmp_table_2;
create table ifnot exists tmp_table_2 as
select ......;
drop table ifexists result_table;
create table ifnot exists result_table as
select ......;
drop table ifexists tmp_table_1;
drop table ifexists tmp_table_2;
3:单个SQL所起的JOB个数尽量控制在5个以下
4:慎重使用mapjoin,一般行数小于2000行,大小小于1M(扩容后可以适当放大)的表才能使用,小表要注意放在join的左边(目前TCL里面很多都小表放在join的右边)。
否则会引起磁盘和内存的大量消耗
5:写SQL要先了解数据本身的特点,如果有join ,group操作的话,要注意是否会有数据倾斜
如果出现数据倾斜,应当做如下处理:
sethive.exec.reducers.max=200;
setmapred.reduce.tasks= 200;---增大Reduce个数
sethive.groupby.mapaggr.checkinterval=100000 ;--这个是group的键对应的记录条数超过这个值则会进行分拆,值根据具体数据量设置
set hive.groupby.skewindata=true;--如果是group by过程出现倾斜 应该设置为true
sethive.skewjoin.key=100000; --这个是join的键对应的记录条数超过这个值则会进行分拆,值根据具体数据量设置
sethive.optimize.skewjoin=true;--如果是join 过程出现倾斜 应该设置为true
6:如果union all的部分个数大于2,或者每个union部分数据量大,应该拆成多个insert into 语句,实际测试过程中,执行时间能提升50%
insert overwitetable tablename partition (dt= ....)
select .....from (
select ... fromA
union all
select ... fromB
union all
select ... fromC
) R
where ...;
可以改写为:
insert intotable tablename partition (dt= ....)
select ....from A
WHERE ...;
insert intotable tablename partition (dt= ....)
select ....from B
WHERE ...;
insert intotable tablename partition (dt= ....)
select ....from C
WHERE ...;

 

 更多分享请关注:http://bbs.superwu.cn 
关注超人学院微信二维码:技术分享

hive优化----控制hive中的map数

标签:

原文地址:http://www.cnblogs.com/CRXY/p/4482215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!