alg_recsys_utils.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. def check_item_hive(args):
  45. project = "loghubods"
  46. table = "alg_recsys_video_info"
  47. partition = args.partition
  48. count = check_data(project, table, partition)
  49. if count == 0:
  50. print("1")
  51. exit(1)
  52. else:
  53. print("0")
  54. def check_user_hive(args):
  55. project = "loghubods"
  56. table = "alg_recsys_user_info"
  57. partition = args.partition
  58. count = check_data(project, table, partition)
  59. if count == 0:
  60. print("1")
  61. exit(1)
  62. else:
  63. print("0")
  64. if __name__ == '__main__':
  65. parser = argparse.ArgumentParser(description='脚本utils')
  66. parser.add_argument('--excute_program', type=str, help='执行程序')
  67. parser.add_argument('--partition', type=str, help='表分区')
  68. args = parser.parse_args()
  69. if args.excute_program == "check_origin_hive":
  70. check_origin_hive(args)
  71. if args.excute_program == "check_item_hive":
  72. check_item_hive(args)
  73. if args.excute_program == "check_user_hive":
  74. check_user_hive(args)
  75. else:
  76. print("无合法参数,验证失败。")
  77. exit(999)