thrift for python部署

14 篇文章 1 订阅

安装这个是为了更方便的使用python操作hive。

获取thrift,

在linux命令下:

wget http://labs.renren.com/apache-mirror/thrift/0.8.0/thrift-0.8.0.tar.gz

tar -xvf thrift-0.8.0.tar.gz

cd thrift-0.8.0

./configure

make

sudo make install

 

然后再安装thrift of python 模块,很简单。用之前的easy_install这个命令自动安装。

[root@master ~]# easy_install thrift
Searching for thrift
Best match: thrift 0.8.0
Processing thrift-0.8.0-py2.7-linux-x86_64.egg
thrift 0.8.0 is already the active version in easy-install.pth

Using /usr/local/lib/python2.7/site-packages/thrift-0.8.0-py2.7-linux-x86_64.egg
Processing dependencies for thrift
Finished processing dependencies for thrift

 

如果是root用户就直接用easy_install thrift,如果是其他用户的话需要sudo easy_install thrift。

另外关于python连接到hive的操作,在官方网站上和其他的博客里都有相关的介绍操作也很简单,这里就不具体举例操作了。


#!/usr/bin/env python
import sys
sys.path.append('/usr/local/hive/lib/py')
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

def hiveExe(sql):

try:
transport = TSocket.TSocket('127.0.0.1', 10000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
transport.open()

client.execute(sql)

print "The return value is : "
print client.fetchAll()
print "............"
transport.close()
except Thrift.TException, tx:
print '%s' % (tx.message)

if __name__ == '__main__':
hiveExe("select * from paylog limit 10")

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值