utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import pickle
  2. import os
  3. import requests
  4. import json
  5. from odps import ODPS
  6. from config import set_config
  7. config_ = set_config()
  8. def get_data_from_odps(date, project, table, connect_timeout=3000, read_timeout=500000,
  9. pool_maxsize=1000, pool_connections=1000):
  10. """
  11. 从odps获取数据
  12. :param date: 日期 type-string '%Y%m%d'
  13. :param project: type-string
  14. :param table: 表名 type-string
  15. :param connect_timeout: 连接超时设置
  16. :param read_timeout: 读取超时设置
  17. :param pool_maxsize:
  18. :param pool_connections:
  19. :return: records
  20. """
  21. odps = ODPS(
  22. access_id='LTAI4FtW5ZzxMvdw35aNkmcp',
  23. secret_access_key='0VKnydcaHK3ITjylbgUsLubX6rwiwc',
  24. project=project,
  25. endpoint='http://service.cn.maxcompute.aliyun.com/api',
  26. connect_timeout=connect_timeout,
  27. read_timeout=read_timeout,
  28. pool_maxsize=pool_maxsize,
  29. pool_connections=pool_connections
  30. )
  31. records = odps.read_table(name=table, partition='dt=%s' % date)
  32. return records
  33. def write_to_pickle(data, filename, filepath=config_.DATA_DIR_PATH):
  34. """
  35. 将数据写入pickle文件中
  36. :param data: 数据
  37. :param filename: 写入的文件名
  38. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  39. :return: None
  40. """
  41. if not os.path.exists(filepath):
  42. os.makedirs(filepath)
  43. file = os.path.join(filepath, filename)
  44. with open(file, 'wb') as wf:
  45. pickle.dump(data, wf)
  46. def read_from_pickle(filename, filepath=config_.DATA_DIR_PATH):
  47. """
  48. 从pickle文件读取数据
  49. :param filename: 文件名
  50. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  51. :return: data
  52. """
  53. file = os.path.join(filepath, filename)
  54. if not os.path.exists(file):
  55. return None
  56. with open(file, 'rb') as rf:
  57. data = pickle.load(rf)
  58. return data
  59. def request_post(request_url, request_data):
  60. """
  61. post 请求 HTTP接口
  62. :param request_url: 接口URL
  63. :param request_data: 请求参数
  64. :return: res_data json格式
  65. """
  66. response = requests.post(url=request_url, json=request_data)
  67. if response.status_code == 200:
  68. res_data = json.loads(response.text)
  69. return res_data