Ich habe eine Hive-Tabelle namens „database_table“ mit den folgenden Feldern:
Code: Select all
id,field_id,table_name,schema_name
1,1,employee_table,employee
2,2,department_table,employeee
3,3,statistics_table,employeee
I habe den folgenden Code:
Code: Select all
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/')
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)