#!/bin/bash
export ETL_HOME=/home/cbd/etl
export PYTHON_HOME=/home/cbd/etl/python
source ${ETL_HOME}/.etl_profile
touser="abd@163.com" #填入企微接收者的邮件地址
today=`date +"%Y%m%d"`
user=`cat ${PYTHON_HOME}/etl_config.py | grep -o -E "ETL_USERNAME_SLAVE02(\s*)=.*" | cut -d '=' -f 2 | grep -o -E "[^\"\s]*"`
password=`cat ${PYTHON_HOME}/etl_config.py | grep -o -E "ETL_PASSWORD_SLAVE02(\s*)=.*" | cut -d '=' -f 2 | grep -o -E "[^\"\s]*"`
host=`cat ${PYTHON_HOME}/etl_config.py | grep -o -E "ETL_HOST_SLAVE02(\s*)=.*" | cut -d '=' -f 2 | grep -o -E "[^\"\s]*"`
port=`cat ${PYTHON_HOME}/etl_config.py | grep -o -E "ETL_PORT_SLAVE02(\s*)=.*" | cut -d '=' -f 2 | grep -o -E "[^\"\s]*"`
db_name=`cat ${PYTHON_HOME}/etl_config.py | grep -o -E "ETL_DB_SLAVE02(\s*)=.*" | cut -d '=' -f 2 | grep -o -E "[^\"\s]*"`
function hadoopjar(){
CURRENT_DATE=$1
i=$2
table_name=`mysql -h${host} -P${port} -u${user} -p${password} -e"select check_table,targettablename from mysql.table_name where id = ${i};" | grep -o -E "[a-zA-Z_]{0,}\.[a-zA-Z0-9_]{0,}"`
source_tablename=$(echo ${table_name[0]} | cut -d ' ' -f 1)
DB=$(echo ${source_tablename} | cut -d '.' -f 1)
TBL_NAME=$(echo ${source_tablename} | cut -d '.' -f 2)
target_tablename=$(echo ${table_name[0]} | cut -d ' ' -f 2)
tablename_tag="."
while [[ source_tablename != *$target_tablename_tag* || $target_tablename != *$target_tablename_tag* ]]
do
sleep 60
table_name=`mysql -h${host} -P${port} -u${user} -p${password} -e"select check_table,targettablename from mysql.table_name where id = ${i};" | grep -o -E "[a-zA-Z_]{0,}\.[a-zA-Z0-9_]{0,}"`
source_tablename=$(echo ${table_name[0]} | cut -d ' ' -f 1)
DB=$(echo ${source_tablename} | cut -d '.' -f 1)
TBL_NAME=$(echo ${source_tablename} | cut -d '.' -f 2)
target_tablename=$(echo ${table_name[0]} | cut -d ' ' -f 2)
done
echo "----------------------------id : ${i} | step 1 start | check done file | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}------------------------------------------"
/home/cbd/etl/bin/check_done_file.sh $DB $TBL_NAME $CURRENT_DATE
echo "----------------------------id : ${i} | step 1 end | check done file | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}------------------------------------------"
sleep 30
echo "----------------------------id : ${i} | step 2 start | struct | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}------------------------------------------"
current_time=`date +"%Y%m%d%H%M%S"`
/home/cbd/etl/bin/hadoop_jar.sh /home/cbd/etl/jar/etl-1.0-SNAPSHOT-jar-with-dependencies.jar com.ppdai.mapreduce.CommonAnalysisStructParquetRun $CURRENT_DATE 'day' $i > /home/cbd/etl/log/${target_tablename}.${current_time}.struct.log 2>&1
RET_CODE=$?
if [ $RET_CODE -ne 0 ]
then
msg="${i}-target_tablename:${target_tablename}-STRUCT-ERROR\n日志:/home/cbd/etl/log/${target_tablename}.${current_time}.struct.log"
echo "${i}-target_tablename:${target_tablename}-STRUCT-ERROR"
CropID="" #填入企业ID值
Secret="" #填入认证密码
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
# 获取token
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
body='{
"touser": "'$touser'",
"msgtype": "text",
"agentid": "1000021",
"text":{
"content":"故障:'$msg' "
},
"safe":0
}'
/usr/bin/curl --data-ascii "$body" $PURL
exit 1
fi
echo "----------------------------id : ${i} | step 2 end | struct | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}------------------------------------------"
sleep 30
echo "----------------------------id : ${i} | step 3 start | data | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}-------------------------------------------"
current_time=`date +"%Y%m%d%H%M%S"`
/home/cbd/etl/bin/hadoop_jar.sh /home/cbd/etl/jar/etl-1.0-SNAPSHOT-jar-with-dependencies.jar com.ppdai.mapreduce.CommonAnalysisDataParquetRun $CURRENT_DATE 'day' $i > /home/cbd/etl/log/${target_tablename}.${current_time}.data.log 2>&1
RET_CODE=$?
if [ $RET_CODE -ne 0 ]
then
msg="${i}-target_tablename:${target_tablename}-DATA-ERROR\n日志:/home/cbd/etl/log/${target_tablename}.${current_time}.data.log"
echo "${i}-target_tablename:${target_tablename}-DATA-ERROR"
CropID="" #填入企业ID值
Secret="" #填入认证密码
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
# 获取token
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
body='{
"touser": "'$touser'",
"msgtype": "text",
"agentid": "1000021",
"text":{
"content":"故障:'$msg' "
},
"safe":0
}'
/usr/bin/curl --data-ascii "$body" $PURL
exit 1
fi
echo "----------------------------id : ${i} | step 3 end | data | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}----------------------------------------------"
sleep 30
echo "----------------------------id : ${i} | step 4 start | touch donefile | source_tablename : ${source_tablename} | target_tablename : ${target_tablename}-----------------------------------------"
#if [ $i -eq 18 -o $i -eq 21 ]
#if [ ${target_tablename} = 'edw.third_midloan_bairong' ]
#then
# touch /home/cbd/etl/done/edw.third_midloan_bairong_18.${today}.done
#else
# touch /home/cbd/etl/done/${target_tablename}.${today}.done
#fi
touch /home/cbd/etl/done/${target_tablename}.${today}.done
DONE_FILE=${DONE_HOME}/${target_tablename}.${today}.done
echo "checking done file ${DONE_FILE}"
while [ ! -f ${DONE_FILE} ]
do
target_tablename=`mysql -h${host} -P${port} -u${user} -p${password} -e"select targettablename from mysql.table_name where id = ${i};" | grep -o -E "[a-zA-Z_]{0,}\.[a-zA-Z0-9_]{0,}"`
touch /home/cbd/etl/done/${target_tablename}.${today}.done
done
echo "${DONE_FILE} found"
echo "----------------------------id : ${i} | step 4 end | touch donefile | source_tablename : ${source_tablename}-----------------------------------------"
}
id=`mysql -h${host} -P${port} -u${user} -p${password} -e"select id from mysql.table_name;" | grep -o -E "[0-9]{0,}"`
tmp_fifofile="/tmp/$$.fifo"
mkfifo $tmp_fifofile # 新建一个fifo类型的文件
exec 6<>$tmp_fifofile # 将fd6指向fifo类型
rm $tmp_fifofile #删也可以
thread_num=5 # 最大可同时执行线程数量
#根据线程总数量设置令牌个数
for ((i=0;i<${thread_num};i++));do
echo
done >&6
for i in ${id};do # 任务数量
# 一个read -u6命令执行一次,就从fd6中减去一个回车符,然后向下执行,
# fd6中没有回车符的时候,就停在这了,从而实现了线程数量控制
read -u6
#可以把具体的需要执行的命令封装成一个函数
{
hadoopjar $CURRENT_DATE $i
} &
echo >&6 # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u6减去的那个
done
wait
exec 6>&- # 关闭fd6
echo $CURRENT_DATE
exit 0
shell脚本并行调用jar包
于 2023-08-18 14:29:35 首次发布