#!/usr/bin/python2.7
#hive --service hiveserver >/dev/null 2>/dev/null&
#/opt/cloudera/parcels/CDH/lib/hive/lib/pyimport sys
# python与hiveserver交互
sys.path.append('C:/hadoop_jar/py')
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift.transport import TSocket
from thrift import Thrift
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
if __name__=='__main__':
try:
socket = TSocket.TSocket('10.70.50.111', 10000)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
sql = 'select * from test'
transport.open()
client.execute(sql)
with open('C:/Users/DWJ/Desktop/python2hive.txt','w') as out_file:
while client.fetchOne():
out_file.write(client.fetchOne())
transport.close()
except Thrift.TException, tx:
print'%s'%(tx.message)
其中,C:/hadoop_jar/py里的包来自于hive安装文件自带的py,如:/opt/cloudera/parcels/CDH/lib/hive/lib/py,将其添加到python中即可。