HiveSql调优系列之Hive严格模式,如何合理使用Hive严格模式

综述

在同样的集群运行环境中,hive调优有两种方式,即参数调优和sql调优

本篇讲涉及到的Hive严格模式。

前两天在优化一个前人遗留下的sql,发现关于严格模式参数是这样使用的,严重错误。

set hive.strict.checks.cartesian.product=flase;
set hive.mapred.mode=nonstrict;

而且我发现在使用参数上,无论sql大小直接贴一堆参数,类似这样。

set hive.exec.parallel=true;
set hive.exec.parallel.thread.number=16;
set hive.merge.mapfiles = true; 
set hive.merge.mapredfiles = true; 
set hive.merge.size.per.task=256000000;
set hive.merge.smallfiles.avgsize = 256000000;
set mapred.max.split.size=1024000000;
set mapred.min.split.size.per.node=1024000000;
set mapred.min.split.size.per.rack=1024000000; 
set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
set hive.join.emit.interval = 2000;
set hive.mapjoin.size.key = 20000;
set hive.mapjoin.cache.numrows = 20000;
set hive.exec.reducers.bytes.per.reducer=2000000000;
set hive.exec.reducers.max=999;
set hive.map.aggr=true;
set hive.groupby.mapaggr.checkinterval=100000;
set hive.auto.convert.join = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.exec.dynamic.partition = true;
set hive.cli.print.header=true;
set hive.resultset.use.unique.column.names=false;
set mapreduce.reduce.memory.mb=4096;
set mapreduce.reduce.java.opts=-Xmx4096m;
set mapred.max.split.size=1024000000;
set mapred.min.split.size.per.node=1024000000;
set mapred.min.split.size.per.rack=1024000000; 

优化是优化了,但是我看到了优化的无目标性,反而在一定程度上多消耗了计算资源。

于是打算开一个系列文章,Hive SQL调优系列,如何合理的使用参数进行SQL优化,针对什么情况使用哪些参数优化。

本篇先说说严格模式相关参数怎么使用。

正文如下。

1.严格模式

所谓Hive的严格模式,就是为了避免用户提交一些恶意SQL,消耗大量资源进而使得运行环境崩溃做出的一些安全性的限制。

或多或少我们都提交过一些执行很久,集群资源不足的SQL。应该能理解。

前文Hive动态分区详解中有提到过

1.1 参数设置

-- strict 为开启严格模式  nostrict 关闭严格模式
set hive.mapred.mode=strict

1.2 查看参数

通过hive的set 查看指定参数

-- 黑窗口查看Hive模式,以下结果为未开启严格模式
hive> set hive.mapred.mode;
hive.mapred.mode is undefined

1.3 严格模式限制内容及对应参数设置

如果Hive开启严格模式,将会阻止一下三种查询:

a.对分区表查询,where条件中过滤字段没有分区字段;

b.对order by查询,order by的查询不带limit语句。

c.笛卡尔积join查询,join查询语句中不带on条件或者where条件;

以上三种查询情况也有自己单独的参数可以进行控制。

  • 分区表查询必须指定分区
-- 开启限制(默认为 false)
set hive.strict.checks.no.partition.filter=true;
  • orderby排序必须指定limit
-- 开启限制(默认为false)
set hive.strict.checks.orderby.no.limit=true;
  • 限制笛卡尔积运算
-- 开启限制(默认为false)
set hive.strict.checks.cartesian.product=true;

2.实际操作

2.1 分区表查询时必须指定分区

分区表查询必须指定分区的原因:如果该表有大量分区,如果不加限制,在读取时会读取到超出预估的数据量。

-- 测试
create table `lubian` (
`id` string comment 'id',
`name` string comment '姓名'
)
comment 'lubian' 
PARTITIONED BY (ymd string)
row format delimited fields terminated by '\t' lines terminated by '\n' 
stored as orc;

set hive.strict.checks.no.partition.filter=true;
select * from lubian limit 111;

执行结果

FAILED: SemanticException [Error 10056]:
    Queries against partitioned tables without a partition filter are disabled for safety reasons.
    If you know what you are doing, please set hive.strict.checks.no.partition.
    filter to false and make sure that hive.mapred.mode is not set to 'strict' to proceed.
    Note that you may get errors or incorrect results if you make a mistake while using some of the unsafe features.
    No partition predicate for Alias "lubian" Table "lubian"

select * from partab where dt='11' limit 111;
Time taken: 0.77 seconds

2.2 order by必须指定limit

order by必须指定limit的主要原因: order by 为全局排序,所有数据只有一个reduceTask来处理,防止单个reduce运行时间过长,而导致任务阻塞

-- 测试
set hive.strict.checks.orderby.no.limit=true;
select * from lubian order by name;

执行结果

FAILED: SemanticException 1:36
    Order by-s without limit are disabled for safety reasons.
    If you know what you are doing, please set hive.strict.checks.orderby.no.limit to false
    and make sure that hive.mapred.mode is not set to 'strict' to proceed.
    Note that you may get errors or incorrect results if you make a mistake while using some of the unsafe features..
    Error encountered near token 'name'

2.3 限制笛卡尔积

限制笛卡尔积运算原因:笛卡尔积可能会造成数据急速膨胀,例如两个1000条数据表关联,会产生100W条数据。n的平方增长。触发笛卡尔积时,join操作会在一个reduceTask中执行

-- 测试
set hive.strict.checks.cartesian.product=true;
select t1.*,t2.* from lubian as t1
inner join lubian as t2;

执行结果

FAILED: SemanticException Cartesian products are disabled for safety reasons.
    If you know what you are doing, please set hive.strict.checks.cartesian.product to false
    and make sure that hive.mapred.mode is not set to 'strict' to proceed.
    Note that you may get errors or incorrect results
    if you make a mistake while using some of the unsafe features.

3.搭配使用

3.1 参数

设置hive严格模式参数如下

set hive.mapred.mode=strict;
set hive.strict.checks.no.partition.filter=true;
set hive.strict.checks.orderby.no.limit=true;
set hive.strict.checks.cartesian.product=true;

以上参数可以使用 set hive.mapred.mode=strict; 默认开启三种情况的严格模式。也可以使用每个限制内容参数开启指定严格校验。

3.2 搭配使用案例

也可以搭配使用,但是使用以下方式就有些问题了:

-- 关闭笛卡尔积运算校验
set hive.strict.checks.cartesian.product=flase;
-- 关闭严格模式
set hive.mapred.mode=nonstrict;

应该是严格模式默认关闭,但仍想对其中一种情况做校验。如下

set hive.mapred.mode=nonstrict;
set hive.strict.checks.cartesian.product=true;

或者严格模式默认开启,但对其中一种不想做校验:

set hive.mapred.mode=strict;
set hive.strict.checks.cartesian.product=false;

以上内容。

按例,欢迎点击此处关注我的个人公众号,交流更多知识。

后台回复关键字 hive,随机赠送一本鲁边备注版珍藏大数据书籍。

posted @ 2022-09-02 18:51  鲁边  阅读(1704)  评论(0编辑  收藏  举报