Python to manipulate hbase instances with thrift

Source: Internet
Author: User
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)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.