python连接hive报错:TypeError: can‘t concat str to bytes

目录

一、完整报错

二、解决

[三、 其他报错](#三、 其他报错)

四、impala方式连接hive


或者直接使用 pip install pyhive[hive] 安装。需要先 pip uninstall pyhive。

一、完整报错

Traceback (most recent call last):

File "D:/Gitlab/my_world/hive2csv.py", line 18, in <module>

conn = hive.Connection(host=host, port=port, username=username, password=password, database=database, auth=auth)

File "D:\Anaconda3\lib\site-packages\pyhive\hive.py", line 269, in init

self._transport.open()

File "D:\Anaconda3\lib\site-packages\thrift_sasl\init.py", line 82, in open

self._send_message(self.START, chosen_mech)

File "D:\Anaconda3\lib\site-packages\thrift_sasl\init.py", line 103, in _send_message

self._trans.write(header + body)

TypeError: can't concat str to bytes

二、解决

2.1、进入D:\Anaconda3\lib\site-packages\thrift_sasl\init.py文件

2.2、添加如下代码到第101行之前

python 复制代码
if (type(body) is str):
    body = body.encode()

2.2,修改后情况

然后就可以运行了。

三、 其他报错

报错:AttributeError: 'TSaslClientTransport' object has no attribute 'readAll'

解决:pip install thrift_sasl==0.3.0

其他报错可以参考文章:python连接hive

四、impala方式连接hive

4.1、依赖包

python 复制代码
pip install six bit_array thrift thrift_sasl sqlalchemy impyla -i https://pypi.tuna.tsinghua.edu.cn/simple

4.2、代码

python 复制代码
from impala.dbapi import connect

conn = connect(host=host,port=port,user=username,password=password,auth_mechanism=auth,database=database)
def get_sql_result(sql):
    # 鎵ц鏌ヨ骞惰幏鍙栫粨鏋?    cursor = conn.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    columns = [desc[0] for desc in cursor.description]

    return result,columns


if __name__ == '__main__':

    hive_sql = 'show databases'
    result,columns = get_sql_result(hive_sql)

    df = pd.DataFrame(result,columns=columns)
相关推荐
白日与明月13 小时前
Hive 大表 Join 优化:从策略选择到倾斜处理
数据仓库·hive·hadoop
理智.62913 小时前
Cursor 中使用 Conda 虚拟环境常见问题与彻底解决方案(踩坑实录)
python·conda
aiguangyuan13 小时前
中文分词与文本分析实战指南
人工智能·python·nlp
小二·13 小时前
Python Web 开发进阶实战:量子机器学习实验平台 —— 在 Flask + Vue 中集成 Qiskit 构建混合量子-经典 AI 应用
前端·人工智能·python
one____dream13 小时前
【网安】pwn-ret2shellcode
python·安全·网络安全·网络攻击模型
给你一页白纸13 小时前
将分散的Pytest测试脚本统一接入测试平台:FastAPI改造方案详解
python·pytest·接口自动化·测试平台
孤狼warrior14 小时前
图像生成 Stable Diffusion模型架构介绍及使用代码 附数据集批量获取
人工智能·python·深度学习·stable diffusion·cnn·transformer·stablediffusion
Java程序员威哥14 小时前
SpringBoot2.x与3.x自动配置注册差异深度解析:从原理到迁移实战
java·大数据·开发语言·hive·hadoop·spring boot·后端
大哥手下留情14 小时前
Python火车票查询方法介绍
开发语言·python
努力毕业的小土博^_^14 小时前
【AI课程领学】第十二课 · 超参数设定与网络训练(课时1) 网络超参数设定:从“要调什么”到“怎么系统地调”(含 PyTorch 可复用模板)
人工智能·pytorch·python·深度学习·神经网络·机器学习