基于PyHive库使用python访问Hive

安装:

  1. sasl下载地址下载所需sasl,要和python版本匹配,pip install sasl‑xxx.whl (如果没安装wheel,pip install wheel)
  2. conda install pyhive

使用:

同步

from pyhive import presto  # or import hive
cursor = presto.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')
print cursor.fetchone()
print cursor.fetchall()

异步

from pyhive import hive
from TCLIService.ttypes import TOperationState
cursor = hive.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10', async=True)

status = cursor.poll().operationState
while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):
    logs = cursor.fetch_logs()
    for message in logs:
        print message

    # If needed, an asynchronous query can be cancelled at any time with:
    # cursor.cancel()

    status = cursor.poll().operationState

print cursor.fetchall()

猜你喜欢

转载自blog.csdn.net/zhaoyin214/article/details/81326502