Passing value from the parent Job to the child Job

Passing value from the parent Job to the child Job
5 (100%) 5 votes

Passing a value from the parent Job to the child Job is a common real world requirement. I am going to describe different ways of passing values from parent job to child job using following different ways.

  • Passing context to child job.
  • Passing globalMap to child and get the result back in parent.
  • Get the result from child job to Parent.

Use Case 1: Pass a context variable and filter the data in child job.

We will use our previous data which we have used for  Data Validation Using tSchemaComplianceCheck demonstration.

Step 1: Create a two jobs and named it as parent job & child job respectively.

Step 2: Open child job and do the following configuration/settings.

  • Create context variable Id as string.
  • tFileInputDelimited (to read the data)
    • configure the basic property to read if not sure see the above post.
  • tMap ( to filter the data)
    • connect tMap with tFileInputDelimited using Main link.
    • Create output named as “filterData”
    • Add all the source columns from left side of tMap to recently created output. (drag & drop)
    • Click on “Activate filter expression and add below code.

row2.ID.equals(context.Id)

Your tMap setting looks like below image.

Child job tMap Settings.
Child job tMap Settings.
  • Add tLogRow  and connect with tMap using “filterData” output link.

Step 3: Now our child job is ready configure the Parent job as follows.

  • Create context variable PId as string with default value “2”.
  • Add tRunJob component and configure as follows
    • job =”childJob” ( you can browse and select job from repository.
    • version=”latest”
    • Context=”Default”
    • Context param
      • Parameters=Id
      • Values=”PId”
  • Final settings will be as shown in below image.
tRunJob setting
tRunJob setting

Step 4: Save and execute parent Job you will see below output on console as we are using tLogRow in child job.

Filter Result from Child Job
Filter Result from Child Job

Use Case 2: Get the result from child job to parent, using global variable.

Step 1: Open Child job and do the following changes.

  • Create gMap context variable as Object.
  • Add tJava component and use “OnSubJobOk” link to connect with tFileIputDelimited, then add the following code in it.

java.util.Map map =((java.util.Map)context.gMap);

context.Id=String.valueOf(map.get("Id"));

  • Remove tLogRow and add tJavaRow on same place, connect tJavaRow to tMap and don not click on synch button.
  • Add the following code in tJavaRow.

java.util.Map map =((java.util.Map)context.gMap);
map.put("Id",input_row.BirthDate);

Step 2: Open Parent job and do the following modifications.

  • Add tSetGlobalVar component and configured as follows.
    • Variables.
      • Key=”Id”
      • value=”2″

Parent Job design & tSetGlobalVar settings.

Complete Job with tSetGlobalVar setting
Complete Job with tSetGlobalVar setting
  • Use “OnComponentOk” trigger to connect with tRunJob.
  • Add tJava after tRunJob and connect using “OnSubJobOk” trigger with tRunJob component, add the following code in tJava.

System.out.println("Value From Child Job: "+globalMap.get("Id"));

Step 3: Save the parent job and execute it will show you following output.

Output from case two
Output from case two

Use Case 3: Get the result from child job to Parent.

previous use case we have seen, how to pass value to the child and get the result using globalMap variables, but this is not convenient way in terms of maintaining and manipulating keys, java code.

But there is sophisticated way to perform this task by using tBufferOutput component & tRunJob schema option.

Do the following changes in both the job.

Step 1: Open and modify child job as follows.

  • Remove tJava & tJavaRow component and add tBufferOutput at tJavaRow’s place.
  • Connect tBufferOutput with tMap using “filterData” output link and click on synch button.

Step 2: Do the following changes in Parent job.

  • Remove tSetGlobalVar & tJava component and add tLogRow at tJavas place.
  • In tRunJob setting do the following changes.
    • Click on “Copy child Job Schema”, it will show you schema copied from child job.
    • Context Param
      • Parameters=”Id”
      • Value=”2″
  • Add tlogRow and connect with tRunJob using Main Link.

Step 3: Save the Parent Job and execute it will display below output on console.

Result from child Job
Result from child Job

We have seen various ways of passing to and receiving values from child job. every way has it`s pros and cons but if you ask me I will prefer the way which is best suitable for my case.

About Umesh

I am Software consultant with approx 7 years of experience mainly in Business Intelligence and data warehousing assignments using Talend. Writing is not my passion but i am doing it to help others. if you have any special case where you want me to demonstrate then please post me.

4 comments on “Passing value from the parent Job to the child Job

  1. Hi Umesh,

    I have tried above example but I am facing one exception

    Exception in component tJava_1
    java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
    at gmt_live_jobs.childjob_0_1.ChildJob.tJava_1Process(ChildJob.java:384)
    at gmt_live_jobs.childjob_0_1.ChildJob.runJobInTOS(ChildJob.java:1428)
    at gmt_live_jobs.childjob_0_1.ChildJob.runJob(ChildJob.java:1284)
    at gmt_live_jobs.parentjob_0_1.ParentJob.tRunJob_1Process(ParentJob.java:540)
    at gmt_live_jobs.parentjob_0_1.ParentJob.tSetGlobalVar_1Process(ParentJob.java:396)
    at gmt_live_jobs.parentjob_0_1.ParentJob.runJobInTOS(ParentJob.java:921)
    at gmt_live_jobs.parentjob_0_1.ParentJob.main(ParentJob.java:774)
    Exception in component tRunJob_1
    java.lang.RuntimeException: Child job running failed
    at gmt_live_jobs.parentjob_0_1.ParentJob.tRunJob_1Process(ParentJob.java:562)
    at gmt_live_jobs.parentjob_0_1.ParentJob.tSetGlobalVar_1Process(ParentJob.java:396)
    at gmt_live_jobs.parentjob_0_1.ParentJob.runJobInTOS(ParentJob.java:921)
    at gmt_live_jobs.parentjob_0_1.ParentJob.main(ParentJob.java:774)

    can you please let me know what will be the issue.

Leave a Reply to tanaya Cancel reply

Your email address will not be published. Required fields are marked *