HBase是一個構建在HDFS上的分佈式列存儲系統,主要用於海量結構化數據存儲。這裏,咱們的目標只是爲Python訪問HBase提供一個基本的環境,故直接下載二進制包,採用單機安裝。下載後解壓,修改配置文件,而後能夠直接啓動HBase了。所用系統版本爲ubuntu14.04。php
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。java
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
修改hbase-site.xml,設置存儲數據的根目錄。python
<configuration> <property> <name>hbase.rootdir</name> <value>file:///home/mi/work/hbase/data</value> </property> </configuration>
啓動git
bin/start-hbase.sh # 啓動 bin/hbase shell # 進入hbase交互shell
安裝好HBase以後,還需安裝Thrift,由於其餘語言調用HBase時,須要經過Thrift進行鏈接。sql
sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config
PS: libboost1.55-all-dev,在個人ubuntu14.04上安裝有點問題,因此裝的是libboost1.55。shell
下載源碼,解壓後進行編譯安裝。Thrift下載地址apache
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/debian, https://thrift.apache.org/docs/BuildingFromSourceubuntu
bin/hbase-daemon.sh start thrift
檢查系統進程ruby
能夠看到ThriftServer已成功啓動,而後咱們就能夠使用多種語言,經過Thrift來訪問HBase了。~/work/hbase/hbase-1.2.4/conf$ jps 3009 ThriftServer 4184 HMaster 5932 Jps 733 Main
下面以Python爲例來演示如何訪問HBase。bash
sudo pip install thrift sudo pip install hbase-thrift
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