安装thrift
下载
http://thrift.apache.org/download/
1004 tar zxvf thrift-0.9.1.tar.gz
1005 cd thrift-0.9.1
1006 ll
1007 cd ..
1008 mv thrift-0.9.1 thrift
1009 cd thrift
1010 ll
1011 ./configure
1012 make
1013 make install
$ 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服务的核心组件,所以你可以全局性安装它们:
个人新增
[hadoop@hadoop4 twitbase.py]$ mkdir thrift
[root@hadoop4 src]# cp -r /root/thrift/lib/py/src/* /home/hadoop/twitbase.py/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声明会失败。当客户端函数库准备好以后,让我们开启服务组件。
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']
走到这里花了一些时间,但是一切正常工作!现在你可以开始处理正事儿了。
[1] Thrift项目最初出自于Facebook,现在是一个Apache项目。更多细节参见http://thrift.apache.org/。
[2] 更多细节,参见JIRA单子 “Thrift server to match the new Java API”:https://issues.apache.org/jira/browse/HBASE-1744。
[3] 好吧,你可以使用endpoint协处理器,但是你必须为开放的每个endpoint修改Hbase.thrift文件。更多细节,参见“Make Endpoint Coprocessors Available from Thrift,” https://issues.apache.org/jira/browse/HBASE-5600。
[4] Homebrew是“The missing package manager for OS X.”,更多细节,参见http://mxcl.github.com/homebrew/。
[5] Apache Thrift需求文档:http://thrift.apache.org/docs/install/.