utils.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. def check_hive(args):
  65. project = args.project
  66. table = args.table
  67. partition = args.partition
  68. count = check_data(project, table, partition)
  69. if count == 0:
  70. print("1")
  71. exit(1)
  72. else:
  73. print("0")
  74. if __name__ == '__main__':
  75. parser = argparse.ArgumentParser(description='脚本utils')
  76. parser.add_argument('--excute_program', type=str, help='执行程序')
  77. parser.add_argument('--partition', type=str, help='表分区')
  78. parser.add_argument('--project', type=str, help='表空间')
  79. parser.add_argument('--table', type=str, help='表名')
  80. args = parser.parse_args()
  81. if args.excute_program == "check_origin_hive":
  82. check_origin_hive(args)
  83. elif args.excute_program == "check_item_hive":
  84. check_item_hive(args)
  85. elif args.excute_program == "check_user_hive":
  86. check_user_hive(args)
  87. elif args.excute_program == "check_hive":
  88. check_hive(args)
  89. else:
  90. print("无合法参数,验证失败。")
  91. exit(999)