最近在學習使用Hive(版本0.13.1)的過程中,發現了一些坑,它們或許是Hive提倡的比關系數據庫更加自由的體現(同時引來一些問題),或許是一些bug。總而言之,這些都需要使用Hive的開發人員額外注意。本文旨在列舉我發現的3個通過查詢語句向表中插入數據過程中的問題,希望大家注意。
為了驗證接下來出現的問題,需要先准備兩張表employees和staged_employees,並准備好測試數據。首先使用以下語句創建表employees:
- create table employees (
- id int comment 'id',
- name string comment 'name')
- partitioned by (country string, state string)
- row format delimited fields terminated by ',';
employees的結構比較簡單,有id、name、country、state四個字段,其中country和state都是分區字段。特別需要提醒的是這里顯示的給行格式指定了字段分隔符為逗號,因為默認的字段分隔符\001不便於筆者准備數據。然后創建表staged_employees:
- create table staged_employees (
- id int comment 'id',
- user_name string comment 'user name')
- partitioned by (cnty string, st string);
staged_employees也有4個字段,除了字段名不同之外,和employees的4個字段的含義是相同的。
我們首先使用以下語句給employees的country等於US,state等於CA的分區加載一些數據:
- load data local inpath '${env:HOME}/test.txt'
- into table employees
- partition (country = 'US', state = 'CA');
再給employees的country等於CN,state等於BJ的分區加載一些數據:
- load data local inpath '${env:HOME}/test2.txt'
- overwrite into table employees
- partition (country = 'CN', state = 'BJ');
以上語句的執行過程如圖1所示。
圖1 給employees加載數據
最后我們看看employees中准備好的數據,如圖2所示。
圖2 employees中准備好的數據
INSERT OVERWRITE的歧義
由於staged_employees中還沒有數據,所以我們查詢employees的數據,並插入staged_employees中:
- insert overwrite table staged_employees
- partition (cnty = 'US', st = 'CA')
- select * from employees e
- where e.country = 'US' and e.state = 'CA';
大家看看這條sql有沒有問題?最終的執行結果如圖3所示。
由於圖3中的文字太小,這里把這些錯誤提示信息列在下邊:
- FAILED: SemanticException [Error 10044]: Line 1:23 Cannot insert into target table because column number/types are different ''CA'': Table insclause-0 has 2 columns, but query has 4 columns.
我們的sql應該沒有問題吧?仔細查看提示信息,說是“表只有2列,但是查詢有4列”。剛才說過,我建的兩張表除了字段名稱的差異,其結構完全一樣。兩張表都有4個字段(2個普通字段和2個分區字段),為什么說staged_employees只有2列呢?這是因為Hive遵循讀時模式且遵循相對寬松的語法,在插入或裝載數據時,不會驗證數據與表的模式是否匹配。只有在讀取數據時才會驗證。因此在向表staged_employees插入數據時不會驗證,而查詢讀取employees表中的數據時會驗證。我對sql進行了調整,調整后的清單如下:
- insert overwrite table staged_employees
- partition (cnty = 'US', st = 'CA')
- select e.id, e.name from employees e
- where e.country = 'US' and e.state = 'CA';
執行這條sql的過程如圖4所示。
圖4 正確執行insert overwrite
我們看看staged_employees表中,現在有哪些數據(如圖5所示):
圖5 staged_employees中的數據
熟悉MySQL等關系型數據庫的同學可能要格外注意此問題了!
FROM ... INSERT ... SELECT的歧義
本節正式開始之前,向employees表中再加載一些數據:
- load data local inpath '${env:HOME}/test3.txt'
- into table employees
- partition (country = 'CA', state = 'ML');
執行上面sql的過程如圖6所示。
圖6 加載新的數據
這時表employees的數據如圖7所示。
圖7
Hive提供了一種特別的INSERT語法,我們不妨先看看其使用方式,sql如下:
- from employees e
- insert into table staged_employees
- partition (cnty = 'CA', st = 'ML')
- select * where e.country = 'CA' and e.state = 'ML';
執行這條sql的過程如圖8所示。
圖8 SemanticException [Error 10044]
可以看到這里再次出現了之前提到的問題,我們依然按照之前的方式進行修改,sql如下:
- from employees e
- insert into table staged_employees
- partition (cnty = 'CA', st = 'ML')
- select e.id, e.name where e.country = 'CA' and e.state = 'ML';
現在執行這條sql,發現可以成功執行,如圖9所示。
圖9
現在來看看staged_employees中的數據(如圖10所示),看來的確將分區數據插入了。
圖10 staged_employees中的數據
FROM ... INSERT ... SELECT存在bug
我們繼續使用FROM ... INSERT ... SELECT語法向staged_employees中插入數據,sql如下:
- from employees e
- insert into table staged_employees
- partition (cnty = 'US', st = 'CA')
- select e.id, e.name where e.country = 'US' and e.state = 'CA';
這條sql很明顯是向staged_employees中再次插入country等於US,state等於CA分區的數據,根據INSERT INTO的通常含義,應當是向表中追加,我們執行這段sql來驗證一下,如圖11所示。
圖11
我們看看這時staged_employees中的數據,如圖12所示。
圖12
的確印證了,INSERT INTO是用於追加的。
我們將sql進行調整,即將INSERT INTO改為INSERT OVERWRITE:
- from employees e
- insert overwrite table staged_employees
- partition (cnty = 'US', st = 'CA')
- select e.id, e.name where e.country = 'US' and e.state = 'CA';
執行這條sql的過程如圖13所示。
圖13
我們看看這時staged_employees中的數據,如圖14所示。
圖14
這說明INSERT OVERWRITE是用於覆蓋的。
根據官方文檔說明,這種FROM ... INSERT ... SELECT語法中的INSERT ... SELECT是可以有多個的,於是我編寫以下sql,用來向表staged_employees中覆蓋“country等於CA,state等於ML”分區的數據,並且覆蓋“country等於US,state等於CA”分區的數據。
- from employees e
- insert overwrite table staged_employees
- partition (cnty = 'US', st = 'CA')
- select e.id, e.name where e.country = 'US' and e.state = 'CA'
- insert overwrite table staged_employees
- partition (cnty = 'CA', st = 'ML')
- select e.id, e.name where e.country = 'CA' and e.state = 'ML';
執行以上sql的過程如圖15所示。
圖15
由於都是覆蓋更新,所以staged_employees中的數據並未發生改變。
根據官方文檔,以上sql中還可以將INSERT OVERWRITE和INSERT INTO進行混用,sql如下:
- from employees e
- insert overwrite table staged_employees
- partition (cnty = 'US', st = 'CA')
- select e.id, e.name where e.country = 'US' and e.state = 'CA'
- insert into table staged_employees
- partition (cnty = 'CN', st = 'BJ')
- select e.id, e.name where e.country = 'CN' and e.state = 'BJ';
這段sql將覆蓋“country等於US,state等於CA”分區的數據,並且追加“country等於CN,state等於BJ”分區的數據。執行這段sql的過程如圖16所示。
圖16
最后,我們來看看staged_employees中的數據,如圖17所示。
圖17
從圖17中看到,“country等於CN,state等於BJ”分區的數據如我們所願追加到表staged_employees中了。“country等於US,state等於CA”分區的數據並沒有被覆蓋,而是追加。這很明顯是一個bug,希望大家注意!
轉自:http://blog.csdn.net/beliefer/article/details/51860510