美文网首页Java玩转大数据
Hue 4.10 查询Hive执行存在中文的SQL出错问题解决

Hue 4.10 查询Hive执行存在中文的SQL出错问题解决

作者: AlienPaul | 来源:发表于2024-02-03 09:15 被阅读0次

错误现象

使用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.inipseudo_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

https://github.com/cloudera/hue/issues/2606

https://github.com/cloudera/hue/issues/470

相关文章

网友评论

    本文标题:Hue 4.10 查询Hive执行存在中文的SQL出错问题解决

    本文链接:https://www.haomeiwen.com/subject/xrqxadtx.html