Sqoop 導入導出經驗淺談

1.在增量導出模式下如何選擇 update-key

    在增量導出模式下,無論是allowinsert模式還是updateonly模式,都需要設置update-key:

    • allowinsert模式:該模式下生成的是insert語句,從這個角度講update-key是沒有作用的,但是在CDH Sandbox上測試時發現,如果不指定update-key則會導致reduce執行失敗。

    • updateonly模式:該模式下生成的是update語句,update-key中指定的字段用於構成update語句中的where字句,因此是必須的,同時也可以看出選擇的update-key字段必須是未被更新的字段,這樣才能確定一條記錄導出前後是否一致,而如果將update-key設置爲被更新過的字段,則在目標表中通過where條件篩選不到匹配的記錄,從而造成數據無法被更新。此外,如果update-key中指定了所有字段,也會報錯。

    假設如下爲抽取到MySQL中的數據,假設Hive中的源數據被更新,如name列不一致,則可以指定update-key爲id;若是id被更新了,則可以指定update-key爲name(此時再指定id是無法更新數據的)

 

2.Sqoop導入導出Null存儲一致性問題

    Hive中的Null在底層是以“\N”來存儲,而MySQL中的Null在底層就是Null,爲了保證數據兩端的一致性。在導出數據時採用--input-null-string和--input-null-non-string兩個參數。導入數據時採用--null-string和--null-non-string。

 

3.Sqoop數據導出一致性問題

3.1 場景1

    如Sqoop在導出到Mysql時,使用4個Map任務,過程中有2個任務失敗,那此時MySQL中存儲了另外兩個Map任務導入的數據,此時老闆正好看到了這個報表數據。而開發工程師發現任務失敗後,會調試問題並最終將全部數據正確的導入MySQL,那後面老闆再次看報表數據,發現本次看到的數據與之前的不一致,這在生產環境是不允許的。

    官網:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html

Since Sqoop breaks down export process into multiple transactions, it is possible that a failed export job may result in partial data being committed to the database. This can further lead to subsequent jobs failing due to insert collisions in some cases, or lead to duplicated data in others. You can overcome this problem by specifying a staging table via the --staging-table option which acts as an auxiliary table that is used to stage exported data. The staged data is finally moved to the destination table in a single transaction.

 

    –staging-table方式

sqoop export --connect jdbc:mysql://192.168.137.10:3306/user_behavior --username root --password 123456 --table app_cource_study_report --columns watch_video_cnt,complete_video_cnt,dt --fields-terminated-by "\t" --export-dir "/user/hive/warehouse/tmp.db/app_cource_study_analysis_${day}" --staging-table app_cource_study_report_tmp --clear-staging-table --input-null-string '\N'

3.2 場景2

    設置map數量爲1個(不推薦使用)

    多個Map任務時,採用–staging-table方式,仍然可以解決數據一致性問題。

 

4.Sqoop底層運行的任務是什麼

    只有Map階段,沒有Reduce階段的任務。

 

5.Sqoop數據導出Parquet

    Sqoop往MySql中導入數據的時候,如果用了orc(Parquet)不能導入,需轉化成text格式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章