安装这个是为了更方便的使用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")