[TOS tutorial 09] Using Context Variables

In this tutorial, see how context variables can help run the same Talend Job in several different environments, and learn how to use them.

This tutorial uses Talend Open Studio Data Integration version 6.

1. Create a repository context group

  1. In the Project Repository, right-click Contexts > Create context group. In the Name field of the Create / Edit a context group wizard, type databasePropertyType, and click Next.
  2. To add variables to the context group, click the [+] button five times and type the field names as host, port, database, username, and password.
  3. To change the Type for the password field, click the field and click Password.
  4. In the Default Values column, assign values for each variable based on your server and click Finish.

The new context group, databasePropertyType 0.1, is displayed under Contexts in the Project Repository. To use this context group, you will need to import it into the Job.

2. Use the context variables in the Job by importing the group

  1. To display all the context groups in the Project Repository, in the Contexts view, click the Import Context button next to the [↓] button.
  2. To select all the variables, in the Select Context Variables window that appears, select Context: databasePropertyType. Alternative method: You can expand the context group and select only a few variables. The variables are imported into the Job along with their values. Note that you cannot change the values because they belong to the context group in the repository.
  3. The imported variables can be accessed in the Job by using the prefix "context." plus the variable name. In the Component view of the MySql component, replace the fixed values from the configuration with the corresponding variable from the context group. Note: While changing these fields, you can summon the auto-completion window by typing the first letters of the word, and then pressing the Ctrl + Space keys.
  4. To run the Job, in the Run view, click Run. The Job is run using the variables and default values defined in the context group.

3. Add more contexts to the Job

  1. In the Project Repository, right-click databasePropertyType 0.1 > Edit context group.
  2. In the Create/Edit a context group wizard, click Next.
  3. To create and configure a new context, click on the [+] button next to the Default column. In the Configure Contexts window that appears, click New and type production as its name.
  4. Similarly, add another context called test. Two new contexts, with the default context values, are created.
  5. To change the values for the production and test contexts, click in each field and enter the new value. Click Finish.
  6. To propagate the modifications to the Job, in the Modification dialogue box that appears, click Yes.
  7. In the Update Detection window, ensure that the new contexts have been selected and click OK.
  8. To view the two new contexts added to the group, in the Contexts view of the Job, scroll to the right.
  9. To run the Job, in the Run view, click the context and then click Run.

4. Use the context variables in a metadata definition

  1. To replace the fixed property values of the metadata with context variables, in the Project Repository, right-click the movies 0.1 metadata, and then clickEdit file delimited.
  2. To export the default values as a context, in the Edit an existing Delimited File wizard, navigate to the third step, and click Export as context.
  3. In the Create / Reuse a context group window, ensure that Create a new repository context is selected, and click Next.
  4. In the Name field, type moviesContextGroup and click Next. Observe that all the parameters have been changed to variables with the default values.
  5. Close the Create/Reuse a context group window and the Edit an existing Delimited File wizard.
  6. To propagate the modifications to the Job, in the Modification dialogue box that appears, click Yes.
  7. In the Update Detection window, ensure that the new contexts have been selected, and click OK.

In the component view of the movies component, observe that all the default property values have been replaced by context variables. Also, under Contexts in the Project Repository, observe that a new context group, moviesContextGroup 0.1 is displayed.

← PREVIOUS TUTORIAL | NEXT TUTORIAL →

Sei pronto a iniziare con Talend?