sqoop,將oracle數據傳遞到hive,可以直接將表數據覆蓋傳遞,也可以按select傳遞,這里我用的是select查詢的
腳本如下:
1、創建分區表,按c1分區
CREATE TABLE `xx.cc`( `c1` string) PARTITIONED BY(`c2` string);
2、通過sqoop將數據導入臨時表中
sqoop import -D oraoop.jdbc.url.verbatim=true \ --hive-import --hive-overwrite \ --connect jdbc:oracle:thin:@ip:port:實例名 --username xx_name --password xx_password \ --query "select c1,c2 from table_name where c1=1 and \$CONDITIONS" \--hive-database hive_database_name --hive-table cc_temp \ --target-dir /user/hive/warehouse/xx.db/cc_temp_target \ --delete-target-dir --num-mappers 1 --fetch-size 5000 --hive-drop-import-delims --null-string '\\N' --null-non-string '\\N'
sqoop說明:
- query里面,必須有where條件,同時必須添加 \$CONDITIONS, \$CONDITIONS在執行時會自動替換位 (1= 0),但是不影響查詢
- query表如果不存在,會自動創建表,字段順序與select的字段相同;如果存在,會根據字段名一一對應
- 非常要注意的,臨時表對應的分區字段,必須放再最后面,其他字段順序是否要求相同,沒有嘗試(不理解他的原理)
- target-dir 對應的目錄,需要當前用戶有操作權限,同時,target-dir對應的目錄,不可存在,或者對應即將生成的表文件;原因如下
- target-dir是sqoop將oracle導入數據生成的臨時文件(是文件,不是目錄),如果已存在,則會報錯
- sqoop將oracle導入到hive后,會將target-dir刪除,如果target-dir對應的是即將生成的表文件,則數據會刪除
3、將臨時表,導入分區表
set hive.exec.dynamic.partition.mode=nonstrict
insert overwrite table xx.cc partition(c1) select * from xx.cc_temp;
4、出現得問題
使用過程中,出現錯誤:Cannot insert into target table because column number/types are different,后台異常提示為:node tried to create too many dynamic partitions.The maximum number of dynamic partitions is... 這是因為默認動態分區只能為100個,所以異常了。可以在執行插入數據到分區時,添加參數設置: set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; set hive.exec.max.dynamic.partitions.pernode=10000; set hive.exec.max.dynamic.partitions=10000; set hive.exec.max.created.files=10000; 或者不使用動態分區,改為新增數據,新增時按指定數據分區