用python写hiveclient - 改善 - ITeye技术网站
第一步:
将hive-0.7.0\lib\py 中所有py文件拷贝到你自己的Python工程中。
第二步:
把下面的代码拷贝一份新建一个py文件,修改IP,port ,输入要执行的SQL。
- #encoding=utf-8
- 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('119.188.7.xx', 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.fetchOne()
- print "............"
- except Thrift.TException, tx:
- print '%s' % (tx.message)
- finally:
- transport.close()
- if __name__ == '__main__':
- hiveExe("select count(*) from pokes")
第三步:
hive --service hiveserver (启动hive server)