pyhive的基本使用
阅读原文时间:2023年07月08日阅读:2

安装

yum -y install cyrus-sasl cyrus-sasl-devel cyrus-sasl-lib  # 解决报错:sasl/sasl.h: No such file or directory

pip install sasl

pip install thrift

pip install thrift_sasl

pip install pyhive

pyhive的两种基本使用方式:同步方式和异步方式:

1、同步DB API

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

2、异步DB API

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)
# 可随时取消异步查询
# cursor.cancel()
status = cursor.poll().operationState

print(cursor.fetchall())

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器