当前位置:   article > 正文

Python 3 使用Hive 总结_python hive

python hive

启动HiveServer2 服务

HiveServer2 是一种可选的 Hive 内置服务,可以允许远程客户端使用不同编程语言向 Hive 提交请求并返回结果。

Thrift服务配置

假设我们已经成功安装了 Hive,如果没有安装,请参考:Hive 一文读懂 。在启动 HiveServer2 之前,我们需要先进行一些配置:

配置项

默认值

说明

hive.server2.transport.mode

binary

HiveServer2 的传输模式,binary或者http

hive.server2.thrift.port

10000

HiveServer2 传输模式设置为 binary 时,Thrift 接口的端口号

hive.server2.thrift.http.port

10001

HiveServer2 传输模式设置为 http 时,Thrift 接口的端口号

hive.server2.thrift.bind.host

localhost

Thrift服务绑定的主机

hive.server2.thrift.min.worker.threads

5

Thrift最小工作线程数

hive.server2.thrift.max.worker.threads

500

Thrift最大工作线程数

hive.server2.authentication

NONE

客户端认证类型,NONE、LDAP、KERBEROS、CUSTOM、PAM、NOSASL

hive.server2.thrift.client.user

anonymous

Thrift 客户端用户名

hive.server2.thrift.client.password

anonymous

Thrift 客户端密码

启动HiveServer2 服务 

方式一:$HIVE_HOME/bin/hiveserver2 

  1. [root@Hadoop3-master bin]# hiveserver2
  2. 2023-08-16 23:14:00: Starting HiveServer2
  3. SLF4J: Class path contains multiple SLF4J bindings.
  4. SLF4J: Found binding in [jar:file:/usr/local/hive/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  5. SLF4J: Found binding in [jar:file:/usr/local/hadoop/share/hadoop/common/lib/slf4j-reload4j-1.7.35.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  6. SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
  7. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
  8. Hive Session ID = 0ba8eb07-5f63-43a1-aa4d-61954f6e244f

方式二:$HIVE_HOME/bin/hive --service hiveserver2

检查 HiveServer2 是否启动成功

netstat -nl | grep 10000

启动 hiveserver2 ,访问Hive 管理平台

默认访问地址:http://192.168.43.11:10002/

效果截图:

Python 连接Hive

依赖的第三方库包 

  1. pip install sasl
  2. pip install thrift
  3. pip install thrift-sasl
  4. pip install PyHive

温馨提示:首先是pyhive的安装:pyhive这个包依 赖于sasl,thrift,thrift-sasl这三个包。

安装Sasl 库表包遇到的问题 

造成错误的原因是:

  1. saslwrapper.cpp
  2. C:\Users\zzg\AppData\Local\Temp\pip-install-1vw7hyr4\sasl_05859569d9c14648abbe3a8901ed3627\sasl\saslwrapper.h(22): fatal error C1083: 无法打开包括文件: “sasl/sasl.h”: No such file or directory

 saslwrapper.cpp 文件中无法找到sasl/sasl.h 头文件。

Google 和百度的解决办法

通过加利福利亚大学的的镜像地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl ,下载sasl.whl 文件。情况:现在的情况是网站已经被关闭。

通过清华大学的镜像地址:https://pypi.tuna.tsinghua.edu.cn/simple/sasl/ ,下载sasl.whl  文件。情况:没有Python-3.10 且支持windows 64 架构的库包。

 温馨提示:清华的镜像地址提供关于sasl.whl 内容主要包含:

  • 支持python-3.5.0 至python-3.9.0 版本且系统架构仅支持Linux 架构。
  • 提供sasl第三方库源码:支持0.1.1 至0.3.1

编译Sasl-0.3.1 源码,生成Sasl.whl 文件

通过清华镜像下载Sasl 源码,解压后的效果截图:

切换至Sasl  源码,执行指令:python setup.py bdist_wheel 

源码编译的错误与pip 安装sasl 库一样。

借鉴其他安装Sasl成功 

环境说明:

python版本为python 3.10

cp310:表示为python的版本,为python 3 10的

win_amd64:表示为驱动为windows 64位的驱动

对应sasl.whl 包 = sasl-0.3.1-cp310-cp310-win_amd64.whl

执行如下指令:

pip install  sasl-0.3.1-cp310-cp310-win_amd64.whl

安装thrift

pip install thrift

安装thrift_sasl

pip install thrift_sasl

 安装pyHive

pip install pyhive

Python 连接Hive 代码 

  1. from pyhive import hive
  2. # 读取数据
  3. def select_pyhive(sql):
  4. # 创建hive连接
  5. conn = hive.Connection(host='192.168.43.11', port=10000, username='默认', database='user')
  6. cur = conn.cursor()
  7. try:
  8. #c = cur.fetchall()
  9. df = pd.read_sql(sql, conn)
  10. return df
  11. finally:
  12. if conn:
  13. conn.close()
  14. sql = "show databases"
  15. df = select_pyhive(sql)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/855533
推荐阅读
相关标签
  

闽ICP备14008679号