下载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.gzcd 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-gomake # 编译耗时较长sudo make install
启动hbase的thrift服务bin/hbase-daemon.sh start thrift
检查系统进程
~/work/hbase/hbase-1.2.4/conf$ jps3009 thriftserver4184 hmaster5932 jps733 main
可以看到thriftserver已成功启动,然后我们就可以使用多种语言,通过thrift来访问hbase了。
python操作hbase下面以python为例来演示如何访问hbase。
安装依赖包sudo pip install thriftsudo pip install hbase-thrift
demo程序from thrift import thriftfrom thrift.transport import tsocketfrom thrift.transport import ttransportfrom thrift.protocol import tbinaryprotocolfrom hbase import hbasefrom 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 datatransport.open() row = 'row-key1'mutations = [mutation(column="cf:a", value="1")] client.mutaterow('test', row, mutations) # get one rowtablename = 'test'rowkey = 'row-key1'result = client.getrow(tablename, rowkey) print resultfor 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
以上就是python中hbase的操作示例代码分析的详细内容。