Win10下使用Python3连接Hive的方法

2019-06-23 13:12:18 来源: 晴天小雨 0

摘要:Windows下使用Python3连接Hive存在很多坑,通过踩坑后,最终成功连接了Hive,现在对踩坑的经验做一下记录,以便后续重新踩坑。

前言

第一次使用的连接方式为pyhive的形式,但安装完毕之后总是失败,有人说需要更改对应的Hive配置,但作为一个运维人员来说,不到万不得已不会去修改生产环境的一些配置(hive.server2.authentication改为NOSASL,然后在hive.connection()的参数中也加入auth='NOSASL'),也有人说sasl不支持windows版本,基于这两点,在Windows下建议使用impyla方式连接Hive。

Python连接Hive的方式有:

  1. ThriftHive
  2. pyhs2 driver(需开启hiveserver2服务)
  3. PyHive(Linux推荐)
  4. impyla(Windows推荐)

impyla安装过程

安装依赖

pip install bit_array

pip install thrift

pip install thriftpy

pip install pure_sasl

pip install --no-deps thrift-sasl==0.2.1

安装impyla

pip install impyla

连接Hive

# -*-coding:utf-8 -*-
from impala.dbapi import connect
conn = connect(host='ip', port=port, database='default', user='user_name', password='password', auth_mechanism="PLAIN")
cur = conn.cursor()
cur.execute('show tables')
print(cur.fetchall())

注意事项

1.重点不要安装sasl,否则会提示报错

卸载方式:pip uninstall sasl

2.在安装过程中,如果出现包安装失败的情况,可以下载whl包进行安装,下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/

安装方式:pip install 包的绝对路径

3.如果在安装过程中,出现任何包安装失败的问题,可以先将之前所有安装过的包统统卸载,再按顺序依次安装一次

4.Linux建议采用pyhive形式连接

sudo yum install cyrus-sasl-devel
sudo yum install gcc-c++

pip3 install sasl
pip3 install thrift
pip3 install thrift-sasl
pip3 install PyHive

问题集锦

impyla (0.14.0) ERROR - 'TSocket' object has no attribute 'isOpen'

这个问题的原因是thrift-sasl版本过高导致的,将其换成0.2.1的版本即可

pip install thrift-sasl==0.2.1

thriftpy2.protocol.exc.TProtocolException: TProtocolException(type=4)

这是由于auth_mechanism设置的问题导致的,加上或将其改为auth_mechanism="PLAIN"即可

TypeError: can’t concat str to bytes

修改 thrift-sasl init.py,在第94行之前加上以下语句即可:

if (type(body) is str):
    body = body.encode()

thrift.transport.TTransport.TTransportException: Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'

这是Windows下采用pyhive连接方式提出的错误,正如前言所述,可能需要修改对应的配置文件,也可能sasl根本就不支持Windows,建议改用impyla形式连接

thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'

修改thriftpy包下\parser\parser.py"中第488行代码,将"if url_scheme == '':" 修改为"if len(url_scheme) <=1:"即可

收藏
登录发表你的评论
0条评论