Thrift is a binary communication middleware developed by Facebook and open source, and through thrift, we can take advantage of each language to write efficient code.
Paper on Thrift: http://pan.baidu.com/share/link?shareid=234128&uk=3238841275
Installing thrift:http://thrift.apache.org/docs/install/ubuntu/
After the installation is complete, go to the HBase directory and locate the Hbase.thrift file in
Hbase-0.94.4/src/main/resources/org/apache/hadoop/hbase/thrift can be found under
Thrift--gen python Hbase.thrift generates gen-py folder and modifies it to hbase
Install Python's Thrift Library
sudo pip install thrift
Thrift service to start HBase: bin/hbase-daemon.sh start Thrift default port is 9090
To 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 ()
Execute the code, after success, enter HBase's shell and use the command list to see 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)
Get 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 are ', R.columns.get (' cf:a '). Value
To return multiple rows, you need to use scan:
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 is to fetch 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)