Skip to main content
 首页 » 编程设计

Python 配置单元 : thrift. transport.TTransport.TTransportException: 无

2024年10月25日40freeliver54

假设 Hive 安装在“g”集群中。我无权访问 Gold Cluster。我正在“s”集群中进行 python 开发工作。我可以从“s”集群访问 Hive 并运行查询。

我有以下代码可以从在“s”集群中运行的 Python 脚本连接到 Hive。

some_table 是 Hive 中已经存在的表。我想执行一个简单的 select * from some_table 命令来获得一些结果。

import sys 
sys.path.append("/usr/lib/hive/lib/py") 
 
from hive_service import ThriftHive 
from hive_service.ttypes import HiveServerException 
from thrift import Thrift 
from thrift.transport import TSocket 
from thrift.transport import TTransport 
from thrift.protocol import TBinaryProtocol 
 
 
transport = TSocket.TSocket('what-ever-server', what-ever-port) 
transport = TTransport.TBufferedTransport(transport) 
protocol = TBinaryProtocol.TBinaryProtocol(transport) 
 
client = ThriftHive.Client(protocol) 
transport.open() 
print "connect success" 
client.execute("SELECT * FROM some_table") 
print client.fetchAll() 
print "executed" 

但是在打印“connect success”后我得到以下错误。我假设连接成功。

Traceback (most recent call last): 
   File "hiveConnect.py", line 30, in <module> 
      row = client.execute("SELECT * FROM some_table") 
   File "/usr/lib/hive/lib/py/hive_service/ThriftHive.py", line 68, in execute 
      self.recv_execute() 
   File "/usr/lib/hive/lib/py/hive_service/ThriftHive.py", line 79, in recv_execute 
       (fname, mtype, rseqid) = self._iprot.readMessageBegin() 
   File "/usr/lib/hive/lib/py/thrift/protocol/TBinaryProtocol.py", line 137, in readMessageBegin 
       name = self.trans.readAll(sz) 
   File "/usr/lib/hive/lib/py/thrift/transport/TTransport.py", line 58, in readAll 
        chunk = self.read(sz-have) 
   File "/usr/lib/hive/lib/py/thrift/transport/TTransport.py", line 155, in read 
        self.__rbuf = StringIO(self.__trans.read(max(sz, self.DEFAULT_BUFFER))) 
   File "/usr/lib/hive/lib/py/thrift/transport/TSocket.py", line 94, in read 
        raise TTransportException('TSocket read 0 bytes') 
thrift.transport.TTransport.TTransportException: None 

我在这段代码中做错了什么?使用服务器名称和端口连接到 Hive 时我没有遇到任何错误,所以我假设一切都很好并且与 Hive 的连接不是问题。

请您参考如下方法:

经过更多的研究,我发现实际上是 HiveServer2 正在使用端口号 10,000。之后我必须安装 pyhs2 才能正常工作。