01_ad_model_update.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/sh
  2. set -x
  3. export PATH=$SPARK_HOME/bin:$PATH
  4. export HADOOP_CONF_DIR=/etc/taihao-apps/hadoop-conf
  5. export JAVA_HOME=/usr/lib/jvm/java-1.8.0
  6. sh_path=$(dirname $0)
  7. source ${sh_path}/00_common.sh
  8. source /root/anaconda3/bin/activate py37
  9. # 全局常量
  10. HADOOP=/opt/apps/HADOOP-COMMON/hadoop-common-current/bin/hadoop
  11. TRAIN_PATH=/dw/recommend/model/31_ad_sample_data_v4_test
  12. BUCKET_FEATURE_PATH=/dw/recommend/model/33_ad_train_data_v4_test
  13. MODEL_PATH=/dw/recommend/model/35_ad_model_test
  14. PREDICT_RESULT_SAVE_PATH=/dw/recommend/model/34_ad_predict_data_test
  15. TABLE=alg_recsys_ad_sample_all
  16. # 特征文件名
  17. feature_file=20240703_ad_feature_name.txt
  18. # 模型OSS保存路径,测试时修改为其他路径,避免影响线上
  19. MODEL_OSS_PATH=oss://art-recommend.oss-cn-hangzhou.aliyuncs.com/model/
  20. # 线上模型名,测试时修改为其他模型名,避免影响线上
  21. model_name=model_xgb_351_1000_v2_test
  22. # 本地保存HDFS模型路径文件
  23. model_path_file=/root/zhaohp/XGB/online_model_path_test.txt
  24. today_early_1="$(date -d '1 days ago' +%Y%m%d)"
  25. # 训练用的数据路径
  26. train_data_path=""
  27. # 评估用的数据路径
  28. predict_date_path=""
  29. #评估结果保存路径
  30. new_model_predict_result_path=""
  31. # 模型保存路径
  32. model_save_path=""
  33. # 模型本地临时保存路径
  34. model_local_path=/root/zhaohp/XGB
  35. # 任务开始时间
  36. start_time=$(date +%s)
  37. # 线上模型在HDFS中的路径
  38. online_model_path=`cat ${model_path_file}`
  39. # 校验命令的退出码
  40. check_run_status() {
  41. local status=$1
  42. local step_start_time=$2
  43. local step_name=$3
  44. local step_end_time=$(date +%s)
  45. local step_elapsed=$(($step_end_time - $step_start_time))
  46. if [ $status -ne 0 ]; then
  47. echo "$LOG_PREFIX -- ${step_name}失败: 耗时 $step_elapsed"
  48. local elapsed=$(($step_end_time - $start_time))
  49. # /root/anaconda3/bin/python ${sh_path}/ad_monitor_util.py --level error --msg "$msg" --start "$start_time" --elapsed "$elapsed"
  50. exit 1
  51. else
  52. echo "$LOG_PREFIX -- ${step_name}成功: 耗时 $step_elapsed"
  53. fi
  54. }
  55. init() {
  56. declare -a date_keys=()
  57. local count=1
  58. local current_data="$(date -d '2 days ago' +%Y%m%d)"
  59. # 循环获取前 n 天的非节日日期
  60. while [[ $count -lt 7 ]]; do
  61. date_key=$(date -d "$current_data" +%Y%m%d)
  62. # 判断是否是节日,并拼接训练数据路径
  63. if [ $(is_not_holidays $date_key) -eq 1 ]; then
  64. # 将 date_key 放入数组
  65. date_keys+=("$date_key")
  66. if [[ -z ${train_data_path} ]]; then
  67. train_data_path="${BUCKET_FEATURE_PATH}/${date_key}"
  68. else
  69. train_data_path="${BUCKET_FEATURE_PATH}/${date_key},${train_data_path}"
  70. fi
  71. count=$((count + 1))
  72. else
  73. echo "日期: ${date_key}是节日,跳过"
  74. fi
  75. current_data=$(date -d "$current_data -1 day" +%Y%m%d)
  76. done
  77. last_index=$((${#date_keys[@]} - 1))
  78. train_first_day=${date_keys[$last_index]}
  79. train_last_day=${date_keys[0]}
  80. model_save_path=${MODEL_PATH}/${model_name}_${train_first_day: -4}_${train_last_day: -4}
  81. predict_date_path=${BUCKET_FEATURE_PATH}/${today_early_1}
  82. new_model_predict_result_path=${PREDICT_RESULT_SAVE_PATH}/${today_early_1}_351_1000_${train_first_day: -4}_${train_last_day: -4}
  83. online_model_predict_result_path=${PREDICT_RESULT_SAVE_PATH}/${today_early_1}_351_1000_${online_model_path: -9}
  84. echo "init param train_data_path: ${train_data_path}"
  85. echo "init param predict_date_path: ${predict_date_path}"
  86. echo "init param new_model_predict_result_path: ${new_model_predict_result_path}"
  87. echo "init param online_model_predict_result_path: ${online_model_predict_result_path}"
  88. echo "init param model_save_path: ${model_save_path}"
  89. echo "init param online_model_path: ${online_model_path}"
  90. echo "init param feature_file: ${feature_file}"
  91. echo "init param model_name: ${model_name}"
  92. echo "init param model_local_path: ${model_local_path}"
  93. echo "init param model_oss_path: ${MODEL_OSS_PATH}"
  94. echo "当前Python环境安装的Python版本: $(python --version)"
  95. echo "当前Python环境安装的三方包: $(python -m pip list)"
  96. }
  97. # 校验大数据任务是否执行完成
  98. check_ad_hive() {
  99. local step_start_time=$(date +%s)
  100. local max_hour=05
  101. local max_minute=30
  102. local elapsed=0
  103. while true; do
  104. local python_return_code=$(python ${sh_path}/ad_utils.py --excute_program check_ad_origin_hive --partition ${today_early_1} --hh 23)
  105. elapsed=$(($(date +%s) - $step_start_time))
  106. if [ "$python_return_code" -eq 0 ]; then
  107. break
  108. fi
  109. echo "Python程序返回非0值,等待五分钟后再次调用。"
  110. sleep 300
  111. local current_hour=$(date +%H)
  112. local current_minute=$(date +%M)
  113. if (( current_hour > max_hour || (current_hour == max_hour && current_minute >= max_minute) )); then
  114. local msg="大数据数据生产校验失败, 分区: ${today_early_1}"
  115. echo -e "$LOG_PREFIX -- 大数据数据生产校验 -- ${msg}: 耗时 $elapsed"
  116. /root/anaconda3/bin/python ${sh_path}/ad_monitor_util.py --level error --msg "$msg" --start "$start_time" --elapsed "$elapsed"
  117. exit 1
  118. fi
  119. done
  120. echo "$LOG_PREFIX -- 大数据数据生产校验 -- 大数据数据生产校验通过: 耗时 $elapsed"
  121. }
  122. origin_data() {
  123. (
  124. source ${sh_path}/25_xgb_make_data_origin_bucket.sh
  125. make_origin_data
  126. )
  127. }
  128. bucket_feature() {
  129. (
  130. source ${sh_path}/25_xgb_make_data_origin_bucket.sh
  131. make_bucket_feature
  132. )
  133. }
  134. xgb_train() {
  135. local step_start_time=$(date +%s)
  136. /opt/apps/SPARK3/spark-3.3.1-hadoop3.2-1.0.5/bin/spark-class org.apache.spark.deploy.SparkSubmit \
  137. --class com.tzld.piaoquan.recommend.model.train_01_xgb_ad_20240808 \
  138. --master yarn --driver-memory 6G --executor-memory 9G --executor-cores 1 --num-executors 31 \
  139. --conf spark.yarn.executor.memoryoverhead=1000 \
  140. --conf spark.shuffle.service.enabled=true \
  141. --conf spark.shuffle.service.port=7337 \
  142. --conf spark.shuffle.consolidateFiles=true \
  143. --conf spark.shuffle.manager=sort \
  144. --conf spark.storage.memoryFraction=0.4 \
  145. --conf spark.shuffle.memoryFraction=0.5 \
  146. --conf spark.default.parallelism=200 \
  147. /root/zhangbo/recommend-model/recommend-model-produce/target/recommend-model-produce-jar-with-dependencies.jar \
  148. featureFile:20240703_ad_feature_name.txt \
  149. trainPath:${train_data_path} \
  150. testPath:${predict_date_path} \
  151. savePath:${new_model_predict_result_path} \
  152. modelPath:${model_save_path} \
  153. eta:0.01 gamma:0.0 max_depth:5 num_round:1000 num_worker:30 repartition:20
  154. local return_code=$?
  155. check_run_status $return_code $step_start_time "XGB模型训练任务"
  156. }
  157. model_predict() {
  158. # 线上模型评估最新的数据
  159. local step_start_time=$(date +%s)
  160. /opt/apps/SPARK3/spark-3.3.1-hadoop3.2-1.0.5/bin/spark-class org.apache.spark.deploy.SparkSubmit \
  161. --class com.tzld.piaoquan.recommend.model.pred_01_xgb_ad_hdfsfile_20240813 \
  162. --master yarn --driver-memory 1G --executor-memory 1G --executor-cores 1 --num-executors 30 \
  163. --conf spark.yarn.executor.memoryoverhead=1024 \
  164. --conf spark.shuffle.service.enabled=true \
  165. --conf spark.shuffle.service.port=7337 \
  166. --conf spark.shuffle.consolidateFiles=true \
  167. --conf spark.shuffle.manager=sort \
  168. --conf spark.storage.memoryFraction=0.4 \
  169. --conf spark.shuffle.memoryFraction=0.5 \
  170. --conf spark.default.parallelism=200 \
  171. /root/zhangbo/recommend-model/recommend-model-produce/target/recommend-model-produce-jar-with-dependencies.jar \
  172. featureFile:20240703_ad_feature_name.txt \
  173. testPath:${predict_date_path} \
  174. savePath:${online_model_predict_result_path} \
  175. modelPath:${online_model_path}
  176. local return_code=$?
  177. check_run_status $return_code $step_start_time "线上模型评估${predict_date_path: -8}的数据"
  178. local mean_abs_diff=$(python ${sh_path}/model_predict_analyse.py -p ${online_model_predict_result_path} ${new_model_predict_result_path})
  179. if (( $(echo "${mean_abs_diff} > 0.000400" | bc -l ) ));then
  180. check_run_status 1 $step_start_time "线上模型评估${predict_date_path: -8}的数据,绝对误差大于0.000400,请检查"
  181. echo "线上模型评估${predict_date_path: -8}的数据,绝对误差大于0.000400,请检查"
  182. exit 1
  183. fi
  184. }
  185. model_upload_oss() {
  186. cd ${model_local_path}
  187. $hadoop fs -get ${model_save_path} ./${model_name}
  188. if [ ! -d ./${model_name} ]; then
  189. echo "从HDFS下载模型失败"
  190. check_run_status 1 $step_start_time "XGB模型训练任务"
  191. exit 1
  192. fi
  193. tar -czvf ${model_name}.tar.gz -C ${model_name} .
  194. rm -rf .${model_name}.tar.gz.crc
  195. $hadoop fs -rm -r -skipTrash ${MODEL_OSS_PATH}/${model_name}.tar.gz
  196. $hadoop fs -put ${model_name}.tar.gz ${MODEL_OSS_PATH}
  197. check_run_status $return_code $step_start_time "模型上传OSS"
  198. echo ${model_save_path} > ${model_path_file}
  199. local step_end_time=$(date +%s)
  200. local elapsed=$(($step_end_time - $start_time))
  201. echo -e "$LOG_PREFIX -- 模型更新完成 -- 模型更新成功: 耗时 $elapsed"
  202. /root/anaconda3/bin/python ${sh_path}/ad_monitor_util.py --level info --msg "模型更新成功" --start "$start_time" --elapsed "$elapsed"
  203. }
  204. # 主方法
  205. main() {
  206. init
  207. check_ad_hive
  208. origin_data
  209. bucket_feature
  210. xgb_train
  211. model_predict
  212. model_upload_oss
  213. }
  214. main