安裝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
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/debian, https://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