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()
相关推荐
WJX_KOI2 小时前
Open Notebook 一个开源的结合AI的记笔记软件
python
0思必得03 小时前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
2301_822382763 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
喵手4 小时前
Python爬虫实战:从零搭建字体库爬虫 - requests+lxml 实战采集字体网字体信息数据(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·采集字体库数据·字体库字体信息采集
2301_790300964 小时前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
程序员敲代码吗6 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
Yyyyy123jsjs6 小时前
如何通过免费的外汇API轻松获取实时汇率数据
开发语言·python
喵手6 小时前
Python爬虫实战:GovDataMiner —— 开放数据门户数据集元数据采集器(附 CSV 导出)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·open data·开放数据门户数据集列表
历程里程碑6 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django