[Python] Display the hive table results in UI using flask

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to Flask. I have a Hive table called database_table with following fields:

    id,field_id,table_name,schema_name
    1,1,employee_table,employee
    2,2,department_table,employeee
    3,3,statistics_table,employeee


    Based on this database_table when we select field_id based on that we should see the results of following of table in UI using Flask code example. When we select field_id =1 then it should pick the table name from database_table and query the employee_Table and display the results in UI.

    I have the following code:

    from pyhive import hive
    from flask import current_app

    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 table_name from database_table where field_id=1 {0}".format(limit)
    cur.execute(query)
    res = cur.fetchall()
    return jsonify(data=res)

    Continue reading...

Compartilhe esta Página