2017-02-28 5 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)