HBase Python API
HBase enables multi-language programming through the thrift mechanism, where information is passed through the port, so Python is a good choice
Spit Groove
Bo Master on Mac configuration hbase, but Zoomkeeper has been error, results Ubuntu virtual machine 10min solve ... But the virtual machine does not have the IDE to write Java code is not convenient, so with the Mac host connected to the idea of virtual machines born, so that can happily use the Host IDE ~
First, server start HBase Thrift RPC
HBase start way There are many, here no longer repeat, Ubuntu started hbase after starting thrift
hbase-daemon.sh start thrift
The default service port is 9090
Second, the client installs the dependency package
sudo pip install thriftsudo pip install hbase-thrift
Third, write the client code
# Coding=utf-8 fromThrift.transportImportTsocket fromThrift.transport.TTransportImportTbufferedtransport fromThrift.protocolImportTbinaryprotocol fromHBaseImportHbase fromHbase.ttypesImportColumndescriptor fromHbase.ttypesImportMutationclassHbaseclient (Object):def __init__( Self, IP, Port=9090):"""establish a connection to the thrift server side """ # server-side address and port settings Self. __transport=Tbufferedtransport (Tsocket.tsocket (IP, port))# Set up transport protocolProtocol=Tbinaryprotocol.tbinaryprotocol ( Self. __transport)# Client Self. __client=Hbase.client (Protocol)# Open Connection Self. __transport.Open()def __del__( Self): Self. __transport.close ()defGet_tables ( Self):"""Get all Tables: Return: List of table names """ return Self. __client.gettablenames ()defCreate_table ( Self, table,*Columns):"""Create a table:p Aram Table: Tables name:p Aram columns: Column family name """Func= LambdaCol:columndescriptor (COL) column_families= Map(Func, columns) Self. __client.createtable (table, column_families)defPut Self, table, row, columns):"""Add a record:p Aram Table: Tables name:p Aram row: Row key:p Aram columns: Column name: return: """Func= Lambda(k, V): Mutation (column=K, value=V) Mutations= Map(Func, Columns.items ()) Self. __client.mutaterow (table, row, mutations)defDelete Self, table, Row, column):"""Deleting Records:p Aram Table: Tables name:p Aram row: Row key """ Self. __client.deleteall (table, row, column)defScan Self, table, Start_row="", columns=None):"""Get Records:p Aram Table: Tables name:p Aram Start_row: Start line:p Aram columns: Column family:p Aram Attributes: """Scanner= Self. __client.scanneropen (table, Start_row, Columns) func= Lambda(k, V): (K, V.value) while True: R= Self. __client.scannerget (Scanner)if notR: Break yield Dict(Map(Func, r[0].columns.items ()))if __name__ == ' __main__ ': Client=Hbaseclient ("10.211.55.7")# client.create_table (' student ', ' name ', ' Course ')Client.put ("Student","1", {"Name:":"Jack","Course:art":" the","Course:math":" A"}) Client.put ("Student","2", {"Name:":"Tom","Course:art":" the","Course:math":" the"}) Client.put ("Student","3", {"Name:":"Jerry"}) Client.delete (' Student ',' 1 ',' Course:math ') forVinchClient.scan (' Student '):PrintV
Iv. test Results
{‘course:art‘‘88‘‘name:‘‘Jack‘}{‘course:art‘‘90‘‘name:‘‘Tom‘‘course:math‘‘100‘}{‘name:‘‘Jerry‘}
V. Summary
With the Python interface, it is very convenient to write simple task scripts, which is greatly facilitated by the RPC mechanism, which provides a good decoupling between client and server and facilitates developer collaboration.
HBase Python API