使用Impyla连接Hive

本文介绍如何在Ubuntu环境下使用Python3.6连接Impala数据库,并执行数据查询。通过实例展示了如何建立连接、执行SQL语句及获取查询结果。同时,针对常见错误如sasl库缺失和版本兼容问题提供了详细的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境: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')]

常见报错解决方案

  1. 安装sasl时报错:sasl/saslwrapper.h:22:10: fatal error: sasl/sasl.h: No such file or directory
    使用sudo apt-get install libsasl2-dev安装libsasl2-dev即可。

  2. 连接时报错: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手撕机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值