错误现象
使用Hue 4.10(基于Python2.7构建)查询Hive,执行SQL中出现中文字符的时候报错。例如执行:
select * from student where name = '张三';
可能出现的错误类似如下。
错误1:
'ascii' codec can't encode characters in position 92-94: ordinal not in range(128)
错误2:
ParseException line 28:35 cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in expression specification
解决方案
需要提前检查的内容
检查Hue后端数据库(例如MySQL)的database和所有的数据表编码格式是否为UTF-8。
检查Hive本身执行中文查询是否存在问题(使用beeline执行同样的SQL),检查Hive metastore后端数据库(例如MySQL)的database和数据表编码格式是否为UTF-8。
如果上面的过程存在问题,需要事先解决。
方法1(无效)
在hue/build/env/lib/python2.7/site-packages
目录下创建sitecustomize.py
文件。增加如下内容:
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
保存后重启Hue。
经尝试后第一节的错误1消失,变为错误2,无法解决问题。
方法2(无效)
修改Hue配置文件Hue.ini
或pseudo_distributed.ini
。找到并修改如下配置。
# Default encoding for site data
default_site_encoding=utf-8
保存后重启Hue。
尝试后无效,错误依旧。
方法3(有效)
经社区调研,Hue4.10存在bug。需要修改hue/apps/beeswax/gen-py/TCLIService/ttypes.py
文件,找到3922
行,将oprot.writeString(self.statement)
修改为oprot.writeString(self.statement.encode('utf-8'))
。
保存后重启Hue。可以成功解决SQL问题。
需要注意的是,仅修改这一处虽然能够应付大多数使用场景,但仍有隐患,建议替换为Hue 4.11版本的ttypes.py
(GitHub:https://github.com/cloudera/hue/blob/release-4.11.0/apps/beeswax/gen-py/TCLIService/ttypes.py)。
方法4(有效)
最为彻底的解决方法为升级为Hue 4.11,4.11版本解决了中文以及其他非英文字符编码问题。
参考链接
https://cloud.tencent.com/developer/article/1800308
网友评论