使用Sqoop进行Mysql UTF8数据导入的问题

时间:2017-09-12 15:57:54

标签: mysql utf-8 sqoop

我使用sqoop import将utf8数据从MySql导入HDFS。 它工作正常,但在数据为utf-8时面临问题。 源MySql表是utf-8兼容的,但看起来sqoop在导入期间转换数据。 示例 - 源值 - л.с。加载为л.Ñ。到HDFS。

目前,Mysql(v5.6.10)字符集&整理如下:

+--------------------------+-----------------------------------------+
| Variable_name            | Value                                   |
+--------------------------+-----------------------------------------+
| character_set_client     | latin1                                  |
| character_set_connection | latin1                                  |
| character_set_database   | latin1                                  |
| character_set_filesystem | binary                                  |
| character_set_results    | latin1                                  |
| character_set_server     | utf8                                    |
| character_set_system     | utf8                                    |
| collation_connection     | latin1_swedish_ci                       |
| collation_database       | latin1_swedish_ci                       |
| collation_server         | utf8_unicode_ci                         |
+--------------------------+-----------------------------------------+

-- Table Structure
CREATE TABLE utf_test_cases_ms 
(
  test_case varchar(50) NOT NULL,
  english_lang varchar(250) NOT NULL,
  language_name varchar(50) NOT NULL,
  utf8_lang varchar(300) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;

-- Mysql
select * from utf_test_cases_ms;
+--------------------+--------------+---------------+-----------+
| test_case          | english_lang | language_name | utf8_lang |
+--------------------+--------------+---------------+-----------+
| Multiple Character | hp           | Russian       | л.с.    |
+--------------------+--------------+---------------+-----------+

-- Sqoop Import Command
sqoop import --connect jdbc:mysql://<<IP_ADDRESS_WITH_PORT>>/<<DB_NAME>> 
--table utfmb_test_cases_ms --username sqoop_user --password sqoop_pwd 
--hive-import --hive-table utf_ms_db.utfmb_test_cases_ms 
--create-hive-table --null-string '\\N' --null-non-string '\\N'  
--fields-terminated-by '|'  --lines-terminated-by '\n' -m 1 

-- Hive (HDFS)
select * from utfmb_test_cases_ms;
Multiple Character  hp  Russian л.Ñ.

我是否需要更改字符集&amp; Mysql配置文件中的排序规则?在通过sqoop导入数据时是否需要传递额外的unicode / utf8参数?

请为此提供解决方案。 提前谢谢!

(来自评论)

CREATE TABLE utf_test_cases_ms (
    test_case varchar(50) NOT NULL, 
    english_lang varchar(250) NOT NULL, 
    language_name varchar(50) NOT NULL, 
    utf8_lang varchar(300) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ; 

2 个答案:

答案 0 :(得分:0)

在sqoop命令中传递字符集参数

sqoop import --connect jdbc:mysql://server.foo.com/db --table bar \
--direct -- --default-character-set=latin1

答案 1 :(得分:0)

或许@marjun的建议应该涉及utf8?

无论如何,л.с. л.Ñ.出现的是#34; Mojibake&#34;如中讨论的那样 http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored

请提供SHOW CREATE TABLE,以便我们验证是否已正确设置。

如果连接参数不起作用,请使用SET NAMES utf8