Returning a value from a child Job to the parent Job
You may need to return a value or rows from a child Job to the parent Job for certain use. Let us see the best practice of how to pass a value with a tBufferOutput component from a child Job to the parent Job.
Here, you will create two Jobs: a parent Job and a child Job. The child Job will read data from a text file, populate the total number of rows and pass the rows to the parent Job.
The input file, named in.txt, is as follows:
id;name 1;Shong 2;Elise 3;Sabrina
Create demo Jobs
This parentJob is created to call the childJob Job with a tRunJob component, get the value buffered by tBufferOutput in the child Job and pass it to a tJavaRow.
System.out.println("The total number of rows is:"+input_row.nb_line);
Execute the Jobs
Execute the parentJob . The results is printed in the console:
Starting job parentJob at 14:37 16/06/2013. [statistics] connecting to socket on port 3801 [statistics] connected 1|Shong 2|Elise 3|Sabrina The total number of rows is:3 [statistics] disconnected Job parentJob ended at 14:37 16/06/2013. [exit code=0]
Returning codes from a child job without tDie
In this recipe, we will see how RETURN CODES can be set in a child job and used in a parent, without having to kill the child process.
Open the job jo_cook_ch11_0060_childReturnCodesNoDie. This job is the end state of the previous recipe.
How to do it…
The first thing we need to do is add the return code value to a buffer for the parent job to pick up.
Buffering the return code
Capturing and storing the return code in the parent
How it works…
The tBufferOut component is used to transmit data from a child job to a parent, and we can easily access the schema of the child job (as set in the tBufferOut component) using the Copy Child Job Schema option in the tRunJob component.
When the parent picks up the code, it stores it in a globalMap variable for use by the If conditions.
Passing parameters to a child job
Talend provides us a very good way of inter-component communication using parameters. We can easily pass the context parameters from one job to another. This feature is very handy as we can create all the parameters in the main job and as per requirement, pass the values of parameters to the sub jobs.
First, create a child job :
1. Define three context variables and do not provide any default values.
2. Drag the tJava component and print the values of the context variables.
3. Execute the child Job.
As we have not provided any values to context variables in child job, It does not display any values in the output.
Now, it’s time to create the Main job and pass the values to the subjob.
4. Create a Main job.
Using tFileInputDelimited component, read the delimited Input file and Drag the child job to the Job designer.
5. Open the settings of the child subjob and pass the values of the context variables defined in child job. Take the values of the fields from row2 link and pass it to context variables.
6. Main job is complete. It’s time to execute it.
Free Demo for Corporate & Online Trainings.