thrift python安装

  

安装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,然后导入ThriftHBase函数库。没有输出信息意味着一切正常:

$ 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']

走到这里花了一些时间,但是一切正常工作!现在你可以开始处理正事儿了。



[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/.



Thrift is a framework for building cross-language services. It allows you to define data types and service interfaces in a simple definition file, and then generate code in various languages to implement those interfaces. In Python, you can use the Thrift library to create both client and server applications. To use Thrift in Python, you first need to install the Thrift library. You can do this using pip: ``` pip install thrift ``` Once installed, you can start using Thrift in your Python code. Here's a simple example to get you started: 1. Define your data types and service interface in a Thrift IDL file (e.g., `example.thrift`): ``` namespace py example struct MyStruct { 1: required string name 2: optional i32 age } service MyService { MyStruct getStruct(1: string id) } ``` 2. Generate Python code from the IDL file using the `thrift` compiler: ``` thrift --gen py example.thrift ``` 3. Implement the service interface in Python: ```python from example import MyService, ttypes class MyHandler: def getStruct(self, id): # Implementation code goes here return ttypes.MyStruct(name="John", age=25) handler = MyHandler() processor = MyService.Processor(handler) # Run the server transport = TSocket.TServerSocket(port=9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() ``` 4. Create a client to interact with the server: ```python from example import MyService, ttypes transport = TSocket.TSocket("localhost", 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MyService.Client(protocol) transport.open() struct = client.getStruct("123") print(struct.name) print(struct.age) transport.close() ``` This is just a basic example to give you an idea of how to use Thrift with Python. You can find more details and advanced usage in the Thrift documentation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值