我将在HDFS中的文件导入到已将建好的MySQL表中。可是出现了如下问题:
mysql> select * from endsql_table;
+------+-----------+------+------+----------+
| area | pname | pnum | rank | day |
+------+-----------+------+------+----------+
| ?? | product33 | 34 | 2 | 2016.5.5 |
| ?? | product4 | 40 | 1 | 2016.5.5 |
| ?? | product70 | 38 | 3 | 2016.5.5 |
| ?? | product48 | 19 | 3 | 2016.5.5 |
| ?? | product40 | 16 | 1 | 2016.5.5 |
| ?? | product9 | 16 | 2 | 2016.5.5 |
| ?? | product94 | 13 | 3 | 2016.5.5 |
| ?? | product38 | 35 | 1 | 2016.5.5 |
| ?? | product96 | 32 | 2 | 2016.5.5 |
| ?? | product5 | 31 | 3 | 2016.5.5 |
| ?? | product26 | 39 | 1 | 2016.5.5 |
| ?? | product7 | 39 | 2 | 2016.5.5 |
| ?? | product88 | 34 | 3 | 2016.5.5 |
| ?? | product16 | 20 | 1 | 2016.5.5 |
| ?? | product56 | 20 | 1 | 2016.5.5 |
| ?? | product67 | 20 | 2 | 2016.5.5 |
| ?? | product60 | 19 | 2 | 2016.5.5 |
| ?? | product95 | 19 | 3 | 2016.5.5 |
+------+-----------+------+------+----------+
这个错误显示我的area字段出现乱码,我查看了一下我HDFS的文件,发现那个文件的area字段为中文。
hadoop:hadoop:/home/hadoop:>hadoop fs -cat /project/endhivetable/days=2016.5.5/000000_0
18/01/24 22:28:08 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
华东 product4 40 1 2016.5.5
华东 product96 32 2 2016.5.5
华东 product5 31 3 2016.5.5
华中 product26 39 1 2016.5.5
华中 product7 39 2 2016.5.5
华中 product70 38 3 2016.5.5
华北 product40 16 1 2016.5.5
华北 product9 16 2 2016.5.5
华北 product94 13 3 2016.5.5
华南 product38 35 1 2016.5.5
华南 product33 34 2 2016.5.5
华南 product88 34 3 2016.5.5
西北 product56 20 1 2016.5.5
西北 product67 20 2 2016.5.5
西北 product48 19 3 2016.5.5
西南 product16 20 1 2016.5.5
西南 product60 19 2 2016.5.5
西南 product95 19 3 2016.5.5
所以说这应该是字符集的问题,有关于字符集不对应的问题,我认为在此案例中应该有三种情况,
我对我对这几种情况进行了排查,
首先我查看mysql方面的字符集
mysql> show create table endsql_table;
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| endsql_table | CREATE TABLE `endsql_table` (
`area` varchar(100) DEFAULT NULL,
`pname` varchar(100) DEFAULT NULL,
`pnum` int(11) DEFAULT NULL,
`rank` int(11) DEFAULT NULL,
`day` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> show full columns from endsql_table
-> ;
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| area | varchar(100) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| pname | varchar(100) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| pnum | int(11) | NULL | YES | | NULL | | select,insert,update,references | |
| rank | int(11) | NULL | YES | | NULL | | select,insert,update,references | |
| day | varchar(100) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
5 rows in set (0.15 sec)
过程中我还排查了这张表每个字段的编码格式,发现格式是正确的,并且area字段也为utf8.
下面我决定试一下sqoop的编码问题,于是我在导出语句的–connect参数中加入了设置编码格式为utf8的语句
sqoop export --connect "jdbc:mysql://localhost:3306/wl?useUnicode=true&characterEncoding=utf-8" --username root --password 123456 --table endsql_table --mapreduce-job-name sql_end --export-dir /project/endhivetable/days=2016.5.5/000000_0 --fields-terminated-by '\t'
我查绚了导出结果
mysql> select * from endsql_table;
+--------+-----------+------+------+----------+
| area | pname | pnum | rank | day |
+--------+-----------+------+------+----------+
| 华东 | product4 | 40 | 1 | 2016.5.5 |
| 西北 | product48 | 19 | 3 | 2016.5.5 |
| 华东 | product96 | 32 | 2 | 2016.5.5 |
| 华东 | product5 | 31 | 3 | 2016.5.5 |
| 华中 | product26 | 39 | 1 | 2016.5.5 |
| 华中 | product7 | 39 | 2 | 2016.5.5 |
| 华中 | product70 | 38 | 3 | 2016.5.5 |
| 华北 | product40 | 16 | 1 | 2016.5.5 |
| 华北 | product9 | 16 | 2 | 2016.5.5 |
| 华北 | product94 | 13 | 3 | 2016.5.5 |
| 华南 | product38 | 35 | 1 | 2016.5.5 |
| 西南 | product16 | 20 | 1 | 2016.5.5 |
| 西南 | product60 | 19 | 2 | 2016.5.5 |
| 西南 | product95 | 19 | 3 | 2016.5.5 |
| 华南 | product33 | 34 | 2 | 2016.5.5 |
| 华南 | product88 | 34 | 3 | 2016.5.5 |
| 西北 | product56 | 20 | 1 | 2016.5.5 |
| 西北 | product67 | 20 | 2 | 2016.5.5 |
+--------+-----------+------+------+----------+
18 rows in set (0.18 sec)
这样就可以了
总结:sqoop导入导出的编码问题,应从两个角度排错:sqoop的编码和导入导出目标的编码。
若泽大数据交流群:671914634