python連接hbase


安裝HBase

HBase是一個構建在HDFS上的分布式列存儲系統,主要用於海量結構化數據存儲。這里,我們的目標只是為Python訪問HBase提供一個基本的環境,故直接下載二進制包,采用單機安裝。下載后解壓,修改配置文件,然后可以直接啟動HBase了。所用系統版本為ubuntu14.04。

下載

wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/1.2.4/hbase-1.2.4-bin.tar.gz tar zxvf hbase-1.2.4-bin.tar.gz

 

配置

修改hbase-env.sh,設置JAVA_HOME。

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

 

修改hbase-site.xml,設置存儲數據的根目錄。

<configuration> <property> <name>hbase.rootdir</name> <value>file:///home/mi/work/hbase/data</value> </property> </configuration>

啟動

bin/start-hbase.sh  # 啟動 bin/hbase shell # 進入hbase交互shell

 

安裝Thrift

安裝好HBase之后,還需安裝Thrift,因為其他語言調用HBase時,需要通過Thrift進行連接。

安裝Thrift依賴

sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config
  • 1

PS: libboost1.55-all-dev,在我的ubuntu14.04上安裝有點問題,所以裝的是libboost1.55。

編譯安裝

下載源碼,解壓后進行編譯安裝。Thrift下載地址

tar zxf thrift-0.10.0.tar.gz cd thrift-0.10.0/ ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go make # 編譯耗時較長 sudo make install

 

參考文檔: 
https://thrift.apache.org/docs/install/debianhttps://thrift.apache.org/docs/BuildingFromSource

啟動HBase的Thrift服務

bin/hbase-daemon.sh start thrift

 

檢查系統進程

~/work/hbase/hbase-1.2.4/conf$ jps 3009 ThriftServer 4184 HMaster 5932 Jps 733 Main

可以看到ThriftServer已成功啟動,然后我們就可以使用多種語言,通過Thrift來訪問HBase了。

Python操作HBase

下面以Python為例來演示如何訪問HBase。

安裝依賴包

sudo pip install thrift sudo pip install hbase-thrift

Demo程序

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.deleteTable('test') client.createTable('test', [contents]) print client.getTableNames() # insert data transport.open() row = 'row-key1' mutations = [Mutation(column="cf:a", value="1")] client.mutateRow('test', row, mutations) # get one row tableName = 'test' rowKey = 'row-key1' result = client.getRow(tableName, rowKey) print result for r in result: print 'the row is ', r.row print 'the values is ', r.columns.get('cf:a').value

執行結果:

['test'] [TRowResult(columns={'cf:a': TCell(timestamp=1488617173254, value='1')}, row='row-key1')] the row is row-key1 the values is 1

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM