徒手构建pyhive运行环境

一般生产环境都是无法联网的,那么如果需要使用python以及相关的一些模块,就需要先把安装包下载好,再进行离线安装。本文将介绍如何离线从0开始构建一个基于python3的pyhive运行环境。

系统版本

笔者使用的系统是CentOS Linux release 7.6.1810

安装Python3

本文使用Anaconda3-2020.02-Linux-x86_64.sh进行安装,不但会自动安装好python3还有pip以及pandas都会一起被安装,下载地址https://repo.anaconda.com/archive/

安装命令

sh Anaconda3-2020.02-Linux-x86_64.sh

安装过程中一路回车就可以,有需要输入的地方,输入yes

配置

root用户安装默认安装路径/root/anaconda3
安装完成以后配置/root/.bash_profile,PATH中加入anaconda3路径,如下所示

PATH=$PATH:$HOME/bin:/root/anaconda3/bin

执行source /root/.bash_profile使其生效,至此python3安装完毕,输入python3 -V验证版本为Python 3.7.6。再执行conda config --set auto_activate_base False可以去掉命令行前面出现的base

安装pyhive

下载安装包

pure-sasl-0.3.0.tar.gz 
PyHive-0.6.4.tar.gz
sasl-0.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
thrift-0.13.0.tar.gz
thrift_sasl-0.3.0.tar.gz

这些包可以在http://mirrors.aliyun.com/pypi/simple/或者https://pypi.tuna.tsinghua.edu.cn/simple/下载

部署过程

首先安装sasl,运行pip install sasl-0.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl;在依次安装thrift-0.13.0.tar.gzpure-sasl-0.3.0.tar.gzthrift_sasl-0.3.0.tar.gzPyHive-0.6.4.tar.gz。这些包的安装方法都是先解压,然后进入到各自的路径下执行python3 setup.py install,全部成功以后,就可以使用pyhive了。

验证

运行下面代码

from pyhive import hive
conn = hive.Connection(host='host001', port=10000, username='hive', database='test')

也许会有报错如下:

Traceback (most recent call last):
  File "", line 1, in 
  File "/root/python-source/PyHive-0.6.4/pyhive/hive.py", line 243, in __init__
    self._transport.open()
  File "/root/anaconda3/lib/python3.7/site-packages/thrift_sasl-0.3.0-py3.7.egg/thrift_sasl/__init__.py", line 79, in open
thrift.transport.TTransport.TTransportException: Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found'

需要再安装一些包,运行yum install cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5
至此就可以愉快的执行下面的代码了

from pyhive import hive
import pandas as pd

conn = hive.Connection(host='host001', port=10000, username='hive', database='test')
sql_str = "select * from user_info limit 2"
df = pd.read_sql(sql_str, conn)
print(df)

总结

在离线安装pyhive的过程中遇到最多的问题就是各种包之间的版本不匹配,笔者经过了多次的尝试,才找到了合适的版本,希望能对大家有所帮助。

你可能感兴趣的:(徒手构建pyhive运行环境)