2017-02-28 12 views
0

私はhbaseからデータを読み込むためのpythonスクリプトを書いています。しかし、ものはthrift生成されたファイルで間違っているようです。ここに私のコードです:TFramedTransportは、属性self.__transを持って、私はTTransport.pyでコードをチェックhbase thrift2 pythonクライアントAPIが無効

Traceback (most recent call last): File "./load_hbase.py", line 46, in fetch_rows_from_hbase(thrift_protocol, thrift_client) File "./load_hbase.py", line 37, in fetch_rows_from_hbase scan_id = thrift_client.openScanner(hbase_table_name, tscan) File "/home/lishaohua/kpn/load_hbase/thrift2/hbase/THBaseService.py", line 715, in openScanner return self.recv_openScanner() File "/home/lishaohua/kpn/load_hbase/thrift2/hbase/THBaseService.py", line 735, in recv_openScanner result.read(iprot) File "/home/lishaohua/kpn/load_hbase/thrift2/hbase/THBaseService.py", line 3278, in read fastbinary.decode_binary(self, iprot.trans, (self.class, self.thrift_spec)) AttributeError: 'TFramedTransport' object has no attribute 'trans'

:ここ

def create_hbase_connection(): 
    thrift_socket = TSocket.TSocket(thrift_server, thrift_port) 
    thrift_socket.setTimeout(thrift_timeout) 
    thrift_transport = TTransport.TFramedTransport(thrift_socket) 
    thrift_protocol = TBinaryProtocol.TBinaryProtocolAccelerated(thrift_transport) 
    thrift_client = THBaseService.Client(thrift_protocol) 
    try: 
     thrift_transport.open() 
    except Exception as e: 
     print "connect to hbase thrift failed. (%s)" % e 
     sys.exit() 

    return thrift_protocol, thrift_client 

def fetch_rows_from_hbase(thrift_protocol, thrift_client, start_row = None): 
    tscan = ttypes.TScan() 
    if start_row != None: 
     tscan.startRow = start_row 
    tscan.maxVersions = max_versions 
    tscan.filterString = "FamilyFilter(!=, 'binary:ge')" 
    scan_id = thrift_client.openScanner(hbase_table_name, tscan) 
    result = thrift_client.getScannerRows(scan_id, row_limits + 1) 
    print result 
    print "=================================================\n" 
    thrift_client.closeScanner(scan_id) 
    thrift_protocol.close() 

if __name__ == '__main__': 
    thrift_protocol, thrift_client = create_hbase_connection() 
    fetch_rows_from_hbase(thrift_protocol, thrift_client) 

そしてエラーです。これを修正するには?私は単にtans__transに変更することができますが、さらに問題があります。

答えて

0

TFramedTransportの代わりにTBufferedTransportを使用しました。私の解決策を試すことができます:

class ThriftConn(object): 
    def __init__(self, ip, port, service_cls): 
     self.socket = TSocket.TSocket(ip, port) 
     self.trans = TTransport.TBufferedTransport(self.socket)                                     
     self.protocol = TBinaryProtocol.TBinaryProtocol(self.trans) 
     self.client = service_cls.Client(self.protocol) 

    def __enter__(self): 
     self.trans.open() 

    def __exit__(self, exception_type, exception_value, 
     exception_traceback): 
     self.trans.close() 

HBASE_SERVER_CLIENT = ThriftConn(ip, port, THBaseService) 
with HBASE_SERVER_CLIENT: 
    scan = TScan() 
    scan_id = HBASE_SERVER_CLIENT.client.openScanner(table, scan) 
関連する問題