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 ...

Saturday, May 24, 2014

Use tSetGlobalVar to Record a Count

If you want to use one or more global variables set by an input flow like a tOracleInput throughout your Talend job, consider using tSetGlobalVar.  Convert the flow from the input component into an iterate and map a field to a key in the tSetGlobalVar component.

tSetGlobalVar is a Talend Open Studio component that allows you to map a field from an input flow to a global variable.

This job shows an input flow -- in this case a tFixedFlowInput -- being connected to a tFlowToIterate.  The tFixedFlowInput can be replaced with a more useful component like a tOracleInput.  The tFixedFlowInput demonstrates how this would look for a single-record result such a SUM or a COUNT in a SQL query.  The top subject (tFixedFlowInput -> tFlowToIterate -> tSetGlobalVar) will loop for each record in the input flow.  For inputs returning more than one record, be aware that the tSetGlobalVar in this subjob would set the global for the last record.

Job Containing a tSetGlobalVar
The tFlowToIterate is configured as in the following screenshot.  It's job is to convert the flow into an iteration.

Certain components are flow-based (tMsSqlInput, tOracleOutput) while others are iteration-based (tSetGlobalVar, tJava).  Both flow and iterate are loops.  The difference between the two is that the flow-based components are driven by a result set (SQL query, lines in a text file, XML elements) and that a schema is made available to downstream components.

tFixedFlowInput Configuration
The tSetGlobalVar component is setting a global variable "mycount" when it encounters the field ACCTIDCNT.  Although row.ACCTIDCNT is involved in the globalMap as is "mycount", this step is needed because the row1 fields won't be available outside of the flow.

tSetGlobalVar Configuration

Finally, in a separate subjob, I output the value of my global using a tJava.

Using the Global in a tJava
This coding technique is cleanest with single-value results.  One input record is converted to one iteration which sets one or more globals.  It is possible to write your own tSetGlobalVar with a tJavaRow that would also eliminate the need for the tFlowToIterate, but the intent of the tSetGlobalVar is clearer.  Even if you name your general-purpose components (tJava, tJavaRow) with a comment, there's still no assurance of exactly what is in there.

No comments:

Post a Comment