Datastage中使用Routine程序执行多条shell指令
2014-08-01 15:38
465 查看
在使用Datastage Parallel开发事实表的过程中,经常会出现因为脏数据、重置某日的日全量数据,而需要重跑数据的情况。这就需要在run job之前首先调用数据库的命令,清除不再需要的数据,以保证后续加载程序的成功运行。如果是单张表的操作,可以直接在前置子进程中,调用shell命令。如果是一个job操作多张表的情况,可以通过开sequential job,通过execute command控件执行。
为了避免开发额外的sequential job,可以通过routine程序多次调用不同sql command.
我使用的环境是这样的,有开发好的call db command的shell程序,只需要在routing中多次调用。同时DB的连接参数是存在DS的参数集里面的。基于以上的条件开发routine程序
*************************************************************************
**ROUTINE名称:DSJCallDBCommand
**ROUTINE类型:Server Subroutine
**ROUTINE功能:执行多条db2命令
**ROUTINE作者:Mike Peng
**ROUTINE日期:20140801
**注意事项:
** 1.避免对同一张表的同时操作
** 2.参数使用";"作为分隔符,且末尾需要加";"
** 3.参数使用方法 db_instance;db_user;db_password;"SQL Statement1";"SQL Statement2";....;
*************************************************************************
*$INCLUDE DSINCLUDE JOBCONTROL.H
ErrorCode = 0 ; * 将该项设置为非零值可停止阶段/作业
delimeter=";" ; * 设置参数分隔符
Call DSLogInfo(InputArg,"DSJCallDBCommand")
if InputArg="" then
Ans="Params should not be empty!"
Call DSLogWarn(Ans,"DSJCallDBCommand")
* ErrorCode = 1
end
else
trimDelimeter=EReplace(InputArg, ";", "")
paramcnt= Len(InputArg)-Len(trimDelimeter) ; *计算参数个数
statementcnt=paramcnt - 3
message="此前置程序需要执行":statementcnt:"个SQL."
Call DSLogInfo(message,"DSJCallDBCommand")
cmdstr="sh /etldata/script/shellscript/call_db2_cmd.sh "
num=1
Loop
While num <= 3 do
if num=1 then
start = 0
end
else
start=index(InputArg,delimeter,num-1)
end
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstr:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
num=num+1
repeat
cmdstrtmp=cmdstr
num=4
Loop
While num <= paramcnt do
start=index(InputArg,delimeter,num-1)
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstrtmp:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
statementN0= num-3
message1="Prepared SQL_":statementN0:cmdstr
message2="Excuting SQL_":statementN0:cmdstr
Call DSLogInfo(message1,"DSJCallDBCommand")
Call DSExecute("UNIX",cmdstr,Ans,SystemReturnCode)
Call DSLogInfo(message2,"DSJCallDBCommand")
num=num+1
REPEAT
END
调用此进程是,参数输入为:#PARAMS_DB.$DB_SID#;#PARAMS_DB.$DBUSER#;#PARAMS_DB.$DBPWD#;"SQL Statement1";"SQL Statement2";
图例为2个sql 语句,可以在参数后端继续添加sql命令
为了避免开发额外的sequential job,可以通过routine程序多次调用不同sql command.
我使用的环境是这样的,有开发好的call db command的shell程序,只需要在routing中多次调用。同时DB的连接参数是存在DS的参数集里面的。基于以上的条件开发routine程序
*************************************************************************
**ROUTINE名称:DSJCallDBCommand
**ROUTINE类型:Server Subroutine
**ROUTINE功能:执行多条db2命令
**ROUTINE作者:Mike Peng
**ROUTINE日期:20140801
**注意事项:
** 1.避免对同一张表的同时操作
** 2.参数使用";"作为分隔符,且末尾需要加";"
** 3.参数使用方法 db_instance;db_user;db_password;"SQL Statement1";"SQL Statement2";....;
*************************************************************************
*$INCLUDE DSINCLUDE JOBCONTROL.H
ErrorCode = 0 ; * 将该项设置为非零值可停止阶段/作业
delimeter=";" ; * 设置参数分隔符
Call DSLogInfo(InputArg,"DSJCallDBCommand")
if InputArg="" then
Ans="Params should not be empty!"
Call DSLogWarn(Ans,"DSJCallDBCommand")
* ErrorCode = 1
end
else
trimDelimeter=EReplace(InputArg, ";", "")
paramcnt= Len(InputArg)-Len(trimDelimeter) ; *计算参数个数
statementcnt=paramcnt - 3
message="此前置程序需要执行":statementcnt:"个SQL."
Call DSLogInfo(message,"DSJCallDBCommand")
cmdstr="sh /etldata/script/shellscript/call_db2_cmd.sh "
num=1
Loop
While num <= 3 do
if num=1 then
start = 0
end
else
start=index(InputArg,delimeter,num-1)
end
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstr:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
num=num+1
repeat
cmdstrtmp=cmdstr
num=4
Loop
While num <= paramcnt do
start=index(InputArg,delimeter,num-1)
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstrtmp:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
statementN0= num-3
message1="Prepared SQL_":statementN0:cmdstr
message2="Excuting SQL_":statementN0:cmdstr
Call DSLogInfo(message1,"DSJCallDBCommand")
Call DSExecute("UNIX",cmdstr,Ans,SystemReturnCode)
Call DSLogInfo(message2,"DSJCallDBCommand")
num=num+1
REPEAT
END
调用此进程是,参数输入为:#PARAMS_DB.$DB_SID#;#PARAMS_DB.$DBUSER#;#PARAMS_DB.$DBPWD#;"SQL Statement1";"SQL Statement2";
图例为2个sql 语句,可以在参数后端继续添加sql命令
相关文章推荐
- 转变思维--使用Python生成Shell命令,批量执行程序
- 使用Python生成Shell命令,批量执行程序代码解析
- shell脚本使用ssh命令连接主机,自动创建目录,分发程序,执行命令
- Linux下使用Shell命令控制程序后台执行
- 使用JVMTI获取Java多线程程序指令执行次序
- linux 使用 java 程序执行 shell命令
- 使用命令shell和MyEclipse两种方式执行Hadoop的WordCount程序
- shell程序之逐行读取一文件中的参数且使用此参数每次执行5分钟
- OC---第一个OC程序(使用GNUstep的shell编译执行)
- 使用Jmeter对Mysql进行压力测试无法执行多条sql语句问题
- 初始化 Microsoft Visual SourceSafe 源代码管理提供程序时失败。您无法使用此提供程序执行源代码管理操作
- 使用关于程序定时执行 cron
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- 使用脚本引擎增加程序运行时动态执行能力(Java篇)
- ASP.NET下使用WScript.Shell执行命令
- 超级终端:另一个程序正在使用选定的电话服务设备,请在其他程序执行完后再试一次.
- 程序中如何使用代码执行Insert操作
- 使用VC创建进程和执行命令行程序的方法
- 初始化 Microsoft Visual SourceSafe 源代码管理提供程序时失败。您无法使用此提供程序执行源代码管理操作。”
- ASP.NET下使用WScript.Shell执行命令