sqoop將mysql數據導入hbase、hive的常見異常處理

原創不易,如需轉載,請註明出處http://www.javashuo.com/article/p-waktkjwp-gs.html,不然將追究法律責任!!! html

1、需求:

一、將如下這張表(test_goods[id,goods_name,goods_price])數據導入Hbase

由此,編寫以下sqoop導入命令mysql

sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:mysql://192.168.1.9/spider --username root --password root --table test_goods --hbase-create-table --hbase-table t_goods  --column-family cf --hbase-row-key id -m 1

一切看着都很正常,接下來開始執行命令,報以下錯誤:linux

  1. Error during import: No primary key could be found for table *git

    報錯緣由就是指定的mysql表名不是大寫,因此mysql表名必須大寫github

  2. Could not insert row with null value for row-key columnsql

    報錯緣由是沒有指定mysql的列名,因此必須指定列名,而且hbase-row-key id 中的id,必須在–columns中顯示。 --columns ID,GOODS_NAME, GOODS_PRICE數據庫

  3. Error parsing arguments for import Unrecognized argumentsegmentfault

    報錯緣由是在指定mysql的列名時,用逗號隔開的時候我多加了空格,因此在
    Columns後顯示的列名只能用逗號隔開,不要帶空格併發

將以上三個問題排除後:個人最新導入命令變爲以下:oracle

sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:mysql://192.168.1.9:3306/spider --username root --password root --table TEST_GOODS --columns ID,GOODS_NAME,GOODS_PRICE --hbase-create-table --hbase-table t_goods --column-family cf --hbase-row-key ID --where "ID >= 5" -m 1

注意:這裏有個小問題:記得將id>=5引發來,ok,查看hbase,數據已經成功導入!!!

二、將導入命令寫成一個腳原本執行(經過sqoop –options-file xxx.file 執行導入命令)

  • 錯誤寫法以下:

    import
    -D sqoop.hbase.add.row.key=true 
    --connect jdbc:mysql://192.168.1.9:3306/spider 
    --username root 
    --password root 
    --table TEST_GOODS 
    --columns ID,GOODS_NAME,GOODS_PRICE 
    --hbase-create-table 
    --hbase-table test_goods 
    --column-family cf 
    --hbase-row-key ID 
    --where "ID >= 5"
    -m 1
  • 錯誤緣由:參數的名稱和參數的值沒有進行回車換行

  • 正確寫法:

    import 
    -D 
    sqoop.hbase.add.row.key=true 
    --connect 
    jdbc:mysql://192.168.1.9:3306/spider 
    --username 
    root 
    --password 
    root 
    --table 
    TEST_GOODS 
    --columns 
    ID,GOODS_NAME,GOODS_PRICE 
    --hbase-create-table 
    --hbase-table 
    tt_goods 
    --column-family 
    cf 
    --hbase-row-key 
    ID 
    --where 
    ID>=5 
    -m 
    1
  • 注:參數含義解釋

    -D sqoop.hbase.add.row.key=true 是否將rowkey相關字段寫入列族中,默認爲false,默認狀況下你將在列族中看不到任何row key中的字段。注意,該參數必須放在import以後。
    --connect 數據庫鏈接字符串
    --username –password  mysql數據庫的用戶名密碼
    --table Test_Goods表名,注意大寫
    --hbase-create-table  若是hbase中該表不存在則建立
    --hbase-table   對應的hbase表名
    --hbase-row-key   hbase表中的rowkey,注意格式
    --column-family   hbase表的列族
    --where    導入是mysql表的where條件,寫法和sql中同樣
    --split-by CREATE_TIME   默認狀況下sqoop使用4個併發執行任務,須要制訂split的列,若是不想使用併發,能夠用參數 --m 1

2、定時增量導入

一、Sqoop增量導入

sqoop import -D sqoop.hbase.add.row.key=true --connect jdbc:mysql://192.168.1.9:3306/spider --username root --password root --table TEST_GOODS --columns ID,GOODS_NAME,GOODS_PRICE --hbase-create-table --hbase-table t_goods --column-family cf --hbase-row-key ID --incremental lastmodified --check-column U_DATE --last-value '2017-06-27' --split-by U_DATE

--incremental lastmodified 增量導入支持兩種模式 append 遞增的列;lastmodified時間戳。
--check-column 增量導入時參考的列
--last-value 最小值,這個例子中表示導入2017-06-27到今天的值

二、Sqoop job

sqoop job --create testjob01 --import --connect jdbc:mysql://192.168.1.9:3306/spider --username root --password root --table TEST_GOODS --columns ID,GOODS_NAME,GOODS_PRICE --hbase-create-table --hbase-table t_goods --column-family cf --hbase-row-key ID -m 1

設置定時執行以上sqoop job
使用linux定時器:crontab -e
例如天天執行

0 0 * * * /opt/local/sqoop-1.4.6/bin/sqoop job ….
--exec testjob01

3、數據從mysql導入hive中後,出現數據不一致狀況

咱們運行hadoop fs -cat /user/hadoop/student/part-m-00000,能夠看到原來字段與字段之間都用‘,’分隔開,這是sqoop默認的,這時候,若是一個字段值當中包含‘,’,再向hive中插入數據時分隔就會出錯。由於hive也是用‘,’分隔的。

解決方法:建議用‘001'來進行sqoop 導入數據時的 分割。也就是--fields-terminated-by 參數。
例子:

sqoop import --connect "jdbc:oracle:thin:@//localhost:1521/student" --password "***" --username "***" --query "select * from student where name='zhangsan' and class_id='003' and \$CONDITIONS" --target-dir "/user/hadoop/student" --fields-terminated-by "\001" --verbose -m 1

4、總結

  • 這些只是工做中一些小問題的解決,但願對你們有所幫助~~

我的博客地址:

cnblogs:https://www.cnblogs.com/baixianlong
csdn:https://blog.csdn.net/tiantuo6513
segmentfault:https://segmentfault.com/u/baixianlong
github:https://github.com/xianlongbai

相關文章
相關標籤/搜索