HiveSql调优系列之Hive严格模式,如何合理使用Hive严格模式
目录
- 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,随机赠送一本鲁边备注版珍藏大数据书籍。