A site about Talend
Context is something you need to understand early on in your Talend development. Getting it right from the start will save a lot of pain later; when you start deploying your Jobs to Test and Production, and will save you major re-writes later as the number of Jobs you have increases and maintenance gets more difficult.
Read our tutorial on how to Reusable Context Load Job from a file.
Context describes the user-defined parameters that are passed to your Job at runtime. Context Variables are the the values that may change as you promote your Job from Development, through to Test and Production. Values may also change as your environment changes, for example, passwords may change from time to time.
Context helps you manage this.Context is stored as an Object.
private ContextProperties context = new ContextProperties();
Every Talend Job has a Default Context. As you add Context Variables to your Job, they are added to the Default Context. You may also add your own contexts, for example, Test and Production. Adding multiple contexts allows you to easily switch the context of your Job. We'll discuss this later.
You use Context Variables as parameters to your executing Job; however programmers often use them just for their global storage. My preference is to use globalMap for storage and to reserve Context Variables just for my execution parameters.
Context defines a collection of Context Variables.
When you create a String Context Variable, either directly within a Job or as part of a Context Group, Talend assigns a value of null. This appears to imply that the Context Variable is, in fact, a null String pointer; however, this is not the case.
I don't know why Talend does this. When looking at Context Groups, it does appear that it is the intention that this does represent a null String pointer; however when you test the context variable within a Job, it has actually been assigned a string value of "null". This seems to be a bug to me although it has persisted through many versions of Talend.
If you clear the assigned value, Talend assigns a zero length string.
When testing if Context Variables are appropriately set, I usually test for all eventualities.
if(context.contextDir == null ||
context.contextDir.equals("null") ||
context.contextDir.equals("")) {
System.err.println("Context variable contextDir is not set");
}
Context Groups allows you to easily define a group of Context Variables that can be added to your Job as a group rather that individually adding each Context Variable to every Job that needs it.
My preference is to always load Context from files. This provides maximum flexibility, ease of maintenance and helps prevent you from inadvertently running your Job against the wrong environment.
The tutorial Load Context shows how to load Context from a file.