Returning Codes and Passing Parameters to a Child Job
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
- Create a Job and name it childJob. It should look as in the following screenshot.
- To create the first subjob, add a tFileInputDelimited in the design workspace, to read data from a text file and a tLogRow to print it in the console.
- To create the second subjob, add a tFixedFlowInput in the design workspace, to populate the total number of rows read by the tFileInputDelimited and buffer it with a tBufferOutput.
- Configure the tFileInputDelimited as in the following screenshot.
- Configure the tFixedFlowInput as in the following screenshot.
- Configure the tBufferOutput schema as follows.
- Create another Job named parentJob. The Job should look as in the following screenshot.
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.
- Configure the tJavaRow as follows.
- Type in the following code in the Code box:
Execute the Jobs
Execute the parentJob . The results is printed in the console:
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
- Open task_1 and replace the tDie component with a tFixedFlowInput component.
- Add an Integer column to the tFixedFlowInput component called returnCode
- Set the value to 4.
- Add a tBufferOutput component and add a flow from the tFixedFlowInput component to it.
Capturing and storing the return code in the parent
- Return to the parent job.
- Add a tJavaRow component to the job.
- Create a flow from the tRunJob component for task 1 to the tJavaRow
- Open the tRunJob component, and click on Copy Child JobSchema, as shown in the next screenshot:
- Open the tJavaRow component, and insert the following code:
- Change the tJava component and the two If links to use returnCode instead of
- Run the job.
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.