Python连接Hadoop/Impala实战:解决语法错误、依赖冲突与SASL异常的完整指南
本文汇总了使用Python通过impyla连接Hadoop Impala时遇到的常见错误及解决方案。涵盖Python 3.7中async关键字冲突、thrift-sasl版本兼容性、SASL机制缺失以及字符串与字节类型转换问题。提供了具体的代码修改建议、依赖包版本推荐及Conda安装策略,帮助开发者快速打通数据链路。
Python连接Hadoop背景与目标
近期计划采用Python结合Hadoop与Pandas开展深度数据分析及机器学习工作(后续可能转向Python+Spark+Hadoop架构,此处暂不展开)。首要任务是实现Python与Hadoop的连通。原以为此举简单,实则遭遇诸多阻碍,耗时半日方得解决。鉴于网络上针对该问题的有效解答稀缺,现将Python连接Hadoop过程中遇到的各类坑点及对策进行汇总分享,旨在帮助同行避开弯路,节省时间。
注:本文所述解决方案参考了大量网络资料,部分关键线索源自GitHub某帖子的角落说明,若忽略此类细节极易出错。因参考来源繁杂,此处不再逐一列出。
首先明确组件选型:本文基于impala配合Python 3.7环境连接Hadoop数据库。若您的技术栈与此不符,建议跳过本文以免浪费时间。
基础连接代码与语法错误排查
基础执行代码如下:
import impala.dbapi as ipdb
conn = ipdb.connect(host="192.168.XX.XXX",port=10000,user="xxx",password="xxxxxx",database="xxx",auth_mechanism='PLAIN')
cursor = conn.cursor()
#其中xxxx是表名,为了不涉及到公司的信息,我把表名隐藏掉了,大家自己换成自己数据库表名
cursor.execute('select * From xxxx')
print(cursor.description) # prints the result set's schema
for rowData in cursor.fetchall():
print(rowData)
conn.close()
坑一:提示语法错误
现象描述:
/Users/wangxxin/miniconda3/bin/python3.7 /Users/wangxxin/Documents/Python/PythonDataAnalyze/project/knDt/pyHiveTest.py Traceback (most recent call last): File "/Users/wangxxin/Documents/Python/PythonDataAnalyze/project/knDt/pyHiveTest.py", line 1, in <module> import impala.dbapi as ipdb File "/Users/wangxxin/miniconda3/lib/python3.7/site-packages/impala/dbapi.py", line 28, in <module> import impala.hiveserver2 as hs2 File "/Users/wangxxin/miniconda3/lib/python3.7/site-packages/impala/hiveserver2.py", line 340 async=True)
解决方法:将代码中所有参数async统一修改为async_(需保持上下文一致且非Python关键字)。原因在于Python 3.0起已将async列为保留关键字,继续使用async作为参数会引发语法错误。需修改的位置包括:
#hiveserver2.py文件338行左右
op = self.session.execute(self._last_operation_string,
configuration,
async_=True)
#hiveserver2.py文件1022行左右
def execute(self, statement, configuration=None, async_=False):
req = TExecuteStatementReq(sessionHandle=self.handle,
statement=statement,
confOverlay=configuration,
runAsync=async_)
Parser.py文件修正与协议异常处理
坑二:提供的Parser.py文件存在问题,加载时抛出异常
解决方法:
#根据网上的意见对原代码进行调整
elif url_scheme in ('c', 'd', 'e', 'f'):
with open(path) as fh:
data = fh.read()
elif url_scheme in ('http', 'https'):
data = urlopen(path).read()
else:
raise ThriftParserError('ThriftPy does not support generating module '
'with path in protocol \'{}\''.format(
url_scheme))
建议直接修正上述坑一与坑二,这两项调整是连接成功的必要前提。
解决前两个问题后继续运行,仍会遭遇如下错误:
TProtocolException: TProtocolException(type=4)
解决方法:
原因在于connect方法中未指定auth_mechanism参数。修改后的代码如下:
import impala.dbapi as ipdb
conn = ipdb.connect(host="192.168.XX.XXX",port=10000,user="xxx",password="xxxxxx",database="xxx",auth_mechanism='PLAIN')`
依赖版本冲突与SASL机制解决
坑四:解决上述问题后,程序继续报错:
AttributeError: 'TSocket' object has no attribute 'isOpen'
解决方法:
该错误源于thrift-sasl版本过高(如0.3.0)。需将其降级至0.2.1版本。
pip uninstall thrift-sasl
pip install thrift-sasl==0.2.1
坑五:处理完上述问题后,运行继续报错(此时解决过程令人崩溃,但请坚持,已接近成功):
thriftpy.transport.TTransportException: TTransportException(type=1, message="Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'")
解决方法:此问题最为棘手,且网上解决方案较少。
I solved the issue, had to uninstall the package SASL and install PURE-SASL, when impyla can´t find the sasl package it works with pure-sasl and then everything goes well.
根本原因在于sasl与pure-sasl存在冲突。直接卸载sasl包即可解决。
pip uninstall SASL
类型转换错误与最终验证
坑六:执行完成后,仍可能报错:
TypeError: can't concat str to bytes
定位至错误发生的最后一行,具体位于init.py文件的第94行(代码中高亮部分)。
header = struct.pack(">BI", status, len(body))
#按照网上的提供的办法增加对BODY的处理
if (type(body) is str):
body = body.encode()
self._trans.write(header + body)
self._trans.flush()
经过上述步骤,应能成功连接Hive库并查询数据。
依赖包管理与安装建议
最后总结:连接Hadoop数据库涉及的依赖包繁多,请务必仔细核对依赖关系(最好保持依赖包版本一致,不多不少,仅保留相关包),这可避免大量潜在问题。
| 序号 | 包名 | 版本号 | 安装命令行 |
|---|---|---|---|
| 1 | pure_sasl | 0.5.1 | pip install pure_sasl==0.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
| 2 | thrift | 0.9.3 | pip install thrift==0.9.3 -i https://pypi.tuna.tsinghua.edu.cn/simple |
| 3 | bitarray | 0.8.3 | pip install bitarray==0.8.3 -i https://pypi.tuna.tsinghua.edu.cn/simple |
| 4 | thrift_sasl | 0.2.1 | pip install thrift_sasl==0.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
| 5 | thriftpy | 0.3.9 | pip install thriftpy==0.3.9 -i https://pypi.tuna.tsinghua.edu.cn/simple |
| 6 | impyla | 0.14.1 | pip install impyla==0.14.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
建议按顺序安装。此前因依赖包问题受阻,最终通过conda安装解决。若在安装thriftpy、thrift_sasl、impyla时报错,可利用conda自动下载依赖包。以下提供具体命令供无conda环境者参考:
| package | build | size |
|---|---|---|
| ply-3.11 | py37_0 | 80 KB |
| conda-4.6.1 | py37_0 | 1.7 MB |
| thriftpy-0.3.9 | py37h1de35cc_2 | 171 KB |
祝您好运!若在实际操作中仍遇其他问题,欢迎留言交流。
SQL语句注意事项
最后提示一点:
SQL语句中请勿包含分号,否则将导致报错。此非环境问题,具体报错如下:
impala.error.HiveServer2Error: Error while compiling statement: FAILED: ParseException line 2:83 cannot recogniz
至此,关于Python连接Hadoop数据库遇到的各类坑点汇总介绍完毕。更多Python连接Hadoop相关内容,请搜索本站过往文章或浏览相关文章,感谢支持!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









