zoukankan      html  css  js  c++  java
  • Hive优化

    一、修改引擎

    默认处理引擎是MapReduce

    在这里插入图片描述

    可以修改处理引擎位Spark

    在这里插入图片描述

    修改为Spark引擎后速度提升10多倍

    二、参数设置

    2.1、启动时提示下面需要设置

    In order to change the average load for a reducer (in bytes):
      set hive.exec.reducers.bytes.per.reducer=<number>
    In order to limit the maximum number of reducers:
      set hive.exec.reducers.max=<number>
    In order to set a constant number of reducers:
      set mapreduce.job.reduces=<number>
    

    reduce数量由以下三个参数决定,
    mapred.reduce.tasks(强制指定reduce的任务数量)

    hive.exec.reducers.bytes.per.reducer(每个reduce任务处理的数据量,默认为1000^3=1G)

    hive.exec.reducers.max(每个任务最大的reduce数,默认为999)

    计算reducer数的公式很简单N=min( hive.exec.reducers.max ,总输入数据量/ hive.exec.reducers.bytes.per.reducer )

    只有一个reduce的场景:
    a、没有group by 的汇总
    b、order by
    c、笛卡尔积

    三、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;
    

    四、本地模式

    大多数的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;
    

    案例实战:

    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)
    
    

    五、表的优化

    5.1、小表join大表

      将key相对分散,并且数据量小的表放在join的左边,这样可以有效减少内存溢出错误发生的几率;再进一步,可以使用mapjoin让小的维度表(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 '	';
    // 创建小表
    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 '	';
    // 创建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 '	';
    
    3.分别向大表和小表中导入数据
    hive (default)> load data local inpath '/uardata1/hivetest/bigtable' into table bigtable;
    hive (default)>load data local inpath '/uardata1/hivetest/smalltable' into table smalltable;
    
    4.关闭mapjoin功能(默认是打开的)
    # 由于mapjoin将小表缓存到内存中, 所以此处为了测试大表,小表join, 将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: 11.304 seconds
    Time taken: 17.587 seconds
    Time taken: 10.252 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: 14.343 seconds
    Time taken: 11.867 seconds
    Time taken: 13.149 seconds
    

    5.2、大表join大表

    5.2.1、空Key

      有时join超时是因为某些key对应的数据太多,而相同key对应的数据都会发送到相同的reducer上,从而导致内存不够。此时我们应该仔细分析这些异常的key,很多情况下,这些key对应的数据是异常数据,我们需要在SQL语句中进行过滤。例如key对应的字段空,操作如下:

    (1)创建原始数据表、空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 '	';
    // 创建空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 '	';
    // 创建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 '	';
    
    (2)分别加载原始数据和空id数据到对应表中
    hive (default)> load data local inpath '/uardata1/hivetest/ori' into table ori;
    hive (default)> load data local inpath '/uardata1/hivetest/nullid' into table nullidtable;
    
    (3)测试不过滤空id
    hive (default)> insert overwrite table jointable 
    select n.* from nullidtable n left join ori o on n.id = o.id;
    
    Time taken: 23.255 seconds
    Time taken: 20.177 seconds
    
    
    (4)测试过滤空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: 7.946 seconds
    Time taken: 7.882 seconds
    
    

    5.2.2、空key转换

      有时虽然某个key为空对应的数据很多,但是相应的数据不是异常数据,必须要包含在join的结果中,此时我们可以表a中key为空的字段赋一个随机的值,使得数据随机均匀地分不到不同的reducer上。例如:

    5.2.2.1、不随机分布空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;
    
    
    Time taken: 23.528 seconds
    Time taken: 21.05 seconds
    

    5.2.2.2、随机分布空null值

    (1)设置5个reduce个数
    set mapreduce.job.reduces = 5;
    (2)JOIN两张表
    # 此处将空key转换为随机数据,与原有数据的key保证不会冲突的同时,对空key散列分布
    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;
    

    5.3、MapJoin

      如果不指定MapJoin或者不符合MapJoin的条件,那么Hive解析器会将Join操作转换成Common Join,即:在Reduce阶段完成join。容易发生数据倾斜。可以用MapJoin把小表全部加载到内存在map端进行join,避免reducer处理。

    5.3.1、开启MapJoin

    (1)设置自动选择Mapjoin
    set hive.auto.convert.join = true; 默认为true
    (2)大表小表的阈值设置(默认25M一下认为是小表):
    set hive.mapjoin.smalltable.filesize=25000000;
    
    

    5.3.2、MapJoin工作机制

    在这里插入图片描述

    5.3.3、实例

    (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: 4.817 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: 5.915 seconds
    

    与5.1、5.2、相比,时间提高一倍。大小表的前后位置没有什么影响

    5.4、 Group By

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

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

    5.5、Count(Distinct) 去重统计

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

    5.5.1、案例一

    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 '	';
    
    2.加载数据
    hive (default)> load data local inpath '/uardata1/hivetest/bigtable' into table
     bigtable;
     
    3.设置5个reduce个数
    set mapreduce.job.reduces = 5;
    
    4.执行去重id查询
    hive (default)> select count(distinct id) from bigtable;
    OK
    99947
    Time taken: 3.284 seconds, Fetched: 1 row(s)
    
    5.采用GROUP by去重id
    hive (default)> select count(id) from (select id from bigtable group by id) a;
    OK
    99947
    Time taken: 3.28 seconds, Fetched: 1 row(s)
    

    虽然会多用一个Job来完成,但在数据量大的情况下,这个绝对是值得的。

    5.6、笛卡尔积

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

    5.7 行列过滤

    • 列处理:在SELECT中,只拿需要的列,如果有,尽量使用分区过滤,少用SELECT *。
    • 行处理:在分区剪裁中,当使用外关联时,如果将副表的过滤条件写在Where后面,那么就会先全表关联,之后再过滤,比如:

    5.7.1、案例一

    1.测试先关联两张表,再用where条件过滤
    hive (default)> select o.id from bigtable b
    join ori o on o.id = b.id
    where o.id <= 10;
    Time taken: 3.282 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: 4.232 seconds, Fetched: 100 row(s)
    
    

    5.8、动态分区调整

      关系型数据库中,对分区表Insert数据时候,数据库自动会根据分区字段的值,将数据插入到相应的分区中,Hive中也提供了类似的机制,即动态分区(Dynamic Partition),只不过,使用Hive的动态分区,需要进行相应的配置

    5.8.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
    

    5.8.2、案例一

    需求:将ori中的数据按照时间(如:20111230000008),插入到目标表ori_partitioned_target的相应分区中。

    (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 '	';
    (2)加载数据到分区表中
    hive (default)> load data local inpath '/uardata1/hivetest/ds1' into table
     ori_partitioned partition(p_time='20200730000010') ;
    hive (default)> load data local inpath '/uardata1/hivetest/ds2' into table ori_partitioned partition(p_time='20200730000011') ;
    (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 '	';
    (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;
    
    

    关注我的公众号【宝哥大数据】, 更多干货

    在这里插入图片描述

  • 相关阅读:
    Python列表和元组知识点
    Python 字符串操作常用知识点
    ng-alain的sf如何自定义部件
    Spring-手写Spring注解版本事务,Spring事物的七大传播行为
    Spring-SpringAOP原理,手写Spring事务框架
    JVM性能优化--类加载器,手动实现类的热加载
    JVM性能优化--字节码技术
    JVM性能优化--JVM参数配置,使用JMeter简单测试配合说明参数调优
    JVM性能优化--Java的垃圾回收机制
    设计模式之原型模式、策略模式、观察者模式
  • 原文地址:https://www.cnblogs.com/chengbao/p/15085987.html
Copyright © 2011-2022 走看看