Db2 For I Parallel Data Load
Upcoming SlideShare
Loading in...5
×
 

Db2 For I Parallel Data Load

on

  • 1,307 views

Code Snippet External Stored Procedure

Code Snippet External Stored Procedure

Statistics

Views

Total Views
1,307
Slideshare-icon Views on SlideShare
1,304
Embed Views
3

Actions

Likes
0
Downloads
3
Comments
0

1 Embed 3

http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Db2 For I Parallel Data Load Db2 For I Parallel Data Load Document Transcript

    • DB2 for i External Stored Procedure ExampleRational for i and Optim Studio Development Environment ............................................. 1Load Process Flowchart (ACS_i5Load and ACSGenesis)................................................ 2Run Stored Procedure from Optim and Rational for i packages (client)............................ 3CPYFRMIMPF commands are submitted for batch processing on i ................................. 3Module syb_i5_LoadFromDelimitedFile in service program ACSGENESIS ................... 4DB2 External Stored Procedure Interface........................................................................... 6DB2 External Stored Procedure Program ........................................................................... 7Benefits of Stored Procedure Processing versus ODBC/JDBC statements........................ 8 Two Types of Stored Procedures .................................................................................... 8 Maintenance performed in one place.............................................................................. 8Get Job Completion Status of Stored Procedure with a Select Statement.......................... 9References......................................................................................................................... 10Rational for i and Optim Studio Development Environment • We would like to load many tables as fast as possible without locking our GUI for an extended timeframe. Page 1 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleLoad Process Flowchart (ACS_i5Load and ACSGenesis) Page 2 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleRun Stored Procedure from Optim and Rational for i packages (client)CPYFRMIMPF commands are submitted for batch processing on i Page 3 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleModule syb_i5_LoadFromDelimitedFile in service program ACSGENESIS // * ============================================================== * // * DB2 for i: Load from delimited files (syB) to ACS tables (db2) * // * ============================================================== *p syb_i5_LoadFromDelimitedFile...p b exportd syb_i5_LoadFromDelimitedFile...d pi nd i5_Context 32 constd i5_Working 128 constd i5_Schema 128 constd i5_BegPos s 5u 0 inz(0)d i5_EndPos s 5u 0 inz(0)d i5_LibFil s 20 inz( )d i5_CpyFil s 1024 inz( )d exist s 1 inz(0)d existIndicator s n based(sqlPointer)d sqlPointer s * inz(%ADDR(exist))d i5_LibNme s 10 inz( ) /Free Monitor; // * ============================================================== * // * Check / Set import application context and working directories * // * ============================================================== * If isApplicationWorkingDirectoryValid( %trim(i5_Context): %trim(i5_Working) ); i5_dirName = %trim(i5_Context) + / + %trim(i5_Working); If i5_Schema = QUSER; i5_LibNme = RtvCurLib( pgmStatus.jobName: pgmStatus.userId: pgmStatus.jobNumber ); Else; i5_LibNme = pgmStatus.userId; EndIf; i5_dirPtr = OpenDir(%trim(i5_dirName)); If (i5_dirPtr <>*NULL); DoU i5_dirEntPtr = *Null; i5_dirEntPtr = ReadDir( i5_dirPtr ); Monitor; On-Error *All; EndMon; If i5_dirEntPtr = *Null; Leave; Else; i5_entryName = %str(%addr(i5_nameFull)); // * ============================================================== * // * Filter navigation entries (.) and select .data delimited files * // * ============================================================== * If %subst(i5_entryName: 1: 1) <> .; i5_EndPos = %scan(.data: %xlate(upper: lower: %trim(i5_entryName)): 1); If i5_EndPos <> 0; i5_BegPos = %scan(.: %xlate(upper: lower: %subst(%trim(i5_entryName): 1: i5_EndPos - 1)): 1); Page 4 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure Example// * ============================================================== *// * i5 internal system object name from DB2 schema catalog entries *// * ============================================================== * i5_LibFil = syb_i5_getTableName(%trim(i5_LibNme): %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1))); If i5_LibFil <> ;// * ============================================================== *// * Construct i5 CpyFrmImpF (Copy From Import File) command string *// * ============================================================== * i5_CpyFil = CPYFRMIMPF FROMSTMF( + ticMark + %trim(i5_Context) + / + %trim(i5_working) + / + %trim(i5_entryName) + ticMark + ) TOFILE( + %subst(i5_LibFil: 1: 10) + / + %subst(i5_LibFil: 11: 10) + ) MBROPT(*REPLACE) RCDDLM(*CRLF + ) DTAFMT(*DLM) STRDLM( + ticMark + " + ticMark + ) RMVBLANK(*TRAILING + ) FLDDLM( + ticMark + , + ticMark + );// * ============================================================== *// * Remove joblog messages, prepare for submit job status capture *// * ============================================================== * RemoveMessage(rmv_PgmQue: rmv_StkCnt: rmv_MsgKey: rmv_Remove: error_Code);// * ============================================================== *// * Construct i5 SbmJob (Submit Job) command (CpyFrmImpF) queuing *// * ============================================================== * i5_CmdStr = SBMJOB CMD( + %trim(i5_CpyFil) + ) INLLIBL(*CURRENT) JOBD(*USRPRF) JOBQ(*JOBD + ) JOB( + %trim(%subst(i5_LibFil: 11: 10)) + ) MSGQ(*LIBL/ACS_GEN500) HOLD(*YES) PRTTXT( + ticMark + %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1)) + ticMark + ); CallP(e) i5_RunCmd(%trim(i5_CmdStr): %len(%trim(i5_CmdStr))); If %Error; snd_Data = LOAD FAILURE: + %trim(i5_entryName) + in + %trim(pgmStatus.mainProcName); Else; snd_Data = LOAD SUCCESS: + %trim(i5_entryName) + in + %trim(pgmStatus.mainProcName);// * ============================================================== *// * Receive joblog messages prepare for submit job status capture *// * ============================================================== * ReceiveMessage(MessageInfo :%size(MessageInfo) :RCVM0100 :* :0 :*ANY :MsgKey :0 :*OLD :error_Code); If i5_ByteProv = 0; If messageInfo.common.QMHMI03 = CPC1221; i5_JobNme = %subst(messageInfo.errMsgDta: 1: 10); i5_UsrNme = %subst(messageInfo.errMsgDta: 11: 10); Page 5 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure Example i5_JobNbr = %subst(messageInfo.errMsgDta: 21: 6); i5_UsrDta = %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1)); If Not WrtLogSts(ACSLOAD_01: i5_TskCnt: i5_JobNme: i5_UsrNme: i5_JobNbr: i5_UsrDta); // Add additional error handling logic here ======> EndIf; EndIf; Else; // Add additional error handling logic here ======> EndIf; EndIf; EndIf; Else; snd_Data = LOAD WARNING: + %trim(i5_entryName) + in + %trim(pgmStatus.mainProcName); EndIf; CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndIf; EndIf; EndDo; existIndicator = *ON; // Return SUCCESS code Else; i5_errNoPtr = errno(); snd_Data = Open directory failed from + %trim(i5_dirName) + in program + %trim(pgmStatus.mainProcName); CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndIf; closeDir(i5_dirPtr); EndIf; On-Error *All; snd_Data = Unexpected error in syb_i5_loadFromDelimitedFile() + in program + %trim(pgmStatus.mainProcName); CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndMon; Return existIndicator; /End-Freep syb_i5_LoadFromDelimitedFile...p eDB2 External Stored Procedure Interface Create Procedure i5ParallelDataLoader ( IN Type char( 10) ) LANGUAGE RPGLE SPECIFIC ACS_I5LOAD NOT DETERMINISTIC External Name ACS_I5LOAD CALLED ON NULL INPUT PARAMETER STYLE DB2SQL; Page 6 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleDB2 External Stored Procedure Program* ================================================================== ** Program name...: ACS_i5LOAD ** Purpose........: Load database tables from delimited files ** Programmer.....: Thomas and Victoria Wolfe ** Create Date....: December 7, 2010 ** ** Maintenance Log: ** ** PGMR SEARCH DATE DESC. OF CHANGE ** ** ================================================================== * /COPY qCPYSRC,ACS_H_PGM /COPY QPRTSRC,ACSGENESISd i5_LoadType s 10d i5_NullInd1 s 5i 0d sqlStateCode s 5d functionName s 517d specificName s 128d errorMessage s 70d i5_ProdLoad s 10 inz( )d message s 80 inz(*Blank)d command s 2048 inz( ) * ================================================================== * * Common program status information data structure (SDS) * * ================================================================== * /COPY QCPYSRC,ACS_SQLOPT * ================================================================== * * i5 DB2 Parallel Data Loader: Submit CpyFrmImpF command to batch * * ================================================================== *c *entry plistc parm i5_LoadTypec parm i5_NullInd1c parm sqlStateCodec parm functionNamec parm specificNamec parm errorMessage /Free Monitor; // Global error handler sqlStateCode = 00000; If i5_NullInd1 = 0; i5_ProdLoad = i5_LoadType; Select; When %xlate(upper: lower: %trim(i5_ProdLoad)) = *sybase; If syb_i5_LoadFromDelimitedFile(%trim(syB_Context): Page 7 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure Example %trim(syB_dataDump): %trim(pgmStatus.UserId) ); Else; sqlStateCode = 38999; errorMessage = Error occured in external stored + procedure. Contact support and report + this error message.; EndIf; Other; sqlStateCode = 38001; errorMessage = Invalid input load parameter + %trim(i5_ProdLoad) + not processed.; EndSL; Else; sqlStateCode = 38001; errorMessage = Null input load parameter not + processed.; EndIf; On-Error *All; // Unmonitored Errors? Dump(a); EndMon; *inlr = *On; Return;/End-FreeBenefits of Stored Procedure Processing versus ODBC/JDBC statements • Secure access to DB2 data. • Access non-DB2 data if necessary –VSAM, IMS, etc. • Code business logic once o Maintenance is simpler • Performance improvement? o Instead of multiple SQL statements being sent • Lots of languages to choose fromTwo Types of Stored Procedures • SQL Stored Procedures • External ProceduresMaintenance performed in one place Page 8 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleGet Job Completion Status of Stored Procedure with a Select StatementSelect PROCESS, PRCSEQ, Cast(Case When STATUS = N Then Success When STATUS = ‘A’ Then ‘Failure’ Else Running’ End As Character(7)) As Status, STARTSTMP, ENDSTMP, RUNNAME, RUNUSER, RUNNUMBER, ERRORID, USRDATA From ACSJOBLOG Order by 4,1,2 Page 9 of 10 1/3/2011 4:49:44 PM
    • DB2 for i External Stored Procedure ExampleReferencesWriting Stored Procedures in RPG MC Press Online 15 January 2008, Susan Gantnerhttp://www.mcpressonline.com/programming/rpg/writing-stored-procedures-in-rpg.htmlStored Procedures, Triggers, and User-Defined Functions on DB2 UDB for iSerieshttp://www.redbooks.ibm.com/abstracts/sg246503.htmlDB2 UDB for AS/400 Object Relational Supporthttp://www.redbooks.ibm.com/abstracts/sg245409.htmlCross-Platform DB2 Stored Procedures: Building and Debugginghttp://www.redbooks.ibm.com/redbooks/pdfs/sg245485.pdfDB2 9 for z/OS Stored Procedures: Through the Call and Beyondhttp://www.redbooks.ibm.com/abstracts/sg247604.htmlDB2 Java Stored Procedures: Learning by Examplehttp://www.redbooks.ibm.com/redbooks/pdfs/sg245945.pdfModernizing IBM eServer iSeries Application Data Access – A Roadmap Cornerstonehttp://www.redbooks.ibm.com/abstracts/sg246393.htmlImproving Stored Procedure Performance 18 December 2009, Kent Milligan, JarekMiszczyk, and Gene Cobb http://tinyurl.com/2ayskqoThe API Corner MC Press online: Automating Recovery (or Keeping the Help Desk Outof the Loop) 21 July 2010 – October 2010, Bruce Vining http://tinyurl.com/32gkc9h Page 10 of 10 1/3/2011 4:49:44 PM