Optimization of the Hive table

A small table, a large table Join 

The key is relatively dispersed, and the small amount of data in the table on the left of the join, which can effectively reduce the chance of memory overflow error occurs; further, can join to small dimension Map Table (record number of 1000 or less) advanced RAM. Reduce the end to complete the map. 

The new version of hive has a large table on the small table JOIN JOIN small table and a large table is optimized. Small table on the left and right have no significant difference. 

Example:

1. Create a large table

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';

Importing large tables of data

load data local inpath '/opt/temp/hive/bigtable' into table bigtable;

2, create a small table

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';

Importing a small table data

load data local inpath '/opt/temp/hive/smalltable' into table smalltable;

3. Create a table after the 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';

4, close mapjoin function (on by default) 

set hive.auto.convert.join=false;

5, small table join large table

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 consuming:

Total MapReduce CPU Time Spent: 40 seconds 990 msec

6, the implementation of a large table JOIN small table statement 

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 consuming:

Total MapReduce CPU Time Spent: 43 seconds 940 msec

Second, the large table Join the large table 

1, air filtration KEY 

Sometimes join timeout because too much of certain key data corresponding to the same key and the corresponding data will be sent to the same reducer, resulting in memory is not enough. At this point we should carefully analyze these unusual key, in many cases, key data corresponding to these data are abnormal, we need to be filtered in SQL statements. E.g. key corresponding to the field is empty.

Example:

(1) create the original table

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';

Import Data:

load data local inpath '/opt/temp/hive/SogouQ1.txt' into table ori;

(2) Create an empty table 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';

Import Data:

load data local inpath '/opt/temp/hive/nullid' into table nullidtable;

(3) create a join table after

create table nullidjointable(
id bigint,
time bigint,
uid string,
keyword string,
url_rank int,
click_num int,
click_url string
) row format delimited fields terminated by '\t';

(4) Do not filter join two tables for empty id

insert overwrite table nullidjointable
select n.*
from nullidtable n
left join ori o
on n.id = o.id;

time consuming:

Total MapReduce CPU Time Spent: 49 seconds 200 msec

(5) Air Filter key

insert overwrite table nullidjointable select n.* from (select * from nullidtable where id is not null ) n left join ori o on n.id = o.id;

time consuming:

Total MapReduce CPU Time Spent: 22 seconds 160 msec

2, empty key conversion 

Although sometimes a key blank corresponding to a lot of data, the corresponding data is not abnormal data, must be included in the result of the join, in this case we can list a key blank fields assigned a random value, such that the data randomization not evenly divided on a different reducer.

(1) is not randomly distributed null null value

set mapreduce.job.reduces = 5; 

insert overwrite table nullidjointable
select n.*
from nullidtable n
left join ori o
on n.id = o.id;

The emergence of data skew, some reducer resource consumption is much larger than other reducer

(2) a random distribution value null null

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;

Can be seen, eliminates data skew, load balancing reducer of resource consumption 

Three, 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 工作机制 

 

四、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 中),最后完成最终的聚合操作。 

五、 Count(Distinct) 去重统计 

数据量小的时候无所谓,数据量大的情况下,由于 COUNT DISTINCT 操作需要用一个Reduce Task 来完成,这一个 Reduce 需要处理的数据量太大,就会导致整个 Job 很难完成,一般 COUNT DISTINCT 使用先 GROUP BY 再 COUNT 的方式替换。

六、笛卡尔积 

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

七、行列过滤 

1、列处理

在 SELECT 中,只拿需要的列,如果有,尽量使用分区过滤,少用 SELECT *。
2、行处理

在分区剪裁中,当使用外关联时,过滤条件最好用在子查询里

(1)先关联两张表,再用 where 条件过滤 (不推荐)

 select o.id from bigtable b join ori o on o.id = b.id where o.id <= 10; 

(2)优化后,通过子查询后,再关联表 (推荐)

 select b.id from bigtable b join (select id from ori where id <= 10 ) o on b.id = o.id; 

 

 

 

 

 

 

 

 

 

 

 

发布了69 篇原创文章 · 获赞 2 · 访问量 4117

Guess you like

Origin blog.csdn.net/zuodaoyong/article/details/104235949
Recommended