Python应用与Hbase交互实例(来源找不着了)

1 生成 Python 语言的 HBase Thrift 客户端函数库
为了建立Thrift 客户端函数库,你需要安装Thrift 。但是Thrift 还没有打包,所以你必须基于源码编译它。因为Thrift 可以通过Homebrew 得到 [4] ,所以在Mac 机器上这一步就很简单:
$ brew install thrift
...
==> Summary
/usr/local/Cellar/thrift/0.8.0: 75 files, 5.4M, built in 2.4 minutes
那些运行其他平台的机器需要手工建立Thrift 。可以查看Thrift 需求 [5] 文档来了解针对你的平台的细节。
完成以后,请验证你的Thrift 已经启动并且工作正常:
$ thrift -version
Thrift version 0.8.0
你希望不用下载HBase 源代码就可以读完这本书,对吗?很抱歉,会让你失望的。如果你需要生成Thrift 客户端,你需要下载HBase 的源代码:
$ wget http://www.apache.org/dist/hbase/hbase-0.92.1/hbase-0.92.1.tar.gz
...
Saving to: `hbase-0.92.1.tar.gz'
$ tar xzf hbase-0.92.1.tar.gz
在下载HBase 源代码和安装Thrift 后,你需要关注一个文件:src/ main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift 。这就是描述HBase 服务API 和有关对象的IDL 文件。请快速浏览一下这个文件——Thrift IDL 是容易读懂的。现在你准备好了用来生成Python 客户端的所有东西。
先给自己创建一个项目目录,然后生成HBase 客户端:
$ mkdir twitbase.py
$ cd twitbase.py
$ thrift -gen py ../hbase-0.92.1/src/main/resources/org/apache/hadoop/hbase/
thrift/Hbase.thrift
$ mv gen-py/* .
$ rm -r gen-py/
你创建了一个叫做twitbase.py 的项目,然后生成了HBase Python 函数库。Thrift 在一个叫做gen-py 的子目录里生成它的代码。把这些文件移动到你的项目里,你可以轻松把代码导入到应用里。看看生成了什么文件:
$ find .
./__init__.py
./hbase
./hbase/__init__.py
./hbase/constants.py
./hbase/Hbase-remote
./hbase/Hbase.py
./hbase/ttypes.py
你还需要安装Thrift Python 函数库。这些是通过Python 使用的所有Thrift 服务的核心组件,所以你可以全局性安装它们:
$ sudo easy_install thrift==0.8.0
Searching for thrift==0.8.0
Best match: thrift 0.8.0
...
Finished processing dependencies for thrift
另外,这个函数库也是你编译的源代码的一部分。你可以像处理HBase 客户端那样把这些文件复制到你的项目里。在twitbase.py 目录下,你可以复制这些文件,如下所示。
$ mkdir thrift
$ cp -r ../thrift-0.8.0/lib/py/src/* ./thrift/
验证一切按照预期那样工作。先启动Python ,然后导入Thrift HBase 函数库。没有输出信息意味着一切正常:
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>> import thrift
>>> import hbase
确保在twitbase.py 目录下运行这些命令,否则 import 声明会失败。当客户端函数库准备好以后,让我们开启服务组件。
2 启动 HBase Thrift 服务
Thrift 服务端组件已经随HBase 预装了,所以它没有涉及到客户端函数库所需要的安装过程。可以使用hbase 命令,如同启动Shell 一样启动Thrift 服务:
$ hbase thrift
...
usage: Thrift [-b ] [-c] [-f] [-h] [-hsha | -nonblocking |
-threadpool] [-p ]
-b,--bind     Address to bind the Thrift server to. Not supported by
the Nonblocking and HsHa server [default: 0.0.0.0]
-c,--compact        Use the compact protocol
-f,--framed         Use framed transport
-h,--help       Print help information
-hsha           Use the THsHaServer. This implies the framed transport.
-nonblocking        Use the TNonblockingServer. This implies the framed
transport.
-p,--port     Port to bind to [default: 9090]
-threadpool         Use the TThreadPoolServer. This is the default.
先确定HBase 已经启动,并且正在运行,再启动Thrift 服务。默认设置应该可以正常工作:
$ hbase thrift start
...
ThriftServer: starting HBase ThreadPool Thrift server on /0.0.0.0:9090
在客户端和服务器都准备好以后,该测试它们了。在twitbase.py 项目目录下打开一个终端窗口,再一次启动Python
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>> from thrift.transport import TSocket
>>> from thrift.protocol import TBinaryProtocol
>>> from hbase import Hbase
>>> transport = TSocket.TSocket('localhost', 9090)
>>> protocol = TBinaryProtocol.TBinaryProtocol(transport)
>>> client = Hbase.Client(protocol)
>>> transport.open()
>>> client.getTableNames()
['followers', 'twits', 'users']
走到这里花了一些时间,但是一切正常工作!现在你可以开始处理正事儿了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值