Hive在大數據中可能是數據工程師使用的最多的組件,常見的數據倉庫一般都是基於Hive搭建的,在使用Hive時候,遇到了兩個奇怪的現象,今天給大家聊一下,以后遇到此類問題知道如何避坑!
坑一:改變字段類型后更新數據不成功
關於hive插入數據的一個小坑,今天插入一個表中數據,插入時寫的是常數,比如0.01 ,表中的字段也是DECIMAL(5,2),按照常理插入的應該是0.01,但是插入后查詢是0,為甚!
遇到問題,咱也不能退縮!就分析呀,看語句沒問題啊!還是上網查查有沒有坑友。查了一圈發現還是觀望最靠譜!上hive官網查,呀~ 發現了原因!
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types#LanguageManualTypes-Decimals
根據官網描述,發現在插入分區表時會出現這種情況,此時需要對之前的分區處理下~:
那就測試一下 按照官網的說法:
先建表:
CREATE TABLE `tb_dw_test`(
`a` int COMMENT '微信服務量')
PARTITIONED BY (
`statis_date` varchar(8))
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|' ;
然后插入數據:
insert overwrite table tb_dw_test partition (statis_date=20160501) values(1.02);
然后查詢:
hive> select * from tb_dw_test;
OK
1 20160501
發現結果跟想象中的一樣~
然后修改表字段:
ALTER TABLE tb_dw_test REPLACE COLUMNS (a DECIMAL(5,2))
然后再次插入數據:
insert overwrite table tb_dw_test partition (statis_date=20160501) values(1.02);
查詢:
hive> select * from tb_dw_test;
OK
1 20160501
發現有問題啦!
那么按照官網處理:
Determine what precision/scale you would like to set for the decimal column in the table. For each decimal column in the table, update the column definition to the desired precision/scale using the ALTER TABLE command:
ALTER TABLE foo CHANGE COLUMN dec_column_name dec_column_name DECIMAL(38,18);
If the table is a partitioned table, then find the list of partitions for the table:If the table is not a partitioned table, then you are done. If the table has partitions, then go on to step 3
SHOW PARTITIONS foo;
ds=2008-04-08/hr=11
ds=2008-04-08/hr=12
...
This can be done with a single ALTER TABLE CHANGE COLUMN by using dynamic partitioning (available for ALTER TABLE CHANGE COLUMN in Hive 0.14 or later, with HIVE-8411):Each existing partition in the table must also have its DECIMAL column changed to add the desired precision/scale.
SET hive.exec.dynamic.partition = true;
-- hive.exec.dynamic.partition needs to be set to true to enable dynamic partitioning with ALTER PARTITION
-- This will alter all existing partitions of the table - be sure you know what you are doing!
ALTER TABLE foo PARTITION (ds, hr) CHANGE COLUMN dec_column_name dec_column_name DECIMAL(38,18);
所以參照官網,這里對表作如下處理:
ALTER TABLE tb_dw_test PARTITION (statis_date) CHANGE COLUMN a a DECIMAL(5,2);
再次插入數據:
insert overwrite table tb_dw_test partition (statis_date=20160501) values(1.02);
然后查詢:
hive> select * from tb_dw_test;
OK
1.02 20160501
Time taken: 0.066 seconds, Fetched: 1 row(s)
發現結果跟想象中的一樣了。這個坑算是過去了~
坑二:增加字段后更新數據不成功
還是上面的例子那張表,再增加一個字段:
alter table tb_dw_test add COLUMNS (b varchar(5))
然后查詢
hive> select * from tb_dw_test;
OK
1.02 NULL 20160501
Time taken: 0.082 seconds, Fetched: 1 row(s)
發現新增的字段默認的值是NULL,現在我重新覆蓋一下元數據,給增加的字段一個值:
insert overwrite table tb_dw_test partition(statis_date=20160501) values (2.01,0371);
然后查詢:
hive> select * from tb_dw_test;
OK
2.01 NULL 20160501
不是我們想象的結果,查看一下官方文檔說明發現了問題的所在:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Add/ReplaceColumns
靠譜的官網用紅色框框提示我們!那就這樣處理:
alter table tb_dw_test replace COLUMNS (a decimal(5,2),b varchar(5)) CASCADE;
直接查詢,發現數據顯示的數據已經發生了變化了~
hive> select * from tb_dw_etst;
OK
2.01 0371 20160501
避坑指南
綜上發現,我們是按照Oracle這樣的標准在考慮HiveSQL,但是通過閱讀官方文檔發現二者之間還是有很大不同的,底層原理差別還是有點大!通過這兩次踩坑發現,仔細閱讀官方文檔的重要性!!!
所以,以后大家遇到大數據開源組件的報錯問題,第一就是去找官方文檔,然后就去找社區,總能發現驚喜!