Home > Backend Development > Python Tutorial > python operates hbase instance through thrift

python operates hbase instance through thrift

高洛峰
Release: 2016-10-18 14:17:37
Original
1569 people have browsed it

Thrift is a binary communication middleware developed and open sourced by Facebook. Through thrift, we can make full use of the advantages of each language and write efficient code.


Paper about thrift: http://pan.baidu.com/share/link?shareid=234128&uk=3238841275


Install thrift: http://thrift.apache.org/docs/install /ubuntu/


After the installation is completed, go to the directory of hbase and find Hbase.thrift. The file is in


hbase-0.94.4/src/main/resources/org/apache/hadoop/hbase/ It can be found under thrift


thrift --gen python hbase.thrift will generate the gen-py folder and modify it to hbase


Install python’s thrift library


sudo pip install thrift


Start hbase's thrift service: bin/hbase-daemon.sh start thrift The default port is 9090


Create hbase table:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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()
  
  
contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])
  
print client.getTableNames()
Copy after login

Execute the code. After success, enter the hbase shell and use the command list See that the test table just created has been successfully created.

Insert data:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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)
Copy after login

Get one row of data:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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()
  
tableName = 'test'
rowKey = 'row-key1'
  
result = client.getRow(tableName, rowKey, None)
print result
for r in result:
    print 'the row is ' , r.row
    print 'the values is ' , r.columns.get('cf:a').value
Copy after login

To return multiple rows, you need to use scan:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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()
  
scan = TScan()
tableName = 'test'
id = client.scannerOpenWithScan(tableName, scan, None)
  
result2 = client.scannerGetList(id, 10)
  
print result2
Copy after login

scannerGet only fetches one row of data at a time:

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from 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()
  
scan = TScan()
tableName = 'test'
id = client.scannerOpenWithScan(tableName, scan, None)
result = client.scannerGet(id)
while result:
    print result
    result = client.scannerGet(id)
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template