checkHiveDataUtil.py 4.3 KB

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