Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Thursday, October 6, 2011

Applying Contexts to Talend Open Studio Jobs

If you're using Talend Open Studio for with stable Dev/Test/Prod configurations, consider creating a Context for each configuration.

In Talend Open Studio, a Context is used to group a set of variables that parameterize a job.  This is an important configuration management feature that gives the caller the ability to deploy the same code in different runtime environments.

Using Only ContextGroups


For example, if I export a job 'PropsJob' with scripts generated, I can call the following from the Windows command line to configure the job to run in different environments.

C:\Jobs\PropsJob_0,1\PropsJob> PropsJob_run.bat --context=Dev
C:\Jobs\PropsJob_0,1\PropsJob> PropsJob_run.bat --context=Test
C:\Jobs\PropsJob_0,1\PropsJob> PropsJob_run.bat --context=Prod
C:\Jobs\PropsJob_0,1\PropsJob> PropsJob_run.bat

The last command will use the Default context.

Within Talend Open Studio, different runtime Contexts are selected from the Run tab.  See the drop down list in the upper right-hand corner.

Select a Context on the Run Tab

Contexts and Context Variables can be created at the Project level, which makes them available to all jobs in a Project.  These are added to the Repository.  Using the Repository is recommended to help with coding standards.  Names like 'DATA_DIR' can be centralized and won't be subject to being renamed in individual jobs.

Alternatively, Contexts and Context Variables can be created for individual jobs.  See the following screenshot showing the Contexts tab of a job 'PropsJob'.

Contexts Tab
Overriding Parameters

If you're running an exported job, you can pass the argument --context_param followed by a name/value pair to override any of the parameters in the context.  For example

C:\Jobs\PropsJob_0.1\PropsJob>PropsJob_run --context_param DBSCHEMA=demo

Will override the context parameter 'DBSCHEMA' defined in a Context.

Preferred Approach: Separate Config from Code

The most robust option for deploying Talend Open Studio jobs is to use property files that are separated from the exported job code.  This allows for the changing of config data like data sources, directories, and passwords without touching code.  To accomplish this, use the tContextLoad component in a job with a source such as tFileInputDelimited.  Other input components like databases can also be used.

Configure the input component schema with key and value fields.  If your input schema doesn't have key and value fields, use a tMap to rename the fields.  In the following screenshot, a tFileInputDelimited is created with two fields: key and value.  The file name used by the tFileInputDelimited is itself parameterized with a Context Variable set for the Default context.  Note that the variable 'context.PROPSFILE' is not expected to be in the properties file (and might be removed to flag an error condition of the job).

The schema in the tFileInputDelimited uses the equals sign for a delimiter ('=').

TOS Job Configured with Custom .properties File

To run the exported job using the default configuration,

C:\Jobs\PropsJob_0.1\PropsJob>PropsJob_run

And to use a different properties file

C:\Jobs\PropsJob_0.1\PropsJob>PropsJob_run --context_param PROPSFILE=C:/jobs/propsjob2.properties

If you would like the job to fail immediately if it can't find a config file, check the "Die on error" option on the tFileInputDelimited component.

Printing Context

In a development or even a production environment, it can be useful to print out the configuration at the start of a job.  This job uses the tContextDump component to echo each name/value setting defined in the context and used in the job to a tLogRow.  Other output data sources can be used, but this example will keep all the messages directed to standard out together.

Using tContextDump


Alternative: Hack the Exported Job

You can also hack the exported job by finding the exported properties files.  In the unzipped export, look for a directory 'contexts' under the Project/Job directories (for example, "propsjobproject/propsjob_0_1/contexts'.  There is a property file for each context that can be edited.  It's best to avoid this practice since it can mix up a deployment that overwrites these files.  However, it can be used to fix an operational problem quickly.


For stable environments that share resources like directories and database accounts, create a Project or Job-wide set of Contexts to parameterize your jobs.  Most everything can be parameterized using using Contexts, but variables like passwords may require special handling.  This is so that passwords don't get mixed up in TOS code leading to a security hack or forcing a new job deployment when a password expires.  In these cases, use the more robust deployment option of a separate properties file.

4 comments:

  1. Thanks for the post.
    I cam across it while troubleshooting a problem with tLoadContext

    I'm following the tutorial at http://www.talendforge.org/tutorials/tutorial.php?idTuto=34

    It instructs to fill the Environment field in the tLoadContext schema with Client

    I can't find any information on what Client is, or where it comes from, or even how Environment works exactly.

    Error: Client cannot be resolved to a variable.
    I'm following the tutorial pretty closely and can't get rid of this error.

    any suggestions?

    ReplyDelete
    Replies
    1. Hi,

      The screenshot of the tRowGenerator on TalendForge looks incorrect. The text instructions alongside the image mention using a quoted string, so try quoting Client ("Client").

      Alternatively, if you have context variable Client defined, you can access this using context.Client (no quotes).

      "Client" is a Java String literal. context.Client is a variable holding a value set in the configuration, command line or another component like a tContextLoad. globalMap("Client") is the preferred way to pass variables around in components. Finally Client (no quotes) can only be a locally-defined variable in a component like tJava.

      Delete
  2. thanks for the post.
    i have an existing ejb java application and i wanna to put jar and context file there. but i have always could not find the default context.However, in the web package it work great.
    please i need to know where to put each file(jar, context) and if it has a configuration to make in ejb package.

    ReplyDelete
    Replies
    1. Hi,

      Is this a jar of ejb client stubs and a config file for remote ejb connect info?

      Delete