ad_utils.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. from odps import ODPS
  3. import argparse
  4. ODPS_CONFIG = {
  5. 'ENDPOINT': 'http://service.cn.maxcompute.aliyun.com/api',
  6. 'ACCESSID': 'LTAIWYUujJAm7CbH',
  7. 'ACCESSKEY': 'RfSjdiWwED1sGFlsjXv0DlfTnZTG1P',
  8. }
  9. def check_data_hh(project, table, partition, hh) -> int:
  10. """检查数据是否准备好,输出数据条数"""
  11. odps = ODPS(
  12. access_id=ODPS_CONFIG['ACCESSID'],
  13. secret_access_key=ODPS_CONFIG['ACCESSKEY'],
  14. project=project,
  15. endpoint=ODPS_CONFIG['ENDPOINT'],
  16. connect_timeout=3000,
  17. read_timeout=500000,
  18. pool_maxsize=1000,
  19. pool_connections=1000
  20. )
  21. try:
  22. t = odps.get_table(name=table)
  23. check_res = t.exist_partition(partition_spec=f'dt={partition},hh={hh}')
  24. if check_res:
  25. sql = f'select * from {project}.{table} where dt = {partition}'
  26. with odps.execute_sql(sql=sql).open_reader() as reader:
  27. data_count = reader.count
  28. else:
  29. data_count = 0
  30. except Exception as e:
  31. print("error:" + str(e))
  32. data_count = 0
  33. return data_count
  34. def check_ad_origin_hive(args):
  35. project = "loghubods"
  36. table = "alg_recsys_ad_sample_all"
  37. partition = args.partition
  38. hh = args.hh
  39. count = check_data_hh(project, table, partition, hh)
  40. if count == 0:
  41. print("1")
  42. exit(1)
  43. else:
  44. print("0")
  45. if __name__ == '__main__':
  46. parser = argparse.ArgumentParser(description='脚本utils')
  47. parser.add_argument('--excute_program', type=str, help='执行程序')
  48. parser.add_argument('--partition', type=str, help='表分区')
  49. parser.add_argument('--hh', type=str, help='小时级分区时的小时')
  50. parser.add_argument('--project', type=str, help='表空间')
  51. parser.add_argument('--table', type=str, help='表名')
  52. args = parser.parse_args()
  53. if args.excute_program == "check_ad_origin_hive":
  54. check_ad_origin_hive(args)
  55. else:
  56. print("无合法参数,验证失败。")
  57. exit(999)