话不多说,直接上代码
from pyhive import hivedef pyhive(hql):
conn = hive.Connection(host='HiveServer2 host', port=10000, database='ods')
cursor = conn.cursor()
cursor.execute(hql)
for result in cursor.fetchall():
return result
部署时遇到一个问题:
Traceback (most recent call last):
File "test_pyhive.py", line 7, in <module>
from pyhive import hive
File "/usr/lib/python2.7/site-packages/pyhive/hive.py", line 15, in <module>
from TCLIService import TCLIService
File "/usr/lib/python2.7/site-packages/TCLIService/TCLIService.py", line 9, in <module>
from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException
ImportError: cannot import name TFrozenDict
解决方法是:
原因是某些包没有关联上,装包时,加上[hive]后缀
pip install pyhive[hive]