Python uses thrift to operate hbase instances

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

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.