【亲测有效】hive最全常用配置参数,加速,优化你的hive任务,all you need,持续更新中

hive原理不多说了。

hive版本:hive-common-1.1.0-cdh5.16.2.jar


Hive设置配置参数的方法

  • Hive提供三种可以改变环境变量的方法,分别是:
    (1)、修改${HIVE_HOME}/conf/hive-site.xml配置文件;
    (2)、命令行参数;
    (3)、在已经进入cli时进行参数声明。

方法一:hive-site.xml配置参数

  • 在Hive中,所有的默认配置都在 "{HIVE_HOME}/conf/hive-default.xml "文件中,如果需要对默认的配置进行修改,可以创建一个 "hive-site.xml" 文件,放在 " {HIVE_HOME}/conf"目录下。里面可以对一些配置进行个性化设定。在hive-site.xml的格式如下:
<configuration>
    <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
        <description>location of
              default database for the warehouse</description>
    </property>
</configuration>
  • 所有的配置都是放在<configuration></configuration>标签之间,一个configuration标签里面可以存在多个<property></property>标签。<name>标签里面就是我们想要设定属性的名称;<value>标签里面是我们想要设定的值;<description;<标签是描述在这个属性的,可以不写。绝大多少配置都是在xml文件里面配置的,因为在这里做的配置都全局用户都生效,而且是永久的。用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hive的配置会覆盖Hadoop的配置。

方法二:命令行参数

  • 在启动Hive cli的时候进行配置,可以在命令行添加-hiveconf param=value来设定参数,例如:
hive --hiveconf mapreduce.job.queuename=queue1
  • 这样在Hive中所有MapReduce作业都提交到队列queue1中。这一设定对本次启动的会话有效,下次启动需要重新配置。

方法三:进入cli时候声明

  • 在已经进入cli时进行参数声明,可以在HQL中使用SET关键字设定参数,例如:
set mapreduce.job.queuename=queue1;

上述三种设定方式的优先级依次递增。即参数声明覆盖命令行参数,命令行参数覆盖配置文件设定。


不想改变hive配置,临时每次启动hive交互客户端或执行hive sql脚本时修改参数:

/usr/bin/hive --hiveconf mapreduce.job.queuename=xxxqueue \
--hiveconf hive.exec.parallel=true \
--hiveconf hive.exec.parallel.thread.number=16 \
--hiveconf mapreduce.job.jvm.numtasks=10 \
--hiveconf mapreduce.map.speculative=true \
--hiveconf mapreduce.reduce.speculative=true \
--hiveconf hive.mapred.reduce.tasks.speculative.execution=true \
--hiveconf hive.exec.compress.intermediate=true \
--hiveconf apreduce.map.output.compress=true \
--hiveconf hive.exec.mode.local.auto=true \
--hiveconf hive.fetch.task.conversion=more ......


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>

hive.fetch.task.conversion=more

开启Hive的本地模式

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

hive.exec.mode.local.auto=true

开启Hive的并行执行

  • Hive会将一个查询转化成一个或者多个阶段。这样的阶段可以是MapReduce阶段、抽样阶段、合并阶段、limit阶段。或者Hive执行过程中可能需要的其他阶段。默认情况下,Hive一次只会执行一个阶段。不过,某个特定的job可能包含众多的阶段,而这些阶段可能并非完全互相依赖的,也就是说有些阶段是可以并行执行的,这样可能使得整个job的执行时间缩短。不过,如果有更多的阶段可以并行执行,那么job可能就越快完成。
  • 通过设置参数hive.exec.parallel值为true,就可以开启并发执行。不过,在共享集群中,需要注意下,如果job中并行阶段增多,那么集群利用率就会增加。
  • 设置参数:
  • set hive.exec.parallel=true; //打开任务并行执行
  • set hive.exec.parallel.thread.number=16; //同一个sql允许最大并行度,默认为8。

JVM重用

  • JVM重用是Hadoop调优参数的内容,其对Hive的性能具有非常大的影响,特别是对于很难避免小文件的场景或task特别多的场景,这类场景大多数执行时间都很短。
  • Hadoop的默认配置通常是使用派生JVM来执行map和Reduce任务的。这时JVM的启动过程可能会造成相当大的开销,尤其是执行的job包含有成百上千task任务的情况。JVM重用可以使得JVM实例在同一个job中重新使用N次。N的值可以在Hadoop的mapred-site.xml文件中进行配置。通常在10-20之间,具体多少需要根据具体业务场景测试得出。

<property>
  <name>mapreduce.job.jvm.numtasks</name>
  <value>10</value>
  <description>How many tasks to run per jvm. If set to -1, there is
  no limit. 
  </description>
</property>

mapreduce.job.jvm.numtasks=10

开启Hive的推测执行

  • 在分布式集群环境下,因为程序Bug(包括Hadoop本身的bug),负载不均衡或者资源分布不均等原因,会造成同一个作业的多个任务之间运行速度不一致,有些任务的运行速度可能明显慢于其他任务(比如一个作业的某个任务进度只有50%,而其他所有任务已经运行完毕),则这些任务会拖慢作业的整体执行进度。为了避免这种情况发生,Hadoop采用了推测执行(Speculative Execution)机制,它根据一定的法则推测出“拖后腿”的任务,并为这样的任务启动一个备份任务,让该任务与原始任务同时处理同一份数据,并最终选用最先成功运行完成任务的计算结果作为最终结果。
  • 设置开启推测执行参数:Hadoop的mapred-site.xml文件中进行配置

<property>
  <name>mapreduce.map.speculative</name>
  <value>true</value>
  <description>If true, then multiple instances of some map tasks 
               may be executed in parallel.</description>
</property>

<property>
  <name>mapreduce.reduce.speculative</name>
  <value>true</value>
  <description>If true, then multiple instances of some reduce tasks 
               may be executed in parallel.</description>
</property>

mapreduce.map.speculative=ture

mapreduce.reduce.speculative=true

  • 不过hive本身也提供了配置项来控制reduce-side的推测执行:
<property>
    <name>hive.mapred.reduce.tasks.speculative.execution</name>
    <value>true</value>
    <description>Whether speculative execution for reducers should be turned on. </description>
  </property>

hive.mapred.reduce.tasks.speculative.execution=true

压缩 开启Map Reduce输出阶段压缩

开启Map输出阶段压缩

  • 开启map输出阶段压缩可以减少job中map和Reduce task间数据传输量。具体配置如下:
1)开启hive中间传输数据压缩功能
hive (default)>set hive.exec.compress.intermediate=true;
2)开启mapreduce中map输出压缩功能
hive (default)>set mapreduce.map.output.compress=true;
3)设置mapreduce中map输出数据的压缩方式
hive (default)>set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;
4)执行查询语句
    hive (default)> select count(ename) name from emp;

hive.exec.compress.intermediate=true

mapreduce.map.output.compress=true

mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec

开启Reduce输出阶段压缩

  • 当Hive将输出写入到表中时,输出内容同样可以进行压缩。属性hive.exec.compress.output控制着这个功能。用户可能需要保持默认设置文件中的默认值false,这样默认的输出就是非压缩的纯文本文件了。用户可以通过在查询语句或执行脚本中设置这个值为true,来开启输出结果压缩功能。
1)开启hive最终输出数据压缩功能
hive (default)>set hive.exec.compress.output=true;
2)开启mapreduce最终输出数据压缩
hive (default)>set mapreduce.output.fileoutputformat.compress=true;
3)设置mapreduce最终数据输出压缩方式
hive (default)> set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;
4)设置mapreduce最终数据输出压缩为块压缩
hive (default)> set mapreduce.output.fileoutputformat.compress.type=BLOCK;
5)测试一下输出结果是否是压缩文件
hive (default)> insert overwrite local directory '/opt/module/datas/distribute-result' select * from emp distribute by deptno sort by empno desc;

hive.exec.compress.output=true;
mapreduce.output.fileoutputformat.compress=true;
mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;
mapreduce.output.fileoutputformat.compress.type=BLOCK;
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;

hive.auto.convert.join=true

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

hive.map.aggr=true

hive.groupby.skewindata=true

是否开启合并 Map/Reduce 小文件

对于 Hadoop 0.20 以前的版本,起一首新的 Map/Reduce Job,对于 0.20 以后的版本,则是起使用 CombineInputFormat 的 MapOnly Job。 默认是:false;

hive.merge.mapredfiles=true

例子:

在命令行下:

/usr/bin/hive --hiveconf mapreduce.job.queuename=xxxqueue \
--hiveconf hive.exec.parallel=true \
--hiveconf hive.exec.parallel.thread.number=16 \
--hiveconf mapreduce.job.jvm.numtasks=10 \
--hiveconf mapreduce.map.speculative=true \
--hiveconf mapreduce.reduce.speculative=true \
--hiveconf hive.mapred.reduce.tasks.speculative.execution=true \
--hiveconf hive.exec.compress.intermediate=true \
--hiveconf mapreduce.map.output.compress=true \
--hiveconf mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec \
--hiveconf hive.exec.compress.output=true \
--hiveconf mapreduce.output.fileoutputformat.compress=true \
--hiveconf mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec \
--hiveconf mapreduce.output.fileoutputformat.compress.type=BLOCK \
--hiveconf hive.auto.convert.join=true \
--hiveconf hive.map.aggr=true \
--hiveconf hive.groupby.skewindata=true \
--hiveconf hive.exec.mode.local.auto=true \
--hiveconf hive.fetch.task.conversion=more \
--hiveconf hive.merge.mapredfiles=true

在hue界面上:

set mapreduce.job.queuename=xxxqueue ;
set hive.exec.parallel=true;
set hive.exec.parallel.thread.number=16;
set mapreduce.job.jvm.numtasks=10;
set mapreduce.map.speculative=true;
set mapreduce.reduce.speculative=true;
set hive.mapred.reduce.tasks.speculative.execution=true;
set hive.exec.compress.intermediate=true;
set mapreduce.map.output.compress=true;
set mapreduce.map.output.compress.codec= org.apache.hadoop.io.compress.SnappyCodec;
set hive.exec.compress.output=true;
set mapreduce.output.fileoutputformat.compress=true;
set mapreduce.output.fileoutputformat.compress.codec = org.apache.hadoop.io.compress.SnappyCodec;
set mapreduce.output.fileoutputformat.compress.type=BLOCK;
set hive.auto.convert.join=true;
set hive.map.aggr=true;
set hive.groupby.skewindata=true;
set hive.exec.mode.local.auto=true;
set hive.fetch.task.conversion=more;
set hive.merge.mapredfiles=true;

hivesql 优化了参数后比优化前提升好几倍速度

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在实际电商业务,可以通过以下一些优化参数来提高 Hive on Spark 的性能和效率: 1. spark.executor.memory:用于设置每个 Executor 的内存大小,默认为 1g。可以根据实际的数据规模和计算任务来调整内存大小。 2. spark.executor.cores:用于设置每个 Executor 的 CPU 核数,默认为 1。可以根据实际的计算任务来调整 CPU 核数。 3. spark.sql.shuffle.partitions:用于设置 shuffle 操作的分区数,默认为 200。可以根据实际的数据规模和计算任务来调整分区数。 4. spark.sql.autoBroadcastJoinThreshold:用于设置自动广播小表的阈值,默认为 10m。可以根据实际的数据规模和计算任务来调整阈值。 5. hive.vectorized.execution.enabled:用于启用 Hive 的向量化执行引擎,可以大幅提高查询效率。默认为 false,可以设置为 true 来启用。 6. hive.cbo.enable:用于启用 Hive 的成本优化器,可以优化查询计划并提高查询效率。默认为 false,可以设置为 true 来启用。 7. hive.exec.parallel:用于设置并行执行任务的数量,默认为 1。可以根据实际的计算任务来调整并行度。 8. hive.exec.dynamic.partition.mode:用于设置动态分区模式,包括 strict、nonstrict 和 strictstrict。可以根据实际的数据规模和查询需求来选择合适的模式。 总之,通过合理设置这些优化参数,可以提高 Hive on Spark 的性能和效率,从而更好地支持电商业务的数据分析和决策。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zda天天爱打卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值