Python操作HBase

转载:https://blog.csdn.net/dutsoft/article/details/60328341

安装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
  • 1
  • 2

配置

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

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

修改hbase-site.xml,设置存储数据的根目录。

<configuration>
    <property>
        <name>hbase.rootdir</name>
        <value>file:///home/mi/work/hbase/data</value>
    </property>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

启动

bin/start-hbase.sh  # 启动
bin/hbase shell  # 进入hbase交互shell
  • 1
  • 2

安装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
  • 1
  • 2
  • 3
  • 4
  • 5

参考文档: 
https://thrift.apache.org/docs/install/debianhttps://thrift.apache.org/docs/BuildingFromSource

启动HBase的Thrift服务

bin/hbase-daemon.sh start thrift
  • 1

检查系统进程

~/work/hbase/hbase-1.2.4/conf$ jps
3009 ThriftServer
4184 HMaster
5932 Jps
733 Main
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到ThriftServer已成功启动,然后我们就可以使用多种语言,通过Thrift来访问HBase了。

Python操作HBase

下面以Python为例来演示如何访问HBase。

安装依赖包

sudo pip install thrift
sudo pip install hbase-thrift
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

执行结果:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值