Configure Thrift
Python uses the package thrift
The python compiler used by the individual is Pycharm Community Edition. In the project setup, locate Project interpreter, locate the package under the appropriate project, then select "+" to add, search Hbase-thrift (Python Client for HBase Thrift Interface), and then After installing the package.
Install server-side thrift.
Reference website, but also can be installed on the computer for terminal use.
Thrift Getting Started
You can also refer to the installation method
Python example for calling HBase
First, install Thrift
Download thrift, here, I'm using thrift-0.7.0-dev.tar.gz this version
Tar xzf thrift-0.7.0-dev.tar.gz
CD Thrift-0.7.0-dev
sudo./configure–with-cpp=no–with-ruby=no
sudo make
sudo make install
Then, in the source bundle of HBase, find
src/main/resources/org/apache/hadoop/hbase/thrift/
Perform
Thrift–gen py Hbase.thrift
MV gen-py/hbase//usr/lib/python2.4/site-packages/(may differ depending on Python version)
Get Data Example 1
# coding:utf-8from Thrift Import thriftfrom thrift.transport import tsocketfrom thrift.transport import ttransportfrom th Rift.protocol Import tbinaryprotocolfrom hbase import hbase# from hbase.ttypes import Columndescriptor, Mutation, Batchmu Tationfrom hbase.ttypes Import *import csvdef client_conn (): # make Socket transport = Tsocket.tsocket (' Hostname,like:loc Alhost ', port) # buffering is critical. Raw sockets is very slow transport = Ttransport.tbufferedtransport (transport) # Wrap in a protocol protocol = Tbinaryprot Ocol. Tbinaryprotocol (Transport) # Create a client to use the Protocol encoder client = hbase.client (protocol) # connect! Transport.open () return clientif __name__ = = "__MAIN__": client = Client_conn () # r = client.getrowwithcolumns (' Table name ', ' Row name ', [' column name ']) # print (R[0].columns.get (' column name ')), type ((R[0].columns.get (' column name ')), result = Client.getrow ("Table name", "Row name") data_simple =[] # print Result[0].columns.items () for K, V in result[0].columns.items (): #.keys () #data. Append ((k,v)) # Print type (k), type (v), V.value,,v.timestamp Data_simple.append (( V.timestamp, V.value)) writer.writerows (data) csvfile.close () csvfile_simple = open ("Data_xy_simple.csv", "WB") writer _simple = Csv.writer (csvfile_simple) writer_simple.writerow (["Timestamp", "value"]) writer_simple.writerows (data_ Simple) Csvfile_simple.close () print "Finished"
The basic python should know that result is a list,result[0].columns.items () is a dict key-value pair. Can query the relevant information. Or, by outputting a variable, observe the value and type of the variable.
Note: The above program Transport.open () to link, after the execution, you also need to disconnect transport.close ()
Currently, only read data is involved, and other dBASE operations will continue to be updated later.