mysql sqoop hbase_sqoop将mysql数据导入hbase、hive的血与泪

一、 需求:(将以下这张表数据导入mysql)

7073b85c2eeda8f7128333c47f99bf2a.png

由此,编写如下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引起来

查看hbase,数据已经成功导入

84ad27ff3cccd5f41ea1f146af7231a2.png

最后我将命令写入一个xxx文件,通过sqoop –options-file xxx 执行导入命令

错误写法如下:

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

到此,bug解决完成!!!

二、知识拓展,定时增量导入

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值