Created
May 29, 2021 16:05
-
-
Save ganny26/47a44da07e8ceeeea0aaf9f79d00d703 to your computer and use it in GitHub Desktop.
main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from impala.dbapi import connect | |
| import logging | |
| import os | |
| from utils.kerberos import * | |
| import atexit | |
| from scheduler import ( | |
| register_kerberos_scheduler, | |
| krb_scheduler, | |
| remove_shutdown_scheduler, | |
| ) | |
| from flask import Flask, jsonify, request | |
| port = int(os.getenv("PORT", 5000)) | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format="[%(asctime)s]: {} %(levelname)s %(message)s".format(os.getpid()), | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| handlers=[logging.StreamHandler()], | |
| ) | |
| app = Flask(__name__) | |
| # KeyTab authentication | |
| keytab_filename = "keytab/" + "samplekeytab" + ".keytab" | |
| keytab_path = os.path.join(app.root_path, keytab_filename) | |
| logging.info("Keytab Path %s", keytab_path) | |
| kerberos = Kerberos("samplekeytab", keytab_path) | |
| ticket_status = kerberos.create_ticket() | |
| logging.info(ticket_status) | |
| logging.info("Current KeyTab User %s", "samplekeytab") | |
| register_kerberos_scheduler("samplekeytab", keytab_path) | |
| conn = connect( | |
| host="abcdata.domain.com", | |
| port=21050, | |
| database="default", | |
| kerberos_service_name="impala", | |
| auth_mechanism="GSSAPI" | |
| ) | |
| logging.info("Connection to Impala successful") | |
| @app.route("/users", methods=["GET"]) | |
| def fetch_users(): | |
| pincode = request.args.get('user') | |
| date = request.args.get('date') | |
| query = """select name,address,city where pincode = '{}' and date = '{}'""".format( | |
| pincode, date) | |
| logging.info("executing query %s", query) | |
| cursor = conn.cursor() | |
| cursor.execute(query) | |
| results = cursor.fetchall() | |
| response = [] | |
| for item in results: | |
| response.append( | |
| { | |
| "name": item[0], | |
| "address": item[1], | |
| "city": item[2] | |
| } | |
| ) | |
| logging.info("response %", response) | |
| return jsonify(response) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=port) | |
| logging.info("Listening on port", port) | |
| atexit.register(lambda: remove_shutdown_scheduler()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment