Python uses thrift for simple hbase operations

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 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:

 1 from thrift import Thrift 2 from thrift.transport import TSocket 3 from thrift.transport import TTransport 4 from thrift.protocol import TBinaryProtocol 5  6 from hbase import Hbase 7 from hbase.ttypes import * 8  9 transport = TSocket.TSocket('localhost', 9090);10 11 transport = TTransport.TBufferedTransport(transport)12 13 protocol = TBinaryProtocol.TBinaryProtocol(transport);14 15 client = Hbase.Client(protocol)16 transport.open()17 18 19 contents = ColumnDescriptor(name='cf:', maxVersions=1)20 client.createTable('test', [contents])21 22 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:

 1 from thrift import Thrift 2 from thrift.transport import TSocket 3 from thrift.transport import TTransport 4 from thrift.protocol import TBinaryProtocol 5  6 from hbase import Hbase 7  8 from hbase.ttypes import * 9 10 transport = TSocket.TSocket('localhost', 9090)11 12 transport = TTransport.TBufferedTransport(transport)13 14 protocol = TBinaryProtocol.TBinaryProtocol(transport)15 16 client = Hbase.Client(protocol)17 18 transport.open()19 20 row = 'row-key1'21 22 mutations = [Mutation(column="cf:a", value="1")]23 client.mutateRow('test', row, mutations, None)

Insert successful. Run the scan command to view the insert result:

Obtain a row of data:

 

 1 from thrift import Thrift 2 from thrift.transport import TSocket 3 from thrift.transport import TTransport 4 from thrift.protocol import TBinaryProtocol 5  6 from hbase import Hbase 7 from hbase.ttypes import * 8  9 transport = TSocket.TSocket('localhost', 9090)10 transport = TTransport.TBufferedTransport(transport)11 12 protocol = TBinaryProtocol.TBinaryProtocol(transport)13 14 client = Hbase.Client(protocol)15 16 transport.open()17 18 tableName = 'test'19 rowKey = 'row-key1'20 21 result = client.getRow(tableName, rowKey, None)22 print result23 for r in result:24     print 'the row is ' , r.row25     print 'the values is ' , r.columns.get('cf:a').value

 

Getrow returns the tresult list. The result is as follows:

If multiple rows are returned, scan is required:

 1 from thrift import Thrift 2 from thrift.transport import TSocket 3 from thrift.transport import TTransport 4 from thrift.protocol import TBinaryProtocol 5  6 from hbase import Hbase 7 from hbase.ttypes import * 8  9 transport = TSocket.TSocket('localhost', 9090)10 transport = TTransport.TBufferedTransport(transport)11 12 protocol = TBinaryProtocol.TBinaryProtocol(transport)13 14 client = Hbase.Client(protocol)15 transport.open()16 17 scan = TScan()18 tableName = 'test'19 id = client.scannerOpenWithScan(tableName, scan, None)20 21 result2 = client.scannerGetList(id, 10)22 23 print result2

Scannergetlist retrieves 10 data records and then outputs the result.

ScannerGet takes only one row of data at a time:

 1 from thrift import Thrift 2 from thrift.transport import TSocket 3 from thrift.transport import TTransport 4 from thrift.protocol import TBinaryProtocol 5  6 from hbase import Hbase 7 from hbase.ttypes import * 8  9 transport = TSocket.TSocket('localhost', 9090)10 transport = TTransport.TBufferedTransport(transport)11 12 protocol = TBinaryProtocol.TBinaryProtocol(transport)13 14 client = Hbase.Client(protocol)15 transport.open()16 17 scan = TScan()18 tableName = 'test'19 id = client.scannerOpenWithScan(tableName, scan, None)20 result = client.scannerGet(id)21 while result:22     print result23     result = client.scannerGet(id)

Output result:

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.