1

I am new to flask and i am using the following flask cookiecutter to start with a quick prototype. The main idea of project is to collect data from hive cluster and push it to the end user using flask.

Although, i was successfully able to connect flask to the hive server using pyhive connector but I am getting a weird issue that's the related to the select limit where i am trying to query more than 50 items.

In my case i built just Hive class similar to the flask extension development around for pyhive similar demo:

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

and created an endpoint to load data from hive:

@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)

At the first run everything works fine if i try to load things with limited to 50 items, but as soon as i increase it keeps in state where nothing load. However when i load data using jupyter notebooks it works fine that's why i suspect that i might missed something from my flask code.

davidism
  • 121,510
  • 29
  • 395
  • 339
ziedTn
  • 262
  • 5
  • 17

1 Answers1

1

The issue was library version issues, solved this by adding the following to my requirements:

# Hive with needed dependencies
sasl==0.2.1
thrift==0.11.0
thrift-sasl==0.3.0
PyHive==0.6.1

The old version was as follow:

sasl>=0.2.1
thrift>=0.10.0
#thrift_sasl>=0.1.0
git+https://github.com/cloudera/thrift_sasl  # Using master branch in order to get Python 3 SASL patches
PyHive==0.6.1

As stated in the development requirement files within pyhive project.

ziedTn
  • 262
  • 5
  • 17