hive從mysql導入數據量變多的解決方案
原始導數命令:
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'等分隔符,導入hive時默認以'n'作換行符,導致hive中的記錄數變多。
解決方法:
導入數據時加上--hive-drop-import-delims選項,會刪除字段中的\n,\r,\01。
最終導數命令:
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
補充:Sqoop導入MySQL數據到Hive遇到的坑
1.sqoop導入到HDFS
1.1執(zhí)行sqoop job,會自動更新last value
# sqoop 增量導入腳本 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 參數是必須的,要不然第二次運行job 會報錯,如下:

至此,sqoop job 已建設完畢!
2.Hive創(chuàng)建表,并讀取sqoop導入的數據
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,設置為date DB數據無法正常加載。
第一次全量加載,整條路線完全OK,hive表可以查詢到數據。
-----------------------重點分割線-----------------------
* sqoop lastmodified格式的增量加載,會將last-value 保存為job執(zhí)行的系統(tǒng)時間,若測試數據庫的check-column 小于當前系統(tǒng)時間(即上一個job的last-value),則數據將不被加載。

如SId=6 就沒有被加載,遂改為今日時間(2018-12-26 17:05)進行數據測試,數據成功被加載!喲呵??!
總結:
使用lastmodified格式,進行sqoop增量導入時,
1.注意--append的使用;
2.last-value為job運行的系統(tǒng)時間,在數據測試時,要保證數據的準確,數據的自增長。
3.一切皆有定數,查看資料,準確定位自己系統(tǒng)遇到的問題
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
linux安裝兩個mysql(8.0和5.7),并同時使用方式
這篇文章主要介紹了如何在CentOS?7上下載和安裝MySQL?8.0和MySQL?5.7.30,并詳細描述了安裝步驟,包括解壓、配置、初始化和啟動等過程2024-12-12
mysql優(yōu)化小技巧之去除重復項實現(xiàn)方法分析【百萬級數據】
這篇文章主要介紹了mysql優(yōu)化小技巧之去除重復項實現(xiàn)方法,結合實例形式分析了mysql去除重復項的方法,并附帶了隨機查詢優(yōu)化的相關操作技巧,需要的朋友可以參考下2020-01-01

