hive python thrift client

XiaoqiangNan 2013-04-24

#!/usr/bin/env python
#coding:utf-8

import sys
sys.path.append("/usr/local/lib/python2.7/site-packages/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

ip = '192.168.0.176'
port = 10000
#sql = "show tables"
sql = "select count(0) from test_table"

def hiveExe(sql):
        transport = None
        try:
                transport = TSocket.TSocket( ip , port )
                transport = TTransport.TBufferedTransport( transport )
                protocol = TBinaryProtocol.TBinaryProtocol( transport )
                client = ThriftHive.Client( protocol )
                transport.open()
                print "execute sql : %s \n" % sql 
                client.execute( sql )
    
                print "The return value is : \n"
                print client.fetchAll()
                print "\n......"

                transport.close()
        except Thrift.TException,tx:
                print '%s' % tx.message
        finally:
                if transport != None :
                        transport.close()
if __name__ == '__main__':
        hiveExe(sql)

相关推荐