hive_to_mongo.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from flask import Flask,request,jsonify
  2. import time
  3. import logging
  4. import traceback
  5. from common.database_dml import insert_data_into_mongo, get_xmo_data_from_hive, delete_data_from_mongo
  6. app = Flask('hive_to_mongo——service')
  7. @app.route('/hello', methods=['POST'])
  8. def hello():
  9. return jsonify(message='Hello, World!')
  10. @app.route('/hive_to_mongo', methods=['POST'])
  11. def data_join():
  12. # 获取程序开始时间
  13. start_time = time.time()
  14. result = {}
  15. success = 0
  16. print("Program starts execution!")
  17. try:
  18. args = request.values.to_dict()
  19. print('args', args)
  20. logger.info(args)
  21. delete_data_from_mongo(args)
  22. df_hive = get_xmo_data_from_hive(args)
  23. if isinstance(df_hive, str):
  24. success = 0
  25. result['msg'] = df_hive
  26. else:
  27. insert_data_into_mongo(df_hive, args)
  28. success = 1
  29. except Exception as e:
  30. my_exception = traceback.format_exc()
  31. my_exception.replace("\n", "\t")
  32. result['msg'] = my_exception
  33. end_time = time.time()
  34. result['success'] = success
  35. result['args'] = args
  36. result['start_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time))
  37. result['end_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time))
  38. print("Program execution ends!")
  39. return result
  40. if __name__ == "__main__":
  41. print("Program starts execution!")
  42. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  43. logger = logging.getLogger("hive_to_mongo")
  44. from waitress import serve
  45. serve(app, host="0.0.0.0", port=10127)
  46. print("server start!")