Python操作Hive数据仓库

Python连接Hive

1、Python如何连接Hive?

Python连接Hive需要使用Impala查询引擎

由于Hadoop集群节点间使用RPC通信,所以需要配置Thrift依赖环境

Thrift是一个轻量级、跨语言的RPC框架,主要用于服务间的RPC通信。由Facebook于2007年开发,2008年进入Apache开源项目

sasl模块是Python中用于实现SASL(Simple Authentication and Security Layer)认证的第三方库,提供了对各种SASL机制的支持,例如与Kafka、Hadoop等进行安全通信

经过验证,以下模块都是Python连接Hive的环境依赖:

shell 复制代码
pip install bitarray
pip install bit_array
pip install thrift
pip install thriftpy
pip install pure_sasl
pip install --no-deps thrift-sasl==0.2.1

安装Impyla模块:

shell 复制代码
pip install impyla

如果安装Impyla报错:

shell 复制代码
ERROR: Failed building wheel for impyla

则需要下载对应的whl文件安装:

Python扩展包whl文件下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/

Ctrl+F查找需要的whl文件,点击下载对应版本

安装:

shell 复制代码
pip install whl文件绝对路径

2、Python连接Hive数据仓库

1) 启动Hadoop集群

2) 使用hiveserver2开启Hive的JDBC服务

3) 基本使用

python 复制代码
from impala.dbapi import connect
from impala.util import as_pandas

# 连接Hive
conn = connect(host='bd91', port=10000, auth_mechanism='PLAIN', user="root", password="123456", database="default")
# 创建游标
cursor = conn.cursor()
# 执行查询
cursor.execute("select * from stu")
# 结果转换为DataFrame
df = as_pandas(cursor)
print(df.to_string())
# 关闭连接
cursor.close()
conn.close()
相关推荐
SEO_juper7 分钟前
外贸多语言站最隐蔽的流量杀手:hreflang 错了,谷歌把德语页推给美国人(附审计脚本)
开发语言·前端·python·seo·独立站·谷歌优化
Metaphor69220 分钟前
快速合并 Word 文档【Python 指南】
python·word
JAVA老架构AI智能化39 分钟前
如何高质量分块
人工智能·python
泡海椒40 分钟前
告别反射低效:JQuick-Java ASM动态调用链性能优化实战
java·python·性能优化
2601_956319881 小时前
用示例和练习,把量化规则先练清楚
人工智能·python
满怀冰雪1 小时前
25-迁移学习入门:加载预训练模型并微调
人工智能·python·机器学习·迁移学习·paddle
2601_962284501 小时前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_962078031 小时前
构建RESTful APIs:使用Python和Flask
python·flask·web开发·api设计·构建restfulapis
半亩码田1 小时前
C#转Python第4.5篇:单元测试:pytest vs xUnit/NUnit
python·单元测试·c#
2601_962300811 小时前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests