内容纲要
概要说明
本案例用于实现 python3 中通过 impyla 连接 Inceptor 数据库。
此方法是除了官方推荐的 ODBC 方式连接 Inceptor 外,还可以通过 Impyla 方式连接 Inceptor。
详细说明
1. 确认环境的 python 版本为 3.3+:
python -V
2. 安装相应的依赖包:
pip install pure-sasl==0.5.1
pip install thrift_sasl==0.2.1 --no-deps
pip install thrift==0.9.3
pip install bitarray==0.8.3
pip install cython thriftpy==0.3.9
pip install impyla
推荐安装上述版本的组件,否则可能会有兼容性问题
3. 编写python测试脚本:
from impala.dbapi import connect
from impala.util import as_pandas
conn = connect(host='172.22.17.1',port=10000,database='dys',auth_mechanism='PLAIN',user='hive',password='123456')
cur=conn.cursor()
cur.execute('show tables')
df = as_pandas(cur)
print(df)
注意其中的 auth_mechanism 需要和 hive-site.xml 中的 hive.server2.authentication
保持一致:
4. 执行脚本验证连接是否成功:
chmod u+x ${脚本名称}
python ${脚本名称}
FAQ
1. 安装 thrift_sasl 遇到报错:sasl/saslwrapper.h:22:23: fatal error: sasl/sasl.h: No such file or director
可以通过pip install thrift_sasl==0.2.1 --no-deps
方式来去掉依赖包的安装
2. 安装完成后,执行脚本报错:TypeError: can’t concat bytes to str
根据报错具体信息,定位到/usr/local/lib/python3.5/site-packages/thrift_sasl/__init__.py
文件中,修改:
header = struct.pack(">BI", status, len(body))
self._trans.write(header + body)
为:
header = struct.pack(">BI", status, len(body))
if(type(body) is str):
body = body.encode()
self._trans.write(header + body)
3. 进行安全验证的时候报错:impala.error.NotSupportedError: Unsupported authentication mechanism: NONE
检查 Inceptor 的 hive-site.xml 中的 hive.server2.authentication
的值,在脚本中保持认证方式的一致