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()
相关推荐
itwangyang52014 小时前
AIDD-人工智能药物设计-字节跳动 PXDesign:AI 设计蛋白,82% 命中率惊艳业界
人工智能·python
Biehmltym14 小时前
【AI】01开发环境:Conda_python包/环境管理,10分钟上手
开发语言·python·conda
袁气满满~_~14 小时前
Python练习
开发语言·python
我可以将你更新哟15 小时前
【爬虫】爬取斗罗大陆漫画,面向对象封装(存入数据库)
数据库·爬虫·python
麦麦大数据15 小时前
F060 基于BERTvue+flask电影评论情感分析系统
后端·python·flask·bert·推荐算法·情感分析·电影评论
yongche_shi15 小时前
第八十九篇:CAP理论、BASE理论在系统设计中的应用
开发语言·python·面试宝典·cap理论·base理论
小智RE0-走在路上15 小时前
Python学习笔记(13) --Mysql,Python关联数据库
数据库·python·学习
YJlio15 小时前
杨利杰YJlio|博客导航目录(专栏总览 + 推荐阅读路线)
开发语言·python·pdf
Swizard15 小时前
数据不够代码凑?用 Albumentations 让你的 AI 模型“看”得更广,训练快 10 倍!
python·算法·ai·训练
智算菩萨15 小时前
【Python机器学习】决策树与随机森林:解释性与鲁棒性的平衡
python·决策树·机器学习