mysql 中文乱码_Sqoop将hive数据导出到MySQL中文乱码了怎么办?

29e38464f68d8e426ac9292ca4504447.png

首先简单说下sqoop的安装:

  • 准备sqoop的安装包sqoop-1.4.5.bin__hadoop-0.23.tar.gz和MySQL的驱动mysql-connector-java-5.1.43-bin.jar。
  • 安装包上传至Linux服务器。
  • 解压sqoop-1.4.5.bin__hadoop-0.23.tar.gz后配置sqoop的环境变量。
  • 修改sqoop的sqoop-env.sh的配置:

# See the License for the specific language governing permissions and

# limitations under the License.

# included in all the hadoop scripts with source command

# should not be executable directly

# also should not be passed any arguments, since we need original $*

# Set Hadoop-specific environment variables here.

#Set path to where bin/hadoop is available

export HADOOP_COMMON_HOME=/home/plus/hadoop/hadoop-2.7.3

#Set path to where hadoop-*-core.jar is available

export HADOOP_MAPRED_HOME=/home/plus/hadoop/hadoop-2.7.3

#set the path to where bin/hbase is available

#export HBASE_HOME=

#Set the path to where bin/hive is available

export HIVE_HOME=/home/plus/hadoop/apache-hive-1.2.1-bin

#Set the path for where zookeper config dir is

export ZOOCFGDIR=/home/plus/hadoop/zookeeper-3.4.6/conf

  • 将MySQL驱动放置到sqoop的lib目录里面。

sqoop将MySQL数据导入hive

sqoop import --connect jdbc:mysql://plus111:3306/test --username root --password 123456 --table Persons -m 1 --hive-import --hive-table aa --fields-terminated-by ''

  • --connect jdbc:mysql://plus111:3306/testjdbc连接MySQL,指定MySQL的ip和端口和数据库
  • --usernameMySQL的用户名
  • --passwordMySQL的密码
  • --tableMySQL的表
  • -m 1使用一个map
  • --hive-import数据导入hive
  • --hive-tablehive的表
  • --fields-terminated-byhive表的分隔符

sqoop将hive数据导出的MySQL:

sqoop-export --connect jdbc:mysql://plus111:3306/test --username root --password 123456 --table cs --export-dir /user/hive/warehouse/aa/part-m-00000 --input-fields-terminated-by ''

  • --export-dir导出hive的表在hdfs中的绝对路径
  • --input-fields-terminated-by 指定分隔符

这样导出的数据里面没有中文的话是没有任何问题的,但是如果数据里面有中文会出现下面的问题:

MySQL没有识别数据中的中文,从而显示的是“?”

分析产生的原因:这是由于MySQL的编码和hive的编码不统一造成的。

解决方法

  • 查看MySQL字符集

show variables like 'character%';

如果database和server都采用了latin1编码,不支持中文,我们需要把它修改为utf-8。

  • 修改全局字符

set character_set_connection=utf8;

set character_set_database=utf8;

set character_set_results=utf8;

set character_set_server=utf8;

set character_set_system=utf8;

set collation_connection=utf8;

set collation_database=utf8;

set collation_server=utf8;

  • 导出的命令换以下的命令,即可解决中文乱码!

sqoop-export --connect "jdbc:mysql://plus111:3306/test?useUnicode=true&characterEncoding=utf-8" --username root --password 123456 --table rr --export-dir /user/hive/warehouse/qq/test.txt --input-fields-terminated-by ''

35f00228630787085a896ff9622ce479.png

需要更多私信我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值