sqoop从mysql导入到hive常见问题
  TEZNKK3IfmPf 2023年11月12日 29 0

sqoop从mysql导入到hive常见问题

mysql中的数据,表中列出了数据导入过程中的“脏”数据\t,\n,\r,Null,下面列出了解决思路

    id=2 这里的des 里面有1个换行符实际为

1234
567
    id=3  desc有一个\t  实际为1234    567
    id=4  desc is null

如果直接通过一般的sqoop导入语句

bin/sqoop import \
    --connect jdbc:mysql://hadoop102:3306/test \
    --username root \
    --password 123456 \
    --table staff \
    --target-dir /user/hive/warehouse/gmall.db/staff \
    --delete-target-dir \
    --num-mappers 1 \
    --fields-terminated-by "\t"

导入后的结果是

staff.id    staff.name                staff.desc            staff.birth
1            chenchi1                1234567                2019-09-01 16:46:08
2            chenchi2                1234                NULL
567            2019-09-05 10:35:05.0    NULL                NULL
3            chenchi3                1234                NULL
4            chenchi4                null                2019-09-02 16:59:21

没有看错就是乱码了,hive里面我用的fields-terminated-by "\t" ,row formatted by '\n',具体就不详解上述原因了

问题有三,1、id=2换行符出现问题,2,id=3tab键出现问题,3、Null值显示为 字符串“null”

1、会导致数据量增多

2、会导致字段不匹配

3、有点坑爹,根本看不出来,本人亲自遇见过,在mysql数据库中倒入到hive后,总数据一模一样,但是查where a is null  and date =yyyy-mm  这种时数据一直对不上

解决办法

bin/sqoop import \
    --connect jdbc:mysql://hadoop102:3306/test \
    --username root \
    --password 123456 \
    --target-dir /user/hive/warehouse/gmall.db/staff \
    --delete-target-dir \
    --num-mappers 1 \
    --fields-terminated-by "t" \
    --hive-delims-replacement "|" \
    --query 'select id,name,REPLACE(des, CHAR(9), "") des ,birth from staff where 1=1 and $CONDITIONS;' \
    --null-string '\\N' \
    --null-non-string '\\N'

hive中查出的数据,备注:使用query是不指定 --table staff \

staff.id        staff.name        staff.desc        staff.birth
1                chenchi1        1234567            2019-09-01 16:46:08
2                chenchi2        1234||567        2019-09-05 10:35:05
3                chenchi3        1234567            2019-09-05 10:33:31
4                chenchi4        NULL            2019-09-02 16:59:21

原理

--hive-delims-replacement "|" \   将换行符替换
--hive-drop-import-delims         将换行符\n, \r, and \01等舍弃
--replace 将mysql表中的\t替换掉,也可以替换\n\r char (9):tab | char(10):  换行符 |char(13):  回车符
--null-string '\\N' \        将字符串中Null保留,如果没有mysql中的Null在hive里会变成null
--null-non-string '\\N'

其实这些都是官网上有说明的,沉下心多看看sqoop官网。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   27   0   0 mysql
  TEZNKK3IfmPf   2024年05月17日   54   0   0 sqlmysql
  TEZNKK3IfmPf   2024年05月31日   31   0   0 数据库mysql
  TEZNKK3IfmPf   2024年05月17日   50   0   0 查询mysql索引
  TEZNKK3IfmPf   2024年05月17日   50   0   0 jsonmysql
  TEZNKK3IfmPf   2024年05月17日   50   0   0 mysqlphp
  TEZNKK3IfmPf   2024年05月31日   27   0   0 数据库mysql