Talend: update a global variable in a subhead

My simplified work looks like this:

tSetGlobalVar ---> (onSubJobOK) ---> tRunJob ---> (onSubJobOK) ---> tJava

  • tSetGlobalVar will define a global variable and its initial value will say: myKey:"firstValue"

  • tRunJob will run a sub-job that only contains a second tSetGlobalVar component that should set a new value for the global variable defined in the main job: ((String)globalMap.get("myKey")): "newValue"

    also tried this: "myKey": "newValue"

  • tJava is only used for debugging, it looks like this: System.out.println(((String)globalMap.get("myKey")));

Actual output: firstValue

Expected Result: newValue

Is there any other way to change the value of the global variable in the subplot and get the updated value in the main job?

+3


source to share


2 answers


In Talend, you can pass context variables to subjobs, and they work like standard Java variables, so if you pass in something unmodifiable (like strings or base types), you won't get any changes, but if you pass "by reference" , types, you will change your object and you will see the changes made by the subtitle, since the father's job still contains a reference to the changed object.

So the solution to your problem is to simply pass the globalMap from the Father's job to the child's job so that the child can read and modify this map and the father's job will see any changes. The map you pass in will be a different map than the globalMap of the child job and will only be used wherever you use it.

Unfortunately, Talend only has one "by reference" type: the object type, so you have to pass anything as an anc object, casting it back where you will use it, but I think this is not such a big problem.



Here are the images for the assignment: Running Father Father job with executin results

Passing the card to the child enter image description here

The child retrieves the map from contex variable and uses the map enter image description here

+3


source


You must pass data to the child job using context variables, not globalMap. Then you pass the data back to the parent job using tBufferOutput in the child job.

As an example, here's a very simple setup that takes an id and date and passes it to a child job, which just prints it to the console / logs, and then passes some data back to the parent, which also just prints it to the console / logs.

Parental work: Parent job layout

Data in tFixedInflow file in parent job: Parent tFixedFlowInput

Notice how you must use a combination of a key value pair for a key and a value to pass it to the tContextLoad component, which will then create a context variable named key and hold on to a specific value.

In the parent job, we set the context variables id

and date

and then immediately print the current contexts for the job (which will only include the specified context variables).

After that, we call the child job using the tRunJob set of components to pass the entire context:

tRunJob configuration to call child job and pass context variables

Alternatively, you can specify which context variables you pass to the child job by unchecking the option Transmit whole context

and explicitly defining which contexts to submit. You can also define the value of the context variables here, but it usually makes sense to generate the values ​​of the context variable in the main thread of your job and pass them as a key value pair to the tContextLoad component.



Child: Child job layout

In the child job, we just print the contexts that were sent by dumping them to a tLogRow with a tContextDump component, and then after that, we use another tFixedInflowInput to hard-code some data in this case:

Child job tFixedFlowInput data

Then we move on to the tBufferOutput component, which allows us to read data in the parent job.

Returning to the parent job, we bind the tLogRow to the tRunJob to the main link and provide the schema that is in the tBufferOutput child job. Then it prints data from the child job.

Example of a job output:

.----+----------.
|Parent Contexts|
|=---+---------=|
|key |value     |
|=---+---------=|
|date|2014-10-30|
|id  |12345     |
'----+----------'
.----+----------.
|Child Contexts |
|=---+---------=|
|key |value     |
|=---+---------=|
|date|2014-10-30|
|id  |12345     |
'----+----------'
.-----+----------.
|Child Data to be Passed to Parent|
|=----+---------=|
|id   |date      |
|=----+---------=|
|12346|2014-10-31|
'-----+----------'

.-----+----------.
|Output from Child1|
|=----+---------=|
|id   |date      |
|=----+---------=|
|12346|2014-10-31|
'-----+----------'

      

GlobalMap stores data in the job itself and is generally not shared with parent or child jobs, so it cannot be used for this purpose.

+8


source







All Articles