用python写hiveclient

    博客分类:

  • hadoop
PythonSQLSQL ServerHadoopBBS

第一步:

hive-0.7.0\lib\py 中所有py文件拷贝到你自己的Python工程中。

第二步:

把下面的代码拷贝一份新建一个py文件,修改IP,port ,输入要执行的SQL。

Python代码
  1. #encoding=utf-8
  2. fromhive_serviceimportThriftHive
  3. fromhive_service.ttypesimportHiveServerException
  4. fromthriftimportThrift
  5. fromthrift.transportimportTSocket
  6. fromthrift.transportimportTTransport
  7. fromthrift.protocolimportTBinaryProtocol
  8. defhiveExe(sql):
  9. try:
  10. transport=TSocket.TSocket('119.188.7.xx',10000)
  11. transport=TTransport.TBufferedTransport(transport)
  12. protocol=TBinaryProtocol.TBinaryProtocol(transport)
  13. client=ThriftHive.Client(protocol)
  14. transport.open()
  15. client.execute(sql)
  16. print"Thereturnvalueis:"
  17. printclient.fetchOne()
  18. print"............"
  19. exceptThrift.TException,tx:
  20. print'%s'%(tx.message)
  21. finally:
  22. transport.close()
  23. if__name__=='__main__':
  24. hiveExe("selectcount(*)frompokes")
#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)