Hive 严格模式与非严格模式

1. hive严格模式
hive提供了一个严格模式,可以防止用户执行那些可能产生意想不到的不好的效果的查询。即某些查询在严格模式下无法执行。通过设置hive.mapred.mode的值为strict,可以禁止3中类型的查询。

(1) 查询一个分区表时
如果在一个分区表执行hive,除非where语句中包含分区字段过滤条件来显示数据范围,否则不允许执行。换句话说,就是用户不允许扫描所有的分区。进行这个限制的原因是,通常分区表都拥有非常大的数据集,而且数据增加迅速。 如果没有进行分区限制的查询可能会消耗令人不可接受的巨大资源来处理这个表:例如

 


hive> select * from t_patition;
FAILED: SemanticException [Error 10041]: No partition predicate found for Alias "t_patition" Table "t_patition"



注意查询的时候加上分区:


hive> select * from t_patition where country=’China’;


 

(2)带有order by的查询
对于使用了orderby的查询,要求必须有limit语句。因为orderby为了执行排序过程会讲所有的结果分发到同一个reducer中进行处理,强烈要求用户增加这个limit语句可以防止reducer额外执行很长一段时间:


hive> select * from student order by id;

FAILED: SemanticException 1:31 In strict mode, if ORDER BY is specified, LIMIT must also be specified. Error encountered near token 'id'



注意查询的时候加上limit:


select * from student order by id limit 10;


 

(3) 限制笛卡尔积的查询
对关系型数据库非常了解的用户可能期望在执行join查询的时候不使用on语句而是使用where语句,这样关系数据库的执行优化器就可以高效的将where语句转换成那个on语句。不幸的是,hive不会执行这种优化,因此,如果表足够大,那么这个查询就会出现不可控的情况:


hive> SELECT * FROM fracture_act JOIN fracture_ads
> WHERE fracture_act.planner_id = fracture_ads.planner_id;
FAILED: Error in semantic analysis: In strict mode, cartesian product
is not allowed. If you really want to perform the operation,
+set hive.mapred.mode=nonstrict+



注意查询的时候使用join和on语句的查询:


hive> SELECT * FROM fracture_act JOIN fracture_ads
> ON (fracture_act.planner_id = fracture_ads.planner_id);


 

 

posted @ 2019-04-13 12:26  Transkai  阅读(4021)  评论(0编辑  收藏  举报