1、 需求:(将如下这张表数据导入mysql) mysql
由此,编写以下sqoop导入命令linux
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
一切看着都很正常,接下来开始执行命令,报以下错误:
一、 Error during import: No primary key could be found for table *
报错缘由就是指定的mysql表名不是大写,因此mysql表名必须大写
二、 Could not insert row with null value for row-key column
报错缘由是没有指定mysql的列名,因此必须指定列名,而且hbase-row-key id 中的id,必须在–columns中显示。 --columns ID,GOODS_NAME, GOODS_PRICE
三、 Error parsing arguments for import Unrecognized argument
报错缘由是在指定mysql的列名时,用逗号隔开的时候我多加了空格,因此在
Columns后显示的列名只能用逗号隔开,不要带空格sql
将以上三个问题排除后:个人最新导入命令变为以下:数据库
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引发来并发
查看hbase,数据已经成功导入oracle
最后我将命令写入一个xxx文件,经过sqoop –options-file xxx 执行导入命令app
错误写法以下:ide
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
错误缘由:参数的名称和参数的值没有进行回车换行oop
正确写法:spa
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 到此,bug解决完成!!!
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 <char>参数。
例子:
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