sqoop从mysql导入到hive的命令如下:
sqoop import --connect jdbc:mysql://serveraddress:3306/test --username root --password pwd123456 --query "select * from user where \$CONDITIONS" --hive-table hiveuser --hive-import --hive-overwrite -m 1 --target-dir /user/hive/warehouse/huetest.db/user 如果不加其他参数,导入的数据默认的列分隔符是'\001',默认的行分隔符是'\n'。
这样问题就来了,如果导入的数据中有'\n',hive会认为一行已经结束,然后后面的数据就被分割成下一行。
这种情况下,导入hive中数据的行数就比原先数据库中的多,而且会出现数据不一致的情况。
Sqoop也指定了参数 --fields-terminated-by和 --lines-terminated-by来自定义行分隔符和列分隔符。
我们来指定这两个参数来试试:
sqoop import --connect jdbc:mysql://serveraddress:3306/test --username root --password pwd123456 --query "select * from user where \$CONDITIONS" --hive-table hiveuser --hive-import --hive-overwrite -m 1 --target-dir /user/hive/warehouse/huetest.db/user --fields-terminated-by '\001' --lines-terminated-by '\001' 执行到最后我们会发现有报错:
FAILED: SemanticException 1:226 LINES TERMINATED BY only supports newline '\n' right now. Error encountered near token ''\001''我们可以看到,虽然你通过--lines-terminated-by指定了其他的字符作为行分隔符,但是hive只支持'\n'作为行分隔符。
所以简单的解决办法就是加上参数--hive-drop-import-delims把导入数据中包含的hive默认的分隔符去掉。
本文介绍使用Sqoop从MySQL导入数据到Hive时遇到的问题,特别是关于数据分隔符导致的数据不一致现象,并提供了解决方案。

1789

被折叠的 条评论
为什么被折叠?



