今天使用sqoop1.4.5版本的(hadoop使用cdh5.4)因为乱码问题很是头痛半天。下面进行一一总结
命令:
[root@sdzn-cdh01 etc]# sqoop export --connect "jdbc:mysql://192.168.200.40:3306/otherdb?useUnicode=true&characterEncoding=utf-8" -m 1 --username root --password root --table use_02 --export-dir /sdzn_yhhx/user0/ --input-null-string "\\\\N" --input-null-non-string "\\\\N" --input-fields-terminated-by "," --input-lines-terminated-by "\\n"
一.连接数据库主机失败
异常表象:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
1.没有mysql-connect-jdbc.jar的驱动jar包
2.不能ping通访问的主机
二.访问权限问题
异常:
Error executing statement: java.sql.SQLException: Access denied for user 'root'@'sdzn-cdh01.zhiyoubao.com' (using password: YES)
问题分析出现上述权限异常,说明200.40所属的mysql中的otherdb数据库没有设置sdzn-cdh01主机的访问权限。换句话,192.168.200.40所在的mysql禁止sdzn-cdh01主机访问,或者访问数据库的名称、密码有误。因为在默认情况下mysql的中的数据库的访问信息为:root@‘localhost’,所以要重置其访问权限。需要说明一点数据库的访问登录名和密码与其进入数据库操作平台的登录名和密码完全是两回事不要将二者混为一谈
操作如下:
首先进入mysql 命令操作平台查看用户权限,以确保是否存在该用户的权限
#mysql -u root -p
mysql>SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
如没有再去为该主机的用户添加权限
添加相应权限
grant 权限 on 数据库名.表名 to 用户名@'可以访问的地址' identified by "密码" 具体解释如链接:http://blog.csdn.net/xiaoshunzi111/article/details/52803663
设置之后要刷新mysql>flush privileges;
三.数据格式问题
如下所示:
Caused by: java.lang.RuntimeException: Can't parse input data: '1,i'
at use_02.__loadFromFields(use_02.java:249)
at use_02.parse(use_02.java:192)
at org.apache.sqoop.mapreduce.TextExportMapper.map(TextExportMapper.java:83)
异常分析:
同步的为mysql结构化数据,对数据格式有固定的要求。所以说当集群文件中数据格式与mysql表中存在冲突时,会导致数据传输异常。
问题解决:1.查看hdfs中文件数据是否与mysql表中对应的表字段数据类型一致(hdfs中string对应mysql中varchar或者char类型等等)
2.查看sqoop命令中分隔符是否与hdfs相应文件中分隔符一致
四.中文乱码问题
最后在说乱码问题,万事俱备只欠东风。乱码问题是同步数据中最常见的问题之一。
具体的乱码显示不在贴出。
问题分析:乱码问题由于两个平台数据编码不一致造成的。或者远程连接平台编码问题以及sqoop命令中编码问题
1.修改sqoop编码[root@sdzn-cdh01 etc]# sqoop export --connect "jdbc:mysql://192.168.200.40:3306/otherdb?useUnicode=true&characterEncoding=utf-8"
2.在创建数据库时指定编码,
mysql> CREATE DATABASE `otherdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; mysql>use otherdb;mysql> create table use_02 (remark varchar(20),groupName varchar(225)) charset utf8 collate utf8_general_ci;
3.修改mysql数据编码
具体连接:http://blog.csdn.net/xiaoshunzi111/article/details/52817034
4.修改远端访问平台编码
五.源数据中存在特殊字符
原:
[root@sdzn-cdh01 ~]# sqoop export --connect "jdbc:mysql://192.168.0.197:3306/otherdb?useUnicode=true&characterEncoding=utf-8" -m 1 --username root --password root --table order_raw_info --export-dir '/user/hive/warehouse/zyb.db/order_raw_info/' --input-null-string "\\\\N" --input-null-non-string "\\\\N" --input-fields-terminated-by "\001" --in^Ct-lines-terminated-by "\\n"
异常:
Caused by: java.io.IOException: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xB025...' for column 'link_name' at row 52
at org.apache.sqoop.mapreduce.AsyncSqlRecordWriter.write(AsyncSqlRecordWriter.java:233)
at org.apache.sqoop.mapreduce.AsyncSqlRecordWriter.write(AsyncSqlRecordWriter.java:46)
at org.apache.hadoop.mapred.MapTask$NewDirectOutputCollector.write(MapTask.java:655)
at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112)
at org.apache.sqoop.mapreduce.TextExportMapper.map(TextExportMapper.java:84)
... 10 more
Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xB025...' for column 'link_name' at row 52
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at org.apache.sqoop.mapreduce.AsyncSqlOutputFormat$AsyncSqlExecThread.run(AsyncSqlOutputFormat.java:233)
方案:
[root@sdzn-cdh01 ~]# sqoop export --connect "jdbc:mysql://192.168.0.197:3306/otherdb?useUnicode=true&characterEncoding=gbk" -m 1 --username root --password root --table order_raw_info --export-dir '/user/hive/warehouse/zyb.db/order_raw_info/' --input-null-string "\\\\N" --input-null-non-string "\\\\N" --input-fields-terminated-by "\001" --in^Ct-lines-terminated-by "\\n"
在sql中如图:
SQL语句:
create table order_raw_info (id int,create_time varchar(50),order_code varchar(50),link_name varchar(225),sex varchar(50),city varchar(50),tel varchar(50),certificate_no varchar(50),close_total_price double,popnum int,occ_date varchar(50),tourname varchar(50)) charset gbk ;
完!