sqoop将mysql数据导入hbase、hive的常见异常处理

原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10700700.html,否则将追究法律责任!!!

一、需求:

1、将以下这张表(test_goods[id,goods_name,goods_price])数据导入Hbase

由此,编写如下sqoop导入命令

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

一切看着都很正常,接下来开始执行命令,报如下错误:

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

    报错原因就是指定的mysql表名不是大写,所以mysql表名必须大写

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

    报错原因是没有指定mysql的列名,所以必须指定列名,并且hbase-row-key id 中的id,必须在–columns中显示。 --columns ID,GOODS_NAME, GOODS_PRICE

  3. Error parsing arguments for import Unrecognized argument

    报错原因是在指定mysql的列名时,用逗号隔开的时候我多加了空格,所以在
    Columns后显示的列名只能用逗号隔开,不要带空格

将以上三个问题排除后:我的最新导入命令变为如下:

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,数据已经成功导入!!!

2、将导入命令写成一个脚本来执行(通过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
    

二、定时增量导入

1、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到今天的值

2、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

三、数据从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

四、总结

  • 这些只是工作中一些小问题的解决,希望对大家有所帮助~~

个人博客地址:

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

posted @ 2019-04-13 12:55  会炼钢的小白龙  阅读(1233)  评论(0编辑  收藏  举报