6/5/13

Using parameters with Talend Open Studio

Using parameters with Talend Open Studio

Passing parameters to Talend Open Studio is a usefull and common solution to parametrize a job's execution. When you realize how to use Context parameters in Talend Open Studio it becomes an easy task.

Talend Open Studio Context

1) First launch Talend Open Studio and open an existing project or create a new one.
Talend Open Studio project creation

2) Next press the right mouse button over Contexts in the Repository tab and select Create context group from the menu. In Talend you can create as many Contexts as you want and choose wich one to use during execution. Most of the times, as in this tutorial, you'll use only one Context.
Talend Open Studio Context
3) Give a name to the new context, as example "Default", and press Next
Talend Open Studio create context
4) The next window is used to define the parameters in the Context. To add a new parameter press the plus button and give a name to the new item. You can also define a parameter type and write a mnemonic Comment.
Talend Open Studio add parameters
5) Now you can assign a default value for each parameter, so they can be used in Talend's jobs. Press on "Values as table" tab and assign the values you want. In this example ParamA has "Test!" as default value while ParamB has "Value!". You can change parameters values every time you want, using the Talend Open Studio's wizard (Right click on Context item -> Edit Context group ->Next) or setting parameter directly at runtime (we'll see how to in the next paragraph).
Talend Open Studio edit parameters
Press Finish to close the wizard and apply the changes you made.
 

Using Context parameters in a Talend's Job

Talend Open Studio is now ready to assign Context parameters to a Job. For this example we create a new Job (Job Designs -> right click -> Create job), but you can always choose an existing one. Once a Job is open select the Contexts tab and click on the button pointed by the cursor in this picture.
Talend Open Studio apply context
In the form that appears you can select which parameters to make avilable in the job. For example select all the values.
Talend Open Studio select variables
The context variables you checked are now passed to the Talend's job. Context parameters can be used in many components, for example in query component or datasource components to parametrize db connections and query executions. To apply a parameter to a component's field simply press Ctrl+Space. Talend will suggest you which parameters are available for the current job.
For example put a tWarn component from the Palette into the Job editor. Then put a tLogCatcher and link it to a new tLogRow component (right click on tLogCatcher -> Row -> Main and point the cursor to tLogRow).
Now select tWarn component and choose the Component tab. As you can see in the picture you can fill component value with parameters and Ctrl+Space key help you much in this task.
Talend Open Studio assign parameter to a component

Now when you run the Job the parameters will be replaced by Talend with the values you have assigned. This is really usefull when you need to parametrize database connections or other procedures.

Assign parameteres values in command line

Parameters are really usefull when you compile your Talend Job and make it executable. To compile a job in Talend Open Studio simply right click on job name and select Export Job Scripts.
Talend Open Studio export job scripts
In the next form make sure the Context scripts field is checked and choose a path where saving the archive ("To archive file" form field).
Talend Open Studio export job settings
Talend compiles the job into a Java (or Perl) application. To run the application first extract the archive created by Export job scripts procedure and then run the command JobTest/JobTest_run.bat (or JobTest_run.sh in Linux).
The most important thing to know is that you can pass a runtime parameter at the application using --context_param command line switch. For example
JobTest_run.bat --context_param ParamA=ValueA --context_param ParamB=ValueB
You can also edit JobTest_run.bat and set the param value directly into the script:
java -Xms256M -Xmx1024M -cp classpath.jar; test2.jobtest_0_1.JobTest --context=Default
 --context_param ParamA=ValueA --context_param ParamB=ValueB

Passing parameters to sub-jobs

Sometimes in your applications a Job calls another Job and so on. When you use parameters you need a method to transmit parameters to sub-jobs. In Talend Open Studio this task is quite easy. When you create a sub job (that is a job who calls another job) simply click its Component tab and make sure Transmit whole context is checked.
Talend Open Studio Transmit parameters sub jobs

 Next time you run or compile the main Job all parameter values will be transmitted to the depending sub Jobs.

No comments: