环境:Ubuntu 18.04 Python 3.6
安装
# coding=utf-8
from impala.dbapi import connect
conn = connect(host='127.0.0.1', port=10000, database='gld', auth_mechanism='PLAIN')
cursor = conn.cursor()
cursor.execute('SELECT * FROM student LIMIT 100')
print(cursor.description) # 打印字段名
results = cursor.fetchall() # 打印结果,results是list of tuples
print(results)
结果:
[('student.id', 'STRING', None, None, None, None, None), ('student.name', 'STRING', None, None, None, None, None), ('student.sex', 'STRING', None, None, None, None, None), ('student.age', 'STRING', None, None, None, None, None), ('student.dept_id', 'STRING', None, None, None, None, None)]
[('1', '孙悟空', '男', '18', '01'), ('2', '明世隐', '男', '19', '01'), ('3', '高渐离', '男', '20', '02'), ('4', '孙尚香', '女', '21', '02'), ('5', '安琪拉', '女', '22', '03')]
常见报错解决方案
-
安装sasl时报错:
sasl/saslwrapper.h:22:10: fatal error: sasl/sasl.h: No such file or directory
使用sudo apt-get install libsasl2-dev安装libsasl2-dev即可。 -
连接时报错:
AttributeError: 'TSocket' object has no attribute 'isOpen'
版本兼容问题,安装:pip3 install thrift_sasl==0.2.1,注意检查版本ply-3.11 thrift-0.9.3 sasl-0.2.1 six-1.12.0 thrift-sasl-0.2.1 thriftpy-0.3.9