我想使用pyhive将Python连接到hive。我正在使用下面的python脚本在本地执行。

#!/usr/bin/env python
# coding: utf-8
from pyhive import hive
from TCLIService.ttypes import TOperationState
def mysql_connect(host, port, username):
 conn = hive.Connection(host=host, port=port, username=username)
 return conn.cursor()

cursor = mysql_connect("localhost", 50070, "hduser")
cursor.execute("show databases")
print_log(cursor)

我将pyhive呈现到​​位置 /usr/local/lib/python2.7/dist-packages ,但是最终出现在以下输出中
vaibhav@vaibhav-Lenovo-G570:~/Desktop/Python/Automation$ ./pyhive_test.py
Traceback (most recent call last):
  File "./pyhive_test.py", line 9, in <module>
    cursor = mysql_connect("localhost", 50070, "hduser")
  File "./pyhive_test.py", line 6, in mysql_connect
    conn = hive.Connection(host=host, port=port, username=username)
  File "/usr/local/lib/python2.7/dist-packages/pyhive/hive.py", line 131, in __init__
    self._transport.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/__init__.py", line 80, in open
    status, payload = self._recv_sasl_message()
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/__init__.py", line 101, in _recv_sasl_message
    payload = read_all_compat(self._trans, length)
  File "/usr/local/lib/python2.7/dist-packages/thrift_sasl/six.py", line 31, in <lambda>
    read_all_compat = lambda trans, sz: trans.readAll(sz)
  File "/home/vaibhav/.local/lib/python2.7/site-packages/thrift/transport/TTransport.py", line 60, in readAll
    chunk = self.read(sz - have)
  File "/home/vaibhav/.local/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 132, in read
    message='TSocket read 0 bytes')
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

编辑
1.文件名从Pyhive更改为pyhive_test
  • pyhive.py已从目录
  • 中删除

    尝试了可能的解决方案:
    1.安装了两个版本的python2.7和python 3.4。一世
    卸载了Python3.4,但该文件夹似乎仍然存在于
    / usr / local / lib /。我在下面运行了一些命令,以检查python的安装位置以及PYTHONPATH中可用的软件包
    vaibhav@vaibhav-Lenovo-G570:~$ which -a python
    /usr/bin/python
    vaibhav@vaibhav-Lenovo-G570:~$ python -c "import sys, pprint; pprint.pprint(sys.path)"
    ['',
     '/home/vaibhav',
     '/usr/lib/python2.7/dist-packages',
     '/usr/lib/python2.7',
     '/usr/lib/python2.7/plat-x86_64-linux-gnu',
     '/usr/lib/python2.7/lib-tk',
     '/usr/lib/python2.7/lib-old',
     '/usr/lib/python2.7/lib-dynload',
     '/home/vaibhav/.local/lib/python2.7/site-packages',
     '/usr/local/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/dist-packages/PILcompat',
     '/usr/lib/python2.7/dist-packages/gtk-2.0',
     '/usr/lib/pymodules/python2.7',
     '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
    

    2.从提到的here链接中获得参考,他们提到了在虚拟环境中使用它或使用干净的 python 。没有使用它们的任何方式,也不知道它将如何影响已经存在的配置。

    3.我使用sudo安装了Pyhive,所以我在此link之后更改了权限,但仍然遇到相同的问题。

    最佳答案

    您正在启动的文件名为pyhive.py
    当你做

    from pyhive import hive
    
    pyhive.py内部,然后它将尝试从模块的(而不是hive库)中导入pyhive
    请命名您要启动的文件,并避免使用现有模块/库的名称。
    docs:

    关于python - Python Pyhive模块无法导入名称配置单元,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44744845/

    10-10 14:38