Understanding Context Variables in Talend
Talend is having the concept of Context Variables; we can access them anywhere in our jobs once it is created. These context variables are as same as global variables in a typical Java Application. Depending on the circumstances the Job is being used in, we might want to manage it differently for various execution types (Production and Testing). These context parameters are mostly context-sensitive variables which will be added to the list of variables available for reuse in the component-specific properties on the Component view through the Ctrl+Space bar keystrokes.
In Talend Open Studio, we can create context groups those will be hold various variables; for each context groups we can define various execution types like production, development, testing, etc. These variables will be useful when we switch our ETL jobs from one environment to another.
It is always a best practice to create context variables using context groups; this is the way we can manage and keep our contexts more consistent and manageable across all of the jobs we are going to develop in Talend Studio.
To create new context variables, right click on the Contexts and click on Create context group as described in figure 1.
Steps to create context variables
There are two ways to create context variables in Talend studio. We can create variables from context groups and another way is to export context variables from the existing database connections metadata.
Create variables from Context Group
Step 1: As soon as we click on the create context group link, the screen will ask us to enter the context related information as described in figure 2.
Step 2: This is going to be next step in creating contexts as in figure 3. Here we are about to define the contexts, variables and its values.
There is a + button on top right corner of the page which will be used to configure contexts. When we click on the button, Configure Contexts screen will be appearing as in the figure 4.
Here we can create contexts as per the requirements on our jobs. Clicking on the ‘New…’ button will lead us to create new contexts. Once the contexts are defined we can move ahead to create variables in it. For example, the below screens are shown to create contexts and its variables for the development environment.
Once the context is created, the next step will be creating variables and assign values for those variables in context section. The + button on the bottom left corner in figure 3 will help us to create new variables. In the values section, we can provide the values for the respective variables which is going to be used across all our jobs.
When we create variables, we have to be aware of defining the Type for those variables. There are numerous types available in Talend such as Boolean, Short, Long, Integer, Date, String, Password, etc. Based on the type of variables, we can carefully choose the Type for them. Once all of the variables are created and values are assigned to them, the screen will as same as in figure 5 to click Finish button to complete the creation section.
We can create new context ‘Production’ as same as the steps followed for development environment. The result will be like below in figure 6.
Auto Generation of Context Variables
Here is the another way to create context variables from metadata settings. While setting up the metadata connection, we can easily generate the contexts, variables and its values automatically. We can auto generate the contexts and its members from already defined metadata entries in Talend.
Once you open the metadata Database connection or creating new connection, the below screen will be pop up as in figure 7.
When we click on the button ‘Export as context’, we can see the create / reuse the context group window. There are options to create new repository context or reuse an existing repository context as described in figure 8.
In the next dialog box, we can give it a name for the context group as same as in figure 2. then click next and you will have to option to alter the auto-generated variable list.
This is another way of creating the context variables and values that can be used globally across all Talend jobs.