当前位置: 代码迷 >> python >> pyhive,sqlalchemy无法连接到hadoop沙箱
  详细解决方案

pyhive,sqlalchemy无法连接到hadoop沙箱

热度:21   发布时间:2023-06-13 15:19:50.0

我已经安装好了

pip install thrift
pip install PyHive
pip install thrift-sasl

由于pip install sasl失败,因此我下载了文件并将其安装在Windows 8.1 PC中。

然后我写了这段代码,

from pyhive import hive
cursor = hive.connect('192.168.1.232', port=10000, auth='NONE')
cursor.execute('SELECT * from sample_07 LIMIT 5',async=True)
print cursor.fetchall()

这给出了错误:

Traceback (most recent call last):
  File "C:/DigInEngine/scripts/UserManagementService/fd.py", line 37, in <module>
    cursor = hive.connect('192.168.1.232', port=10000, auth = 'NONE')
  File "C:\Python27\lib\site-packages\pyhive\hive.py", line 63, in connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\pyhive\hive.py", line 104, in __init__
    self._transport.open()
  File "C:\Python27\lib\site-packages\thrift_sasl\__init__.py", line 72, in open
    message=("Could not start SASL: %s" % self.sasl.getError()))
thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2

这段代码给出了

from sqlalchemy import create_engine
engine = create_engine('hive://192.168.1.232:10000/default')
try:
    connection = engine.connect()
except Exception, err:
    print err
result = connection.execute('select * from sample_07;')
engine.dispose()

这个错误,

无法启动SASL:sasl_client_start(-4)中的错误SASL(-4):无可用的机制:无法找到回调:2

我已从下载Hortonworks沙箱,并在单独的服务器中使用它。

注意:我也经历了 ,但是可接受的答案对我来说不起作用,因为从蜂巢导入ThriftHive会导致导入错误,尽管我已经安装了蜂巢。 所以我决定使用pyhive或sqlalchemy

如何连接到配置单元并轻松执行查询?

使用pyhive时,不能将身份验证作为auth="NOSASL"传递,而不是"None"传递,因此您的代码应如下所示:

from pyhive import hive
cursor = hive.connect('192.168.1.232', port=10000, auth='NOSASL')
cursor.execute('SELECT * from sample_07 LIMIT 5',async=True)
print cursor.fetchall()

以下是在Windows上构建SASL的步骤,但是您的工作量可能会有所不同:很多取决于您特定系统的路径和可用的库。

另请注意,这些说明特定于Python 2.7(我从问题路径中看到了这些说明)。

高层概述是您正在安装此项目: : 。 为此,您必须使用用于构建Python 2.7的旧版C ++编译器。 还有其他几个步骤可以使它起作用。

预先建立步骤:

  1. 安装 。 使用默认安装路径-请注意接下来2个步骤的安装位置(以下列表中包含2个选项)
  2. 复制到适合您安装的任何包含位置
  3. 从在相同的include目录中创建一个unistd.h文件

建立步骤:

  1. git clone https://github.com/cyrusimap/cyrus-sasl
  2. 从步骤1中打开与编译器一起安装的“ VS2013 x64本机工具命令提示符”
  3. 将目录更改为由步骤4创建的目录,然后更改为lib子目录
  4. nmake /f ntmakefile STATIC=no prefix=C:\\sasl64
  5. nmake /f ntmakefile prefix=C:\\sasl64 STATIC=no install 请参见下面的注释
  6. copy /BC:\\sasl64\\lib\\libsasl.lib /BC:\\sasl64\\lib\\sasl2.lib
  7. pip install thrift_sasl --global-option=build_ext \\ --global-option=-IC:\\\\sasl64\\\\include \\ --global-option=-LC:\\\\sasl64\\\\lib

“包含”位置:

  • “ C:\\ Program Files(x86)\\ Common Files \\ Microsoft \\ Visual C ++ for Python \\ 9.0 \\ VC \\ include \\ stdint.h”
  • “%USERPROFILE%\\ AppData \\ Local \\ Programs \\ Common \\ Microsoft \\ Visual C ++ for Python \\ 9.0 \\ VC \\ include”

这是对这些相同步骤的参考,并带有一些附加的注释和解释: : 。

注意 :所引用的指令还执行了includewin32\\include子目录中的步骤(8),您可能也必须这样做。