01_ad_model_update.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/bin/sh
  2. set -x
  3. source /root/anaconda3/bin/activate py37
  4. sh_path=$(dirname $0)
  5. source ${sh_path}/00_common.sh
  6. export PATH=$SPARK_HOME/bin:$PATH
  7. export HADOOP_CONF_DIR=/etc/taihao-apps/hadoop-conf
  8. export JAVA_HOME=/usr/lib/jvm/java-1.8.0
  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.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. }
  95. # 校验大数据任务是否执行完成
  96. check_ad_hive() {
  97. local step_start_time=$(date +%s)
  98. local max_hour=05
  99. local max_minute=30
  100. local elapsed=0
  101. while true; do
  102. local python_return_code=$(python ${sh_path}/ad_utils.py --excute_program check_ad_origin_hive --partition ${today_early_1} --hh 23)
  103. elapsed=$(($(date +%s) - $step_start_time))
  104. if [ "$python_return_code" -eq 0 ]; then
  105. break
  106. fi
  107. echo "Python程序返回非0值,等待五分钟后再次调用。"
  108. sleep 300
  109. local current_hour=$(date +%H)
  110. local current_minute=$(date +%M)
  111. if (( current_hour > max_hour || (current_hour == max_hour && current_minute >= max_minute) )); then
  112. local msg="大数据数据生产校验失败, 分区: ${today_early_1}"
  113. echo -e "$LOG_PREFIX -- 大数据数据生产校验 -- ${msg}: 耗时 $elapsed"
  114. /root/anaconda3/bin/python ${sh_path}/ad_monitor_util.py --level error --msg "$msg" --start "$start_time" --elapsed "$elapsed"
  115. exit 1
  116. fi
  117. done
  118. echo "$LOG_PREFIX -- 大数据数据生产校验 -- 大数据数据生产校验通过: 耗时 $elapsed"
  119. }
  120. make_origin_data() {
  121. local step_start_time=$(date +%s)
  122. /opt/apps/SPARK2/spark-2.4.8-hadoop3.2-1.0.8/bin/spark-class2 org.apache.spark.deploy.SparkSubmit \
  123. --class com.aliyun.odps.spark.examples.makedata_ad.v20240718.makedata_ad_31_originData_20240718 \
  124. --master yarn --driver-memory 1G --executor-memory 2G --executor-cores 1 --num-executors 16 \
  125. ./target/spark-examples-1.0.0-SNAPSHOT-shaded.jar \
  126. tablePart:64 repartition:32 \
  127. beginStr:${today_early_1}00 endStr:${today_early_1}12 \
  128. savePath:${TRAIN_PATH} \
  129. table:${TABLE} \
  130. filterHours:00,01,02,03,04,05,06,07 \
  131. idDefaultValue:0.1 &
  132. /opt/apps/SPARK2/spark-2.4.8-hadoop3.2-1.0.8/bin/spark-class2 org.apache.spark.deploy.SparkSubmit \
  133. --class com.aliyun.odps.spark.examples.makedata_ad.v20240718.makedata_ad_31_originData_20240718 \
  134. --master yarn --driver-memory 1G --executor-memory 2G --executor-cores 1 --num-executors 16 \
  135. ./target/spark-examples-1.0.0-SNAPSHOT-shaded.jar \
  136. tablePart:64 repartition:32 \
  137. beginStr:${today_early_1}13 endStr:${today_early_1}18 \
  138. savePath:${TRAIN_PATH} \
  139. table:${TABLE} \
  140. filterHours:00,01,02,03,04,05,06,07 \
  141. idDefaultValue:0.1 &
  142. /opt/apps/SPARK2/spark-2.4.8-hadoop3.2-1.0.8/bin/spark-class2 org.apache.spark.deploy.SparkSubmit \
  143. --class com.aliyun.odps.spark.examples.makedata_ad.v20240718.makedata_ad_31_originData_20240718 \
  144. --master yarn --driver-memory 1G --executor-memory 2G --executor-cores 1 --num-executors 16 \
  145. ./target/spark-examples-1.0.0-SNAPSHOT-shaded.jar \
  146. tablePart:64 repartition:32 \
  147. beginStr:${today_early_1}19 endStr:${today_early_1}23 \
  148. savePath:${TRAIN_PATH} \
  149. table:${TABLE} \
  150. filterHours:00,01,02,03,04,05,06,07 \
  151. idDefaultValue:0.1 &
  152. wait
  153. local return_code=$?
  154. check_run_status $return_code $step_start_time "spark原始样本生产任务"
  155. }
  156. make_bucket_feature() {
  157. local step_start_time=$(date +%s)
  158. /opt/apps/SPARK2/spark-2.4.8-hadoop3.2-1.0.8/bin/spark-class2 org.apache.spark.deploy.SparkSubmit \
  159. --class com.aliyun.odps.spark.examples.makedata_ad.v20240718.makedata_ad_33_bucketData_20240718 \
  160. --master yarn --driver-memory 2G --executor-memory 4G --executor-cores 1 --num-executors 16 \
  161. ./target/spark-examples-1.0.0-SNAPSHOT-shaded.jar \
  162. beginStr:${today_early_1} endStr:${today_early_1} repartition:100 \
  163. filterNames:_4h_,_5h_,adid_,targeting_conversion_ \
  164. readPath:${TRAIN_PATH} \
  165. savePath:${BUCKET_FEATURE_PATH}
  166. local return_code=$?
  167. check_run_status $return_code $step_start_time "spark特征分桶任务"
  168. }
  169. xgb_train() {
  170. local step_start_time=$(date +%s)
  171. /opt/apps/SPARK3/spark-3.3.1-hadoop3.2-1.0.5/bin/spark-class org.apache.spark.deploy.SparkSubmit \
  172. --class com.tzld.piaoquan.recommend.model.train_01_xgb_ad_20240808 \
  173. --master yarn --driver-memory 6G --executor-memory 9G --executor-cores 1 --num-executors 31 \
  174. --conf spark.yarn.executor.memoryoverhead=1000 \
  175. --conf spark.shuffle.service.enabled=true \
  176. --conf spark.shuffle.service.port=7337 \
  177. --conf spark.shuffle.consolidateFiles=true \
  178. --conf spark.shuffle.manager=sort \
  179. --conf spark.storage.memoryFraction=0.4 \
  180. --conf spark.shuffle.memoryFraction=0.5 \
  181. --conf spark.default.parallelism=200 \
  182. /root/zhangbo/recommend-model/recommend-model-produce/target/recommend-model-produce-jar-with-dependencies.jar \
  183. featureFile:20240703_ad_feature_name.txt \
  184. trainPath:${train_data_path} \
  185. testPath:${predict_date_path} \
  186. savePath:${new_model_predict_result_path} \
  187. modelPath:${model_save_path} \
  188. eta:0.01 gamma:0.0 max_depth:5 num_round:1000 num_worker:30 repartition:20
  189. local return_code=$?
  190. check_run_status $return_code $step_start_time "XGB模型训练任务"
  191. }
  192. model_predict() {
  193. # 线上模型评估最新的数据
  194. local step_start_time=$(date +%s)
  195. /opt/apps/SPARK3/spark-3.3.1-hadoop3.2-1.0.5/bin/spark-class org.apache.spark.deploy.SparkSubmit \
  196. --class com.tzld.piaoquan.recommend.model.pred_01_xgb_ad_hdfsfile_20240813 \
  197. --master yarn --driver-memory 1G --executor-memory 1G --executor-cores 1 --num-executors 30 \
  198. --conf spark.yarn.executor.memoryoverhead=1024 \
  199. --conf spark.shuffle.service.enabled=true \
  200. --conf spark.shuffle.service.port=7337 \
  201. --conf spark.shuffle.consolidateFiles=true \
  202. --conf spark.shuffle.manager=sort \
  203. --conf spark.storage.memoryFraction=0.4 \
  204. --conf spark.shuffle.memoryFraction=0.5 \
  205. --conf spark.default.parallelism=200 \
  206. /root/zhangbo/recommend-model/recommend-model-produce/target/recommend-model-produce-jar-with-dependencies.jar \
  207. featureFile:20240703_ad_feature_name.txt \
  208. testPath:${predict_date_path} \
  209. savePath:${online_model_predict_result_path} \
  210. modelPath:${online_model_path}
  211. local return_code=$?
  212. check_run_status $return_code $step_start_time "线上模型评估${predict_date_path: -8}的数据"
  213. local mean_abs_diff=$(/root/anaconda3/bin/python ${sh_path}/model_predict_analyse.py -p ${online_model_predict_result_path} ${new_model_predict_result_path})
  214. if (( $(echo "${mean_abs_diff} > 0.000400" | bc -l ) ));then
  215. check_run_status 1 $step_start_time "线上模型评估${predict_date_path: -8}的数据,绝对误差大于0.000400,请检查"
  216. echo "线上模型评估${predict_date_path: -8}的数据,绝对误差大于0.000400,请检查"
  217. exit 1
  218. fi
  219. }
  220. model_upload_oss() {
  221. cd ${model_local_path}
  222. $hadoop fs -get ${model_save_path} ./${model_name}
  223. if [ ! -d ./${model_name} ]; then
  224. echo "从HDFS下载模型失败"
  225. check_run_status 1 $step_start_time "XGB模型训练任务"
  226. exit 1
  227. fi
  228. tar -czvf ${model_name}.tar.gz -C ${model_name} .
  229. rm -rf .${model_name}.tar.gz.crc
  230. $hadoop fs -rm -r -skipTrash ${MODEL_OSS_PATH}/${model_name}.tar.gz
  231. $hadoop fs -put ${model_name}.tar.gz ${MODEL_OSS_PATH}
  232. check_run_status $return_code $step_start_time "模型上传OSS"
  233. echo ${model_save_path} > ${model_path_file}
  234. }
  235. # 主方法
  236. main() {
  237. init
  238. check_ad_hive
  239. make_origin_data
  240. make_bucket_feature
  241. xgb_train
  242. model_predict
  243. # model_upload_oss
  244. }
  245. main