python连接 hive 执行hsql

记录一下笔记:


#!/usr/bin/env python
import sys
sys.path.append('/usr/local/hive-0.10.0/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('192.168.232.200', 10001)
                transport = TTransport.TBufferedTransport(transport)
                protocol = TBinaryProtocol.TBinaryProtocol(transport)
                client = ThriftHive.Client(protocol)
                transport.open()

                sqls = sql.split(";")
                for s in sqls:
                        print s
                        client.execute(s)

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

if __name__ == '__main__':
        hiveExe("use flightdata;show tables")


你可能感兴趣的:(hadoop,数据相关,python)