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);