hive從mysql導(dǎo)入數(shù)據(jù)量變多的解決方案
原始導(dǎo)數(shù)命令:
bin/sqoop import -connect jdbc:mysql://192.168.169.128:3306/yubei -username root -password 123456 -table yl_city_mgr_evt_info --split-by rec_id -m 4 --fields-terminated-by "\t" --lines-terminated-by "\n" --hive-import --hive-overwrite -create-hive-table -delete-target-dir -hive-database default -hive-table yl_city_mgr_evt_info
原因分析:可能是mysql中字段里面有'\n'等分隔符,導(dǎo)入hive時默認(rèn)以'n'作換行符,導(dǎo)致hive中的記錄數(shù)變多。
解決方法:
導(dǎo)入數(shù)據(jù)時加上--hive-drop-import-delims選項,會刪除字段中的\n,\r,\01。
最終導(dǎo)數(shù)命令:
bin/sqoop import -connect jdbc:mysql://192.168.169.128:3306/yubei -username root -password 123456 -table yl_city_mgr_evt_info --split-by rec_id -m 4 --hive-drop-import-delims --fields-terminated-by "\t" --lines-terminated-by "\n" --hive-import --hive-overwrite -create-hive-table -delete-target-dir -hive-database default -hive-table yl_city_mgr_evt_info
參考官方文檔:https://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html
補(bǔ)充:Sqoop導(dǎo)入MySQL數(shù)據(jù)到Hive遇到的坑
1.sqoop導(dǎo)入到HDFS
1.1執(zhí)行sqoop job,會自動更新last value
# sqoop 增量導(dǎo)入腳本 bin/sqoop job --create sqoop_hdfs_test02 -- import \ --connect jdbc:mysql://localhost:3306/pactera_test \ --username root \ --password 123456 \ --table student \ --target-dir /user/sqoop/test002/ \ --fields-terminated-by "\t" \ --check-column last_modified \ --incremental lastmodified \ --last-value "2018-12-12 00:03:00" \ --append
說明:--append 參數(shù)是必須的,要不然第二次運行job 會報錯,如下:
至此,sqoop job 已建設(shè)完畢!
2.Hive創(chuàng)建表,并讀取sqoop導(dǎo)入的數(shù)據(jù)
create external table if not exists student_hive (SId int,Sname string ,Sage string,Ssex string , last_modified Timestamp) row format delimited fields terminated by '\t' location 'hdfs://node01:8020/user/sqoop/test002/';
注意:此處hive中時間的格式為timestamp,設(shè)置為date DB數(shù)據(jù)無法正常加載。
第一次全量加載,整條路線完全OK,hive表可以查詢到數(shù)據(jù)。
-----------------------重點分割線-----------------------
* sqoop lastmodified格式的增量加載,會將last-value 保存為job執(zhí)行的系統(tǒng)時間,若測試數(shù)據(jù)庫的check-column 小于當(dāng)前系統(tǒng)時間(即上一個job的last-value),則數(shù)據(jù)將不被加載。
如SId=6 就沒有被加載,遂改為今日時間(2018-12-26 17:05)進(jìn)行數(shù)據(jù)測試,數(shù)據(jù)成功被加載!喲呵??!
總結(jié):
使用lastmodified格式,進(jìn)行sqoop增量導(dǎo)入時,
1.注意--append的使用;
2.last-value為job運行的系統(tǒng)時間,在數(shù)據(jù)測試時,要保證數(shù)據(jù)的準(zhǔn)確,數(shù)據(jù)的自增長。
3.一切皆有定數(shù),查看資料,準(zhǔn)確定位自己系統(tǒng)遇到的問題
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Mysql鎖內(nèi)部實現(xiàn)機(jī)制之C源碼解析
數(shù)據(jù)庫之所以要加鎖,因為數(shù)據(jù)庫是一個多用戶使用的共享資源。當(dāng)多個用戶并發(fā)地存取數(shù)據(jù)時,在數(shù)據(jù)庫中就會產(chǎn)生多個事務(wù)同時存取同一數(shù)據(jù)的情況。若對并發(fā)操作不加控制就可能會讀取和存儲不正確的數(shù)據(jù),破壞數(shù)據(jù)庫的一致性2022-08-08linux下改良版本mysqldump來備份MYSQL數(shù)據(jù)庫
我的備份腳本都是在凌晨執(zhí)行的,經(jīng)常在慢查詢?nèi)罩纠锩婵吹竭@樣的信息:select * from table1; 之前一直很納悶,最后才了解到原來是MYSQLDUMP搞的鬼。2008-07-07Mysql數(shù)據(jù)庫中數(shù)據(jù)表的優(yōu)化、外鍵與三范式用法實例分析
這篇文章主要介紹了Mysql數(shù)據(jù)庫中數(shù)據(jù)表的優(yōu)化、外鍵與三范式用法,結(jié)合實例形式較為詳細(xì)的分析了Mysql數(shù)據(jù)庫中數(shù)據(jù)表的優(yōu)化、外鍵與三范式相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2019-11-11MySql修改數(shù)據(jù)庫編碼為UTF8避免造成亂碼問題
mysql 創(chuàng)建數(shù)據(jù)庫時指定編碼很重要,很多開發(fā)者都使用了默認(rèn)編碼,亂碼問題可是防不勝防,下面與大家分享下通過修改數(shù)據(jù)庫默認(rèn)編碼方式為UTF8來減少數(shù)據(jù)庫創(chuàng)建時的設(shè)置,避免因粗心造成的亂碼問題2013-06-06mysql中獲取一天、一周、一月時間數(shù)據(jù)的各種sql語句寫法
今天抽時間整理了一篇mysql中與天、周、月有關(guān)的時間數(shù)據(jù)的sql語句的各種寫法,部分是收集資料,全部手工整理,自己學(xué)習(xí)的同時,分享給大家,并首先默認(rèn)創(chuàng)建一個表、插入2條數(shù)據(jù),便于部分?jǐn)?shù)據(jù)的測試,其中部分名詞或函數(shù)進(jìn)行了解釋說明。直入主題2014-05-05