使用Sqoop迁移MySQL数据到Hive,数据条数不一致

使用Sqoop迁移MySQL数据到Hive,数据条数不一致

问题描述

项目上有一个将 MySQL 数据迁移到 Hive 数据的需求,使用了Sqoop 工具,但是在迁移结束后核验数据的时候发现 Hive 中数据始终比 MySQL 中多了几行;

在执行Sqoop 成功之后,日志输出有显示Retrieved 775044 records.

2020-04-17 15:31:40,990 INFO [main] mapreduce.ImportJobBase (ImportJobBase.java:runJob(183)) - Transferred 134.8739 MB in 54.6111 seconds (2.4697 MB/sec)
2020-04-17 15:31:40,995 INFO [main] mapreduce.ImportJobBase (ImportJobBase.java:runJob(185)) - Retrieved 775044 records.
from mysql sqoop sparkuiinfo data to HDFS succeeded.
[2020-04-17 15:31:41.416][pool-5-thread-34] INFO i.t.workflow.job.AbstractJob - About to end the job

MySQL 中核实发现数据也确实是
MySQL中核实数据条数一致是775044
但是在Hive 中根据 HDFS 文件建表后发现数据条数是790313,比 MySQL 多了 15000+ 条
核实 hive 中数据条数

问题分析

使用的Sqoop 命令以及参数如下:

sqoop import \
--options-file /home/config/mysql5728  \
--query 'select * from East.sparkui_4 where $CONDITIONS' \
--target-dir /eastmoney/sparkuiinfo \
--fields-terminated-by ',' \
--null-string '\\N' --null-non-string '\\N' \
--merge-key id \
--split-by id -m 1

因为看到 Sqoop 执行后的命令输出数据记录是正确的,所以猜测可能是在写入 Hive 的过程中有一些问题;
然后我核验了Hive 中数据,发现字段对应是乱的,有一些在MySQL中是一个字段的数据,在Hive 中被拆分成了2个字段;然后查看了Hive 手册,在Hive中建表时,如果没有指定列分隔符,默认是使用 ‘,’ 作为分隔符的,而我的数据内容中刚好有 ‘,’,导致在Hive 中被切分成了两个字段。
核验Hive 中数据

解决方法

找到问题原因之后,解决方案也就比较简单了,Sqoop 用户指南中提到了这种问题,并给出了建议:
Sqoop用户指南

Even though Hive supports escaping characters, it does not handle escaping of new-line character. Also, it does not support the notion of enclosing characters that may include field delimiters in the enclosed string. It is therefore recommended that you choose unambiguous field and record-terminating delimiters without the help of escaping and enclosing characters when working with Hive; this is due to limitations of Hive’s input parsing abilities.

在原有sqoop语句中添加 --hive-drop-import-delims,这个参数是将MySQL中字符串中的特殊字符(比如\n, \r, and \01等)删掉。

修改后Sqoop 命令以及参数如下

sqoop import \
--options-file /home/config/mysql5728  \
--query 'select * from East.sparkui_4 where $CONDITIONS' \
--target-dir /eastmoney/sparkuiinfo \
--fields-terminated-by ',' \
--hive-drop-import-delims \
--null-string '\\N' --null-non-string '\\N' \
--merge-key id \
--split-by id -m 1

结果验证

重跑一遍Sqoop,然后查看了 Hive 中数据条数和 MySQL 中一致;
Hive 中数据条数正常
PS:这个参数是删除了MySQL字符串中的无效字符,可能会影响数据最好的一致性,谨慎使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值