Datastage Routine示例——使用Routine提取JOB错误信息录入数据库

环境:Red Hat Linux AS 4 + Datastage EE 7.5.2 + Oracle 10g

##在此感谢一下金证科技的Li Bin和Wang Jun,是他们带我学习Datastage的!

目标:使用Routine将JOB运行后的错误信息录入数据库(暂时忽略警告信息)

方案:Server Routine调用Shell脚本,Shell脚本调用存储过程,存储过程把传递的参数信息写入数据库表

实现过程:
1.创建Server Routine,命名DSJobLog,系统默认DSJ开头的Routine为内置(Built-in)Routine,类型为Before/After Subroutine

2.编写Routine的Code,代码及注释如下:

*************************************************************************
**ROUTINE名称:DSJobLog                                                   
**ROUTINE类型:Server Subroutine                                      
**ROUTINE功能:提取JOB运行后的错误日志并写入数据库    
**ROUTINE输入参数1:InputArg                                          
**ROUTINE返回参数1:ErrorCode                                         
**ROUTINE作者:wangfei ()        
**ROUTINE日期:20100623                                               
*************************************************************************

$INCLUDE DSINCLUDE JOBCONTROL.H

*设定返回值
ErrorCode = 0

*设定当前运行的JOB
JobHandle = DSJ.ME

*设定变量cmdstr,存储shell命令及参数
cmdstr=" sh /home/dsadm/EDCETL/SH/PARA/etl_log.sh "

* 如果状态为失败,则产生错误日志

*设定变量LogList并初始化,存储JobLog信息
   LogList =""

*判断运行subroutine前JOB的执行状态(DSJ.JOBINTERIMSTATUS)
*如果产生错误(代码为3),则将error信息存储于动态数组Summary中(1为INFO,2为WARN,3为FATAL)
   if DSGetJobInfo(JobHandle,DSJ.JOBINTERIMSTATUS) = 3 then
      Summary=DSGetLogSummary(JobHandle , DSJ.LOGFATAL, DSGetJobInfo(JobHandle,DSJ.JOBSTARTTIMESTAMP),
                             DSGetJobInfo(JobHandle,DSJ.JOBLASTTIMESTAMP), 10)

*通过循环先取到错误事件的ID(EvtID),然后依据EvtID获得错误信息
   *循环开始
    FOR I=1 TO 10
        EvtID = Field(Summary<I>,'\',1)
        if EvtID = "" then
           exit
        end
       
        LogList =LogList : DSGetLogEntry(JobHandle ,EvtID):"ZhongTu"
    NEXT I
    *循环结束
   end
*如果状态为失败,则产生错误日志

*开始拼接shell命令的参数
*Char(34)在VB中是双引号,这里必须添加双引号以便Shell脚本能够正确的把带单引号的参数传递给存储过程
cmdstr=cmdstr:Char(34) :"'":DSGetJobInfo(JobHandle,DSJ.JOBNAME):"'":Char(34):" "
cmdstr=cmdstr:Char(34) :"'":DSGetJobInfo(JobHandle,DSJ.JOBSTARTTIMESTAMP):"'":Char(34):" "
cmdstr=cmdstr:Char(34) :"'":DSGetJobInfo(JobHandle,DSJ.JOBLASTTIMESTAMP):"'":Char(34):" "
cmdstr=cmdstr:Char(34) :"'":DSGetJobInfo(JobHandle,DSJ.JOBINTERIMSTATUS):"'":Char(34):" "
cmdstr=cmdstr:"1":" "
cmdstr=cmdstr:Char(34) :"'":LogList:"'":Char(34)
*结束拼接shell命令的参数

*执行Shell脚本
Call DSExecute("UNIX", cmdstr, Ans, SystemReturnCode)

3.其他标签页的内容默认即可,保存并编译!

4.在Job中调用该Routine,打开一个JOB,在JOB属性中选择Routine即可。


5.Linux系统下编写Shell脚本,命名etl_log.sh,脚本代码如下:

###******************************************************************
##
## 程序名:    etl_log.sh
## 执行环境: /home/dsadm/EDCETL/SH目录下
## 程序描述: 调用存储过程将JOB日志信息写到数据库中
## 输入参数:
## 输出参数:
## 编写人员: wangfei()
## 创建日期: 2010.06.24
## 代码版本: V1.0
##
###******************************************************************

if [ $# -ne 6 ]
   then
   exit 2
fi

#参数文件、日志文件、临时文件存放路径
PARAPATH=/home/dsadm/EDCETL/SH/PARA


#参数文件
ETLCFG=$PARAPATH/etl_configure.cfg

#获取控制ETL运行的数据库服务器信息
DBNAME=$(awk -F= '/^DBNAME_CTL/{print $2}' $ETLCFG)
DBUSR=$(awk -F= '/^DBUSR_CTL/{print $2}' $ETLCFG)
DBPWD=$(awk -F= '/^DBPWD_CTL/{print $2}' $ETLCFG)

VALUE=`sqlplus -s } <<END
     set pagesize 0 feedback off verify off heading off echo off
     call SP_GEN_ETL_LOG($1,$2,$3,$4,$5,$6);
     exit;
     END`
echo "$VALUE"
exit 0
###-------------------EOF-------------------####

6.在Oracle中编写存储过程SP_GEN_ETL_LOG,将错误信息写入数据库,在此省略存储过程代码!

-----------------------------------------20100625---BJ---EOF---------------------------------------------------


Logo

更多推荐