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()
相关推荐
好学且牛逼的马1 小时前
从“混沌初开”到“有序统一”:Java集合框架发展历程与核心知识点详解
前端·数据库·python
a1117761 小时前
快速制作 虚拟形象项目 MotionPNGTuber
python·live2d
一切尽在,你来1 小时前
AI大模型应用开发前置知识:Python迭代器和生成器深入详解
python·langchain·ai编程
小雨中_2 小时前
2.7 强化学习分类
人工智能·python·深度学习·机器学习·分类·数据挖掘
摩拜芯城IC2 小时前
ATSHA204A‑STUCZ CryptoAuthentication 安全认证芯片IC
python·安全
小雨中_3 小时前
2.4 贝尔曼方程与蒙特卡洛方法
人工智能·python·深度学习·机器学习·自然语言处理
MediaTea4 小时前
Python:可迭代对象(对象语义角色)
开发语言·python
skywalk81634 小时前
Diffusers 库介绍,它支持LTX-2模型
python
一个处女座的程序猿O(∩_∩)O5 小时前
Python函数参数*args和**kwargs完全指南:从入门到精通
开发语言·python
好家伙VCC5 小时前
**发散创新:编译器优化实战——从LLVM IR到性能飞跃的奇妙旅程**
java·开发语言·python·算法