checkHiveDataUtil.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. from odps import ODPS
  3. from FeishuBot import FeishuBot
  4. import argparse
  5. ODPS_CONFIG = {
  6. 'ENDPOINT': 'http://service.cn.maxcompute.aliyun.com/api',
  7. 'ACCESSID': 'LTAIWYUujJAm7CbH',
  8. 'ACCESSKEY': 'RfSjdiWwED1sGFlsjXv0DlfTnZTG1P',
  9. }
  10. def check_origin_hive(args):
  11. project = "loghubods"
  12. table = args.table
  13. beginStr = args.beginStr
  14. endStr = args.endStr
  15. # 检查从begin到end的每一个小时级分区数据是否存在,有一个存在即算存在可以处理
  16. # 如果全都为空报警
  17. time_sequence = generate_time_sequence(beginStr, endStr)
  18. # exist_partition = []
  19. for time_str in time_sequence:
  20. result = split_date_time(time_str)
  21. partitionDt = result[0]
  22. partitionHh = result[1]
  23. count = check_data(project, table, partitionDt, partitionHh)
  24. if count == 0:
  25. bot = FeishuBot()
  26. # msg = (
  27. # f'推荐模型数据更新 \n【任务名称】:step1校验hive数据源\n【是否成功】:success\n【信息】:table:{table},beginStr:{beginStr},endStr:{endStr}\n【详细日志】:{exist_partition}')
  28. msg = (
  29. f'推荐模型数据更新 \n【任务名称】:step1校验hive数据源\n【是否成功】:error\n【信息】:table:{table},{time_str}分区数据不存在,继续检查')
  30. bot.send_message(msg)
  31. print('1')
  32. exit(1)
  33. else:
  34. continue
  35. print('0')
  36. # exist_partition.append(f'分区:dt={partitionDt}/hh={partitionHh},数据:{count}')
  37. # if len(exist_partition) == 0:
  38. # print('1')
  39. # exit(1)
  40. # else:
  41. # bot = FeishuBot()
  42. # msg = (
  43. # f'推荐模型数据更新 \n【任务名称】:step1校验hive数据源\n【是否成功】:success\n【信息】:table:{table},beginStr:{beginStr},endStr:{endStr}\n【详细日志】:{exist_partition}')
  44. # bot.send_message(msg)
  45. # print('0')
  46. def check_data(project, table, partitionDt, partitionDtHh) -> int:
  47. """检查数据是否准备好,输出数据条数"""
  48. odps = ODPS(
  49. access_id=ODPS_CONFIG['ACCESSID'],
  50. secret_access_key=ODPS_CONFIG['ACCESSKEY'],
  51. project=project,
  52. endpoint=ODPS_CONFIG['ENDPOINT'],
  53. # connect_timeout=300000,
  54. # read_timeout=500000,
  55. # pool_maxsize=1000,
  56. # pool_connections=1000
  57. )
  58. try:
  59. t = odps.get_table(name=table)
  60. # check_res = t.exist_partition(partition_spec=f'dt={partition}')
  61. # 含有hh分区
  62. # if not {partitionDtHh}:
  63. check_res = t.exist_partition(partition_spec=f'dt={partitionDt},hh={partitionDtHh}')
  64. if check_res:
  65. sql = f'select * from {project}.{table} where dt = {partitionDt} and hh={partitionDtHh}'
  66. with odps.execute_sql(sql=sql).open_reader() as reader:
  67. data_count = reader.count
  68. else:
  69. data_count = 0
  70. # else:
  71. # check_res = t.exist_partition(partition_spec=f'dt={partitionDt}')
  72. # if check_res:
  73. # sql = f'select * from {project}.{table} where dt = {partitionDt}'
  74. # with odps.execute_sql(sql=sql).open_reader() as reader:
  75. # data_count = reader.count
  76. # else:
  77. # data_count = 0
  78. except Exception as e:
  79. print("error:" + str(e))
  80. data_count = 0
  81. return data_count
  82. def generate_time_sequence(beginStr, endStr):
  83. # 将字符串时间转换为datetime对象
  84. from datetime import datetime, timedelta
  85. # 定义时间格式
  86. time_format = "%Y%m%d%H"
  87. # 转换字符串为datetime对象
  88. begin_time = datetime.strptime(beginStr, time_format)
  89. end_time = datetime.strptime(endStr, time_format)
  90. # 生成时间序列
  91. time_sequence = []
  92. current_time = begin_time
  93. while current_time <= end_time:
  94. # 将datetime对象转换回指定格式的字符串
  95. time_sequence.append(current_time.strftime(time_format))
  96. # 增加一个小时
  97. current_time += timedelta(hours=1)
  98. return time_sequence
  99. def split_date_time(date_time_str):
  100. # 假设date_time_str是一个长度为12的字符串,格式为YYYYMMDDHH
  101. # 切片获取日期部分(前8位)和时间部分(后4位中的前2位,因为后两位可能是分钟或秒,但这里只取小时)
  102. date_part = date_time_str[:8]
  103. time_part = date_time_str[8:10] # 只取小时部分
  104. # 将结果存储在一个数组中(在Python中通常使用列表)
  105. result = [date_part, time_part]
  106. return result
  107. if __name__ == '__main__':
  108. parser = argparse.ArgumentParser(description='脚本utils')
  109. parser.add_argument('--beginStr', type=str, help='表分区Dt,beginStr')
  110. parser.add_argument('--endStr', type=str, help='表分区Hh,endStr')
  111. parser.add_argument('--table', type=str, help='表名')
  112. argv = parser.parse_args()
  113. check_origin_hive(argv)