您现在的位置是:首页 > 文章详情

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

日期:2019-04-12点击:353

sqoop将mysql数据导入hbase、hive的常见异常处理
一、需求:
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
一切看着都很正常,接下来开始执行命令,报如下错误:

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后显示的列名只能用逗号隔开,不要带空格

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

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
四、总结
这些只是工作中一些小问题的解决,希望对大家有所帮助~~
原文出处https://www.cnblogs.com/baixianlong/p/10700700.html

原文链接:https://yq.aliyun.com/articles/698059
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章