Sqoop imports data from hive into MySQL and cannot display Chinese normally - solved

question:

 reason:

View results


question:

After doing exercises and using the sqoop tool to import tables in hive into MySQL, the Chinese part cannot be displayed normally when viewed in MySQL.

Enter the sqoop execution statement

sqoop export -connect "jdbc:mysql://HadoopMaster:3306/hive_to_mysql?useUnicode=true&characterEncoding=UTF-8" -username sqoop03 -password 123456 -table hive_resoult1 --fields-terminated-by ',' --export-dir /user/hive_mysql/resoult/resoult1/

There are no errors during execution, but when mysql queries the imported data, all Chinese characters are replaced with Chinese characters:
+------+--------+
| name | object |
+--- ---+--------+
| ?? | ??? |
| ?? | ??? |
+------+--------+

reason:

Sqoop uses UTF-8 encoding by default to export data to MySQL, but MySQL uses latin1 encoding by default, so garbled characters will appear when displaying Chinese in MySQL.

Solution:

First. Add the data encoding method charset utf8 collate utf8_general_ci when creating the mysql table.

create
table hive_to_mysql.hive_resoult1
(
name varchar(30),
object varchar(30)
)
charset utf8 collate utf8_general_ci;

Finally. Modify the command for sqoop to import hive data into the mysql database: add double quotes and character set encoding when specifying the connection: 

"jdbc:mysql://HadoopMaster:3306/hive_to_mysql?useUnicode=true&characterEncoding=UTF-8" 

sqoop export 
--connect "jdbc:mysql://HadoopMaster:3306/hive_to_mysql?useUnicode=true&characterEncoding=UTF-8" \
--username sqoop03 \
--password 123456  \
--table hive_resoult1  \
--fields-terminated-by ','  \
--export-dir /user/hive_mysql/resoult/resoult1/ \

View results

After importing, go to mysql to check and the Chinese will be displayed normally.

 

Guess you like

Origin blog.csdn.net/m0_61232019/article/details/130623277