Я новичок в flask и использую следующий flask cookiecutter, чтобы начать с быстрого прототипа. Основная идея проекта — собрать данные из кластера hive и отправить их конечному пользователю с помощью flask.
Хотя мне удалось подключить flask к серверу улья с помощью соединителя pyhive
, но я получаю странную проблему, связанную с select limit
, где я пытаюсь запросить более 50 элементов.
В моем случае я создал только класс Hive, аналогичный разработке расширения фляги, для pyhive
похожей демонстрации:
from pyhive import hive
from flask import current_app
# Find the stack on which we want to store the database connection.
# Starting with Flask 0.9, the _app_ctx_stack is the correct one,
# before that we need to use the _request_ctx_stack.
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
class Hive(object):
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
# Use the newstyle teardown_appcontext if it's available,
# otherwise fall back to the request context
if hasattr(app, 'teardown_appcontext'):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
def connect(self):
return hive.connect(current_app.config['HIVE_DATABASE_URI'], database="orc")
def teardown(self, exception):
ctx = stack.top
if hasattr(ctx, 'hive_db'):
ctx.hive_db.close()
return None
@property
def connection(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'hive_db'):
ctx.hive_db = self.connect()
return ctx.hive_db
и создал конечную точку для загрузки данных из улья:
@blueprint.route('/hive/<limit>')
def connect_to_hive(limit):
cur = hive.connection.cursor()
query = "select * from part_raw where year=2018 LIMIT {0}".format(limit)
cur.execute(query)
res = cur.fetchall()
return jsonify(data=res)
При первом запуске все работает нормально, если я пытаюсь загрузить вещи с ограничением до 50 элементов, но как только я увеличиваю, он остается в состоянии, когда ничего не загружается. Однако, когда я загружаю данные с помощью ноутбуков Jupyter, все работает нормально, поэтому я подозреваю, что мог что-то пропустить в своем коде фляги.