Thrift는 Facebook에서 개발하고 오픈소스로 제공하는 바이너리 통신 미들웨어입니다. Thrift를 통해 각 언어의 장점을 최대한 활용하고 효율적인 코드를 작성할 수 있습니다.
절약에 관한 논문: http://pan.baidu.com/share/link?shareid=234128&uk=3238841275
thrift 설치: http://thrift.apache.org/docs/install/ubuntu/
설치가 완료된 후 hbase 디렉토리로 이동하여 다음을 찾으세요. Hbase.thrift, 파일은
hbase-0.94.4/src/main/resources/org/apache/hadoop/hbase/thrift
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090); transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport); client = Hbase.Client(protocol) transport.open() contents = ColumnDescriptor(name='cf:', maxVersions=1) client.createTable('test', [contents]) print client.getTableNames()코드를 실행한 후 hbase 쉘에 들어가 명령 목록을 사용하여 확인합니다. 방금 말씀하신 내용 테스트 테이블이 성공적으로 생성되었습니다. 데이터 삽입:
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() row = 'row-key1' mutations = [Mutation(column="cf:a", value="1")] client.mutateRow('test', row, mutations, None)데이터 행 가져오기:
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() tableName = 'test' rowKey = 'row-key1' result = client.getRow(tableName, rowKey, None) print result for r in result: print 'the row is ' , r.row print 'the values is ' , r.columns.get('cf:a').value여러 행을 반환하려면 스캔을 사용해야 합니다.
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() scan = TScan() tableName = 'test' id = client.scannerOpenWithScan(tableName, scan, None) result2 = client.scannerGetList(id, 10) print result2scannerGet 한 번에 한 행의 데이터만 가져옵니다.
from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() scan = TScan() tableName = 'test' id = client.scannerOpenWithScan(tableName, scan, None) result = client.scannerGet(id) while result: print result result = client.scannerGet(id)