在过去的几天里,我一直在绞尽脑汁,试图用上的pyhive连接到Hive服务器。我对Hive (pyhive也是如此)很陌生,但我是一个相当有经验的Python。我总是会犯以下错误:
(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'
执行下列脚本时:
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
现在,我只是尝试用上面的脚本连接到Hive,但最终我打算在一个SQLAlchemy应用程序中使用pyhive。换句话说:->烧瓶- SQLAlchemy -> SQLAlchemy -> pyhive。在生产中,Flask应用程序将由(即Linux的一些风格)托管,但将在Windows系统上开发(因此也必须运行)。
当然,我已经在Cloudera的网站上看过很多问题,还有关于Hive连接问题的GitHub,如果有人拿枪指着我的头,我不得不说,从Windows客户端尝试这个问题可能是问题的一部分,因为这似乎不是一件很常见的事情。
No mechanism available
这个错误到底意味着什么?如果有一些关于如何从python中配置和使用SASL的文档,那当然很好--如果有,我想知道。
导致错误的行在thrift_sasl/__init__.py
中。
ret, chosen_mech, initial_response = self.sasl.start(self.mechanism)
self.mechanism
是“平原”;chosen_mech
和initial_response
是空字符串(‘)。ret
为False,这将导致抛出异常。
我知道我不是唯一一个试图在Windows上用pyhive连接到Hive的人--这个家伙(SASL error when trying to connect to hive(hue) by python from my PC - Windows10)曾经是,但是他的“解决方案”--把Ubuntu作为一个VM安装在他的Windows上--对我来说是行不通的。
发布于 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应用程序中。我注射节俭运输的方式如下:
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断言host
、port
、auth
、kerberos_service_name
和password
除了None
之外没有任何其他值。不幸的是,如果没有提供端口号,SQLAlchemy将默认的Hive端口10000分配给port
。解决方案是替换HiveDialect.create_connect_args
方法,如下所示:https://github.com/devinstevenson/pure-transport/issues/7。简单地子类化HiveDialect类在这里是行不通的,因为名称HiveDialect
在SQLAlchemy的方言注册表中,不能简单地替换。
https://stackoverflow.com/questions/69487538
复制相似问题