export TZ=Asia/Shanghai
export HDP_VERSION='3.1.0.14-1'
export KRB5_CONFIG=/usr/java/default/jre/lib/security/krb5.conf
kinit -kt /u/users/$USER/.$USER.keytab $USER
start_date=$1
end_date=$2
sql_path=$3
log_path=$4
run_type=$5
if [ "$run_type" = "month" ]; then
while(( $start_date <= $end_date ))
do
start_current=`date "+%Y-%m-%d %H:%M:%S"`
start_timestamp=`date -d "$start_current" +%s`
m_start_date=`date -d "${start_date:0:4}${start_date:4:2}01" +%Y-%m-%d`
m_end_date=`date -d "$start_date next month" +%Y-%m-%d`
m_end_date=`date -d "${m_end_date:0:4}${m_end_date:5:2}01 -1 day" +%Y-%m-%d`
yestoday_int=`date -d "1 days ago" +%Y%m%d`
yestoday_str=`date -d "1 days ago" +%Y-%m-%d`
m_end_time=`date -d "$m_end_date" +"%Y%m%d"`
for(( i=1;i<4;i++)) do
if [ "$m_end_time" -gt "$yestoday_int" ]; then
m_end_date=$yestoday_str
echo "传入参数1:$m_start_date"
echo "传入参数2:$m_end_date"
echo "脚本运行开始时间:$start_current"
hive -hivevar start_date=$m_start_date -hivevar end_date=$m_end_date -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break 2
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
else
echo "传入参数1:$m_start_date"
echo "传入参数2:$m_end_date"
echo "脚本运行开始时间:$start_current"
hive -hivevar start_date=$m_start_date -hivevar end_date=$m_end_date -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
fi
done
start_date=`date -d " +1 day $m_end_date" +"%Y%m%d"`
done;
elif [ "$run_type" = "day" ]; then
while(( $start_date <= $end_date ))
do
start_current=`date "+%Y-%m-%d %H:%M:%S"`
start_timestamp=`date -d "$start_current" +%s`
yestoday_i=`date -d "1 days ago" +%Y%m%d`
yestoday_s=`date -d "1 days ago" +%Y-%m-%d`
for(( i=1;i<4;i++)) do
if [ "$start_date" -gt "$yestoday_i" ]; then
yestoday_str=$yestoday_s
echo "传入参数:$yestoday_str"
echo "脚本运行开始时间:$start_current"
hive -hivevar yestoday_str=$yestoday_str -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break 2
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
else
yestoday_str=$(date -d "$start_date" +%Y-%m-%d)
echo "传入参数:$yestoday_str"
echo "脚本运行开始时间:$start_current"
hive -hivevar yestoday_str=$yestoday_str -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
fi
done
start_date=$(date -d "$yestoday_str 1 day" +%Y%m%d)
done
elif [ "$run_type" = "halfMonth" ]; then
while(( $start_date <= $end_date ))
do
start_current=`date "+%Y-%m-%d %H:%M:%S"`
start_timestamp=`date -d "$start_current" +%s`
m_start_date=`date -d "$start_date" +%Y-%m-%d`
m_end_date=`date -d " +15 day $m_start_date" +%Y-%m-%d`
yestoday_int=`date -d "1 days ago" +%Y%m%d`
yestoday_str=`date -d "1 days ago" +%Y-%m-%d`
m_end_time=`date -d "$m_end_date" +"%Y%m%d"`
for(( i=1;i<4;i++)) do
if [ "$m_end_time" -gt "$yestoday_int" ]; then
m_end_date=$yestoday_str
echo "传入参数1:$m_start_date"
echo "传入参数2:$m_end_date"
echo "脚本运行开始时间:$start_current"
hive -hivevar start_date=$m_start_date -hivevar end_date=$m_end_date -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break 2
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
else
echo "传入参数1:$m_start_date"
echo "传入参数2:$m_end_date"
echo "脚本运行开始时间:$start_current"
hive -hivevar start_date=$m_start_date -hivevar end_date=$m_end_date -f $sql_path > $log_path 2>&1
ret=$?
if [ $ret -eq 0 ] ; then
end_current=`date "+%Y-%m-%d %H:%M:%S"`
end_timestamp=`date -d "$end_current" +%s`
run_time=`expr $end_timestamp - $start_timestamp`
echo "脚本运行结束时间:$end_current"
echo "脚本运行时间共:$run_time秒"
echo "执行成功,开始下一个周期"
break
fi
if [ $ret -gt 0 ] ; then
echo "执行失败,开始重试下一次"
continue
fi
fi
done
start_date=`date -d " +1 day $m_end_date" +"%Y%m%d"`
done;
fi
TZ='Asia/Shanghai'
export TZ
sql_file_1="dwd_item_sams_cbec_master_info.sql"
sql_file_2="dws_item_sams_cbec_sales_mid_di.sql"
current_date=$(date +%Y-%m-%d)
yestoday_str=$(date -d "${current_date} -1 day" +%Y-%m-%d)
while true
do
now=$(date +%H)
echo -e "现在是 $now 点"
if [ $now -ge 04 ]; then
hive -f $sql_file_1 -hivevar yestoday_str=$yestoday_str
if [ $? -eq 0 ];then
echo "$yestoday_str $sql_file_1 成功"
else
echo "$yestoday_str $sql_file_1 失败 重新回到while第一行运行"
continue
fi
hive -f $sql_file_2 -hivevar yestoday_str=$yestoday_str
if [ $? -eq 0 ];then
echo "$yestoday_str $sql_file_2 成功"
break
else
echo "$yestoday_str $sql_file_2 失败 重新回到while第一行运行"
continue
fi
else
echo "正在等待,60s 后继续检查当前时间。。。。。"
sleep 120
fi
done
exit 0