em......
话不多少直接上代码(通过shell执行hsql脚本),可以直接执行的
#!/bin/bash
#DWD目标表
hive_dwd_table=XXX
echo $hive_dwd_table ;
#ODS源表
hive_ods_table=XXX
echo $hive_ods_table;
#日志信息表
hive_log_table=XXX
echo $hive_log_table;
#判读是否传入日期参数,如果传入则使用传入的日期,没有则使用$date_time
if [ ! -n "$1" ] ;then
date_time=$(date +%Y%m%d)
else
date_time=$1
fi
echo " 输出开始时间到日志"
start_time=$(date "+%Y-%m-%d %H:%M")
echo $date_time
hive -e "alter TABLE $hive_dwd_table add if not exists partition (etl_time_field=${date_time});";
hive -e " INSERT overwrite TABLE $hive_dwd_table partition (etl_time_field=${date_time})
select
mc mc,
qx qx,
tjsj tjsj,
'ods_xcb' data_source,
from_unixtime(unix_timestamp(),'YYYY-MM-dd HH:mm:ss') INSERT_DATE,
ETL_FLAG_FIELD ETL_FLAG_FIELD
from $hive_ods_table where
concat(substr(etl_time_field,1,4),substr(etl_time_field,6,2),substr(etl_time_field,9,2))=$date_time ;";
#判断上面的代码是否成功 $?:返回上一条命令的执行状态,0为正常,1表时异常
if [ $? -ne 0 ]; then
status_job="执行失败"
echo " 执行失败"
echo "====================================开始输出日志信息======================================="
#输出结束时间到日志中
end_time=$(date "+%Y-%m-%d %H:%M")
echo $start_time,$end_time,$hive_dwd_table,$hive_ods_table,$status_job;
#脚本插入mysql日志数据
mysqlhost=`sed -n '1p' ../../native_lib/mysql_conn_info`
mysqlport=`sed -n '2p' ../../native_lib/mysql_conn_info`
mysqluser=`sed -n '3p' ../../native_lib/mysql_conn_info`
mysqlpw=`sed -n '4p' ../../native_lib/mysql_conn_info`
#访问目标数据库
databaseName=dim_common
sqoop eval --connect 'jdbc:mysql://'${mysqlhost}':'${mysqlport}'/'${databaseName} \
--username ${mysqluser} \
--password ${mysqlpw}\
--query "insert into $hive_log_table (job_name,target_table,source_table,start_time,end_time,status,expmessage,data_size) VALUES('${hive_dwd_table}','${hive_dwd_table}','${hive_ods_table}','${start_time}','${end_time}','$status_job','null','null');"
code=$?
if [ $code != "0" ];then
echo "插入日志失败"
exit 1
else
echo "插入日志成功"
fi
exit 1
else
status_job="执行成功"
echo " 执行成功"
echo "====================================开始输出日志信息======================================="
#输出结束时间到日志中
end_time=$(date "+%Y-%m-%d %H:%M")
echo $start_time,$end_time,$hive_dwd_table,$hive_ods_table,$status_job;
#脚本插入mysql日志数据
mysqlhost=`sed -n '1p' ../../native_lib/mysql_conn_info`
mysqlport=`sed -n '2p' ../../native_lib/mysql_conn_info`
mysqluser=`sed -n '3p' ../../native_lib/mysql_conn_info`
mysqlpw=`sed -n '4p' ../../native_lib/mysql_conn_info`
#访问目标数据库
databaseName=dim_common
sqoop eval --connect 'jdbc:mysql://'${mysqlhost}':'${mysqlport}'/'${databaseName} \
--username ${mysqluser} \
--password ${mysqlpw}\
--query "insert into $hive_log_table (job_name,target_table,source_table,start_time,end_time,status,expmessage,data_size) VALUES('${hive_dwd_table}','${hive_dwd_table}','${hive_ods_table}','${start_time}','${end_time}','$status_job','null','null');"
code=$?
if [ $code != "0" ];then
echo "插入日志失败"
exit 1
else
echo "插入日志成功"
fi
exit 0
fi