Thrift is a binary communication middleware developed and open-source by facebook. with thrift, we can make full use of the advantages of various languages to compile efficient code. Thrift is a binary communication middleware developed and open-source by facebook. with thrift, we can make full use of the advantages of various languages to compile efficient code.
Thrift paper: http://pan.baidu.com/share/link? Consumer id = 234128 & uk = 3238841275
Install thrift: http://thrift.apache.org/docs/install/ubuntu/
After the installation is complete, go to the hbase directory and find Hbase. thrift.
Hbase-0.94.4/src/main/resources/org/apache/hadoop/hbase/thrift can be found
Thrift -- gen python hbase. thrift will generate the gen-py folder and change it to hbase
Install python's thrift library
Sudo pip install thrift
Start hbase's thrift service: bin/hbase-daemon.sh start thrift default port is 9090
Create an hbase table:
from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol from hbase import Hbasefrom 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()
Run the code. after successful execution, go to the hbase shell and run the command list to check that the test table has been created successfully.
Insert data:
from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom 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)
Obtain a row of data:
from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol from hbase import Hbasefrom 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 resultfor r in result: print 'the row is ' , r.row print 'the values is ' , r.columns.get('cf:a').value
If multiple rows are returned, scan is required:
from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol from hbase import Hbasefrom 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 result2
ScannerGet takes only one row of data at a time:
from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol from hbase import Hbasefrom 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)