utils.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(project, table, partition) -> 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}')
  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_origin_hive(args):
  35. project = "loghubods"
  36. table = "alg_recsys_view_sample_v2"
  37. partition = args.partition
  38. count = check_data(project, table, partition)
  39. if count == 0:
  40. print("1")
  41. exit(1)
  42. else:
  43. print("0")
  44. if __name__ == '__main__':
  45. parser = argparse.ArgumentParser(description='脚本utils')
  46. parser.add_argument('--excute_program', type=str, help='执行程序')
  47. parser.add_argument('--partition', type=str, help='表分区')
  48. args = parser.parse_args()
  49. if args.excute_program == "check_origin_hive":
  50. check_origin_hive(args)
  51. else:
  52. print("无合法参数,验证失败。")
  53. exit(999)