Sqoop定時增量導入mysql數據到hdfs(hive)

需求

有2張大的mysql表,量級分別是1億和4.5億(太大了,DBA的同學正在考慮分表),而且數據是增量的,需要寫spark任務做處理,直接讀取mysql有點喫力,想通過sqoop定時增量直接導入hive,然後spark sql再與hive交互,能避免mysql的很多瓶頸,研究好幾天sqoop定時任務,使用的是sqoop1,版本是sqoop-1.4.6-cdh5.7.0。

1. 創建並執行sqoop job:

sqoop job -delete torderincrementjob //先刪除之前的job

 
  1.  
    sqoop job --create torderincrementjob -- import \
  2.  
    --connect jdbc:mysql://172.16.*.*:3306/*?useCursorFetch=true \
  3.  
    --username *\
  4.  
    --password-file /input/sqoop/pwd/109mysql.pwd \
  5.  
    --target-dir /mysqldb/t_order \
  6.  
    --table t_order \
  7.  
    --fields-terminated-by "\t" \
  8.  
    --lines-terminated-by "\n" \
  9.  
    --null-string '\\N' \
  10.  
    --null-non-string '\\N' \
  11.  
    --incremental append \
  12.  
    --check-column id \
  13.  
    --last-value 1281 \
  14.  
    -m 1

其中:
--password-file指定hdfs上存放的密碼
--fields-terminated-by "\t" \ 指定列分隔符,即製表符
--lines-terminated-by "\n" \ 指定行分隔符,及換行符
--split-by id \ 指定分割的字段
--null-string '\N' \ 指定string類型到hive裏的值爲NULL
--null-non-string '\N' \ 指定非string類型到hive裏的值爲NULL
--incremental append
--check-column id
--last-value 1281
以上3個參數組合使用做增量
創建完成後,執行這個job:sqoop job -exec torderincrementjob會看到在日誌裏有如下select語句:
SELECT MIN(id), MAX(id) FROM t_order WHERE ( id >= '1281' AND id < '100701508' ),也就是last-value指定的id,sqoop會自己維護記錄,下次再執行這個任務,起始id就是100701508,每次都是從上次執行的id到當前id的區間增量,這樣就能通過crontab做定時任務,定時增量導入到hdfs

每次執行sqoop都會更新last-value的值,下次從新的值開始,以下是我從3次打印的日誌裏截取的:
Lower bound value: 1281
Upper bound value: 100701508
Lower bound value: 100701508
Upper bound value: 100703035
Lower bound value: 100703035
Upper bound value: 100704475
第一次執行完job後hdfs就有數據了,可以在命令行或者通過50070查看

2. 在hive中創建外部表關聯HDFS上的數據:

 
  1.  
    CREATE external TABLE `t_order`(
  2.  
    `id` bigint,
  3.  
    `serial` string,
  4.  
    `product_id` int,
  5.  
    `product_type` tinyint,
  6.  
    `product_name` string,
  7.  
    `quantity` double,
  8.  
    `buyer_id` bigint,
  9.  
    `payer_id` bigint,
  10.  
    `price` double,
  11.  
    `vip_price` double,
  12.  
    `settle_price` double,
  13.  
    `currency` string,
  14.  
    `payer_level` tinyint,
  15.  
    `status` tinyint,
  16.  
    `pay_mode` tinyint,
  17.  
    `payment_serial` string,
  18.  
    `client_type` string,
  19.  
    `app_type` tinyint,
  20.  
    `seller_id` string,
  21.  
    `partner_id` int,
  22.  
    `reference` string,
  23.  
    `channel_source` string,
  24.  
    `note` string,
  25.  
    `expiration_time` string,
  26.  
    `operator` string,
  27.  
    `create_time` string,
  28.  
    `pay_time` string,
  29.  
    `update_time` string)
  30.  
    ROW FORMAT DELIMITED
  31.  
    FIELDS TERMINATED BY '\t'
  32.  
    LINES TERMINATED BY '\n'
  33.  
    LOCATION
  34.  
    'hdfs://golive-master:8020/mysqldb/t_order'

這時候就可以通過hive查詢hdfs上的數據了

select * from golivecms20.t_order limit 10;

3. crontab定時任務

創建如下3個文件:

timermysqltohdfs.cron //定時任務
timermysqltohdfs.sh //腳本文件
timermysqltohdfs.log //日誌文件

 
  1.  
    timermysqltohdfs.sh:
  2.  
    #!/bin/sh
  3.  
    current_time=$(date +%Y%m%d%H%M%S)
  4.  
    echo $current_time >> /data/bigdata/app/sqoopjob/timermysqltohdfs.log
  5.  
    echo ............................>> /data/bigdata/app/sqoopjob/timermysqltohdfs.log
  6.  
    #t_order表同步
  7.  
    /data/bigdata/sqoop-1.4.6-cdh5.7.0/bin/sqoop job -exec torderincrementjob
  8.  
    #t_userlogout表同步
  9.  
    /data/bigdata/sqoop-1.4.6-cdh5.7.0/bin/sqoop job -exec tuserlogoutincrementjob
  10.  
     
  11.  
    timermysqltohdfs.cron(每天1點、7點、13點、19點定時執行):
  12.  
    00 1,7,13,19 * * * /bin/bash /data/bigdata/app/sqoopjob/timermysqltohdfs.sh >> /data/bigdata/app/sqoopjob/timermysqltohdfs.log 2>&1
  13.  
     

另一個表t_userlogout也是一樣,相關命令如下:
創建job:

 
  1.  
    sqoop job --create tuserlogoutincrementjob -- import \
  2.  
    --connect jdbc:mysql://172.16.*.*:3306/*?useCursorFetch=true \
  3.  
    --username *\
  4.  
    --password-file /input/sqoop/pwd/68mysql.pwd \
  5.  
    --target-dir /mysqldb/t_userlogout \
  6.  
    --table t_userlogout \
  7.  
    --fields-terminated-by "\t" \
  8.  
    --lines-terminated-by "\n" \
  9.  
    --null-string '\\N' \
  10.  
    --null-non-string '\\N' \
  11.  
    --incremental append \
  12.  
    --check-column ID \
  13.  
    --last-value 1 \
  14.  
    -m 1

首次執行:sqoop job -exec tuserlogoutincrementjob
在hive創建外部表:

 
  1.  
    CREATE external TABLE `t_userlogout`(
  2.  
    `ID` bigint,
  3.  
    `GoliveId` string,
  4.  
    `InstalmentCode` string,
  5.  
    `ManufacturerCode` string,
  6.  
    `MacAddress` string,
  7.  
    `AreaCode` string,
  8.  
    `IpAddress` string,
  9.  
    `LoginTime` string,
  10.  
    `LogoutTime` string,
  11.  
    `DeviceID` string,
  12.  
    `VersionType` string,
  13.  
    `Version` string,
  14.  
    `Platform` string,
  15.  
    `PartnerID` int,
  16.  
    `BranchType` int,
  17.  
    `LicenseProviderCode` string)
  18.  
    ROW FORMAT DELIMITED
  19.  
    FIELDS TERMINATED BY '\t'
  20.  
    LINES TERMINATED BY '\n'
  21.  
    LOCATION
  22.  
    'hdfs://golive-master:8020/mysqldb/t_userlogout'

後邊就是定時任務,增量導入了

附錄

除了指定--table導入表的全部字段,也可以通過--query指定sql:

 
  1.  
    --query "select ID,GoliveId,InstalmentCode,ManufacturerCode,MacAddress,IpAddress,LoginTime,VersionType,Version,PartnerID from t_userlogout where $CONDITIONS" \
  2.  
     
  3.  
    --query "select id,serial,product_id,product_type,product_name,buyer_id,price,vip_price,settle_price,status,pay_mode,client_type,seller_id,partner_id,channel_source,expiration_time,create_time,pay_time,update_time from t_order where $CONDITIONS" \

 

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