首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过Windows上的pyhive连接蜂箱

如何通过Windows上的pyhive连接蜂箱
EN

Stack Overflow用户
提问于 2021-10-07 20:34:05
回答 1查看 1K关注 0票数 0

在过去的几天里,我一直在绞尽脑汁,试图用上的pyhive连接到Hive服务器。我对Hive (pyhive也是如此)很陌生,但我是一个相当有经验的Python。我总是会犯以下错误:

代码语言:javascript
运行
复制
(pyhive-test) C:\dev\sandbox\pyhive-test>python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    conn = hive.Connection(host='192.168.1.196', port='10000', database='default', auth='NONE')
  File "C:\Users\harnerd\Anaconda3\envs\pyhive-test\lib\site-packages\pyhive\hive.py", line 192, in __init__
    self._transport.open()
  File "C:\Users\harnerd\Anaconda3\envs\pyhive-test\lib\site-packages\thrift_sasl\__init__.py", line 84, in open
    raise TTransportException(type=TTransportException.NOT_OPEN,
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'

执行下列脚本时:

代码语言:javascript
运行
复制
from pyhive import hive

conn = hive.Connection(host='192.168.1.196', port='10000', database='default', auth='NONE')
cur = conn.cursor()
cur.execute('show tables')
data = cur.fetchall()
print(data)

HiveServer2实例是来自Cloudera的开箱即用的HDP沙箱VM,HiveServer2身份验证设置为'None‘。

Client是Windows 10上的Anaconda虚拟环境,带有Python3.8.5和conda安装的以下软件包:

0.13.0

  • thrift-sasl
  • pyhive 0.6.1
  • sasl 0.2.1
  • thrift
  • 0.4.2

现在,我只是尝试用上面的脚本连接到Hive,但最终我打算在一个SQLAlchemy应用程序中使用pyhive。换句话说:->烧瓶- SQLAlchemy -> SQLAlchemy -> pyhive。在生产中,Flask应用程序将由(即Linux的一些风格)托管,但将在Windows系统上开发(因此也必须运行)。

当然,我已经在Cloudera的网站上看过很多问题,还有关于Hive连接问题的GitHub,如果有人拿枪指着我的头,我不得不说,从Windows客户端尝试这个问题可能是问题的一部分,因为这似乎不是一件很常见的事情。

代码语言:javascript
运行
复制
No mechanism available

这个错误到底意味着什么?如果有一些关于如何从python中配置和使用SASL的文档,那当然很好--如果有,我想知道。

导致错误的行在thrift_sasl/__init__.py中。

代码语言:javascript
运行
复制
ret, chosen_mech, initial_response = self.sasl.start(self.mechanism)

self.mechanism是“平原”;chosen_mechinitial_response是空字符串(‘)。ret为False,这将导致抛出异常。

我知道我不是唯一一个试图在Windows上用pyhive连接到Hive的人--这个家伙(SASL error when trying to connect to hive(hue) by python from my PC - Windows10)曾经是,但是他的“解决方案”--把Ubuntu作为一个VM安装在他的Windows上--对我来说是行不通的。

EN

Stack Overflow用户

发布于 2021-10-14 13:40:30

长话短说,这个问题的答案是PyHive在Windows上根本不受支持。这是因为PyHive使用sasl库进行Hive连接,而sasl不仅很难从Windows上的源代码编译,而且它似乎根本无法在Windows上工作。

关键是提供您自己的thrift_transport,而不是依赖PyHive来创建它。Devin提供了一种替代传输(https://github.com/devinstevenson/pure-transport),它在Windows上运行良好,并且应该在其他OSes上工作(不过,我还没有对此进行测试)。他的回购提供了示例,直接使用Hive和SQLAlchemy使用纯传输。

在我的用例中,我将它与Flask-SQLAlchemy一起使用在一个Flask应用程序中。我注射节俭运输的方式如下:

代码语言:javascript
运行
复制
from flask_sqlalchemy import SQLAlchemy
import puretransport

thrift_transport = puretransport.transport_factory(host='127.0.0.1',
                                                   port=10000,
                                                   username='a_user',
                                                   password='a_password')


class MySQLAlchemy(SQLAlchemy):
    '''
    Subclassing the standard SQLAlchemy class so we can inject our own thrift
    transport which is needed to get pyhive to work on Windows
    '''
    def apply_driver_hacks(self, app, sa_url, options):
        '''
        If the current driver is for Hive, add our thrift transport
        '''
        if sa_url.drivername.startswith('hive'):
            if 'connect_args' not in options:
                options['connect_args'] = {'thrift_transport': thrift_transport}
        return super(MySQLAlchemy, self).apply_driver_hacks(app, sa_url, options)

# later, in models.py...
db = MySQLAlchemy()

class AModelClass(db.Model):
    __tablename__ = 'some_table'
    id = db.Column(db.Integer, primary_key=True)
    # etc...

在我的例子中,我为我的Hive连接使用的URL只是表单hive:///{database_name},即:hive:///customers,因为所有必要的信息都是使用节俭传输传递的。不过,有一点要注意--当注入节俭传输时,PyHive断言hostportauthkerberos_service_namepassword除了None之外没有任何其他值。不幸的是,如果没有提供端口号,SQLAlchemy将默认的Hive端口10000分配给port。解决方案是替换HiveDialect.create_connect_args方法,如下所示:https://github.com/devinstevenson/pure-transport/issues/7。简单地子类化HiveDialect类在这里是行不通的,因为名称HiveDialect在SQLAlchemy的方言注册表中,不能简单地替换。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69487538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档