操作代码
虚拟机开启相关进程,hadoop、hiveserver2
操作代码很简单如下所示
注意先不要运行,这里缺少相关需求包组件
from pyhive import hive
conn = hive.Connection(host='192.168.56.21', port=10000, username='root',password='ok', database='mydemo',auth='LDAP')
cursor = conn.cursor()
cursor.execute('select * from scores')
for result in cursor.fetchall():
print(result)
注:
使用pycharm连接hive,运行相关需求包如sasl很难下载,windows环境下载比较复杂,所以我们可以使用linux下安装的juypter notebook进行相关连接操作
下载需求包
linux执行如下指令下载相关需求包
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive
安装sasl踩坑
linux虚拟机环境安装sasl时会报错,如下
ERROR: Command errored out with exit status 1:
command: /home/hadoop/.virtualenvs/phone_location_info/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-o5ypiowe/sasl/setup.py'"'"'; __file__='"'"'/tmp/pip-install-o5ypiowe/sasl/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-algodbuc
cwd: /tmp/pip-install-o5ypiowe/sasl/
解决方案如下:
1. 切换到root目录下执行如下命令:
yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64
2. 此时就可以下载相关需求包了
pip install sasl
3. 下载完相关进程需要重启
执行连接成功
执行相关代码,连接成功,如下所示,可以对hive进行相关操作