How to integrate groovy and Talend

Talend proposes 2 components to use Groovy in jobs, tGroovy and tGroovyFile.

tGroovy allows to execute groovy code and tGroovyFile allows to launch an external groovy script.

However, those 2 components have both a severe drawback. They run at the beginning of the job and they do not give the possibility to process the rows in the middle of a job.

Recently, I wanted to implement a quite complicate logic to process a row. Moreover, I wanted to be able to apply the logic on rows with different schema.  

I could have used the tJavaFlex but then I would have been obliged to recode the logic for every job because of the schema difference.

I could have used a custom Java method but the logic was really simpler to code in groovy. I needed dynamic expression like row.${attribute} and this is far more easier to do in Groovy than with the Java reflection API.

I started thinking about a way to integrate Groovy and Talend and I finally end up with the following.

The main idea is to put in the globalMap, an groovy objet that will perform the requested logic. Then at the appropriate step in the job, one will call the right method on the groovy object to do the work.

Putting the groovy object in the global map is done using the tGroovyFile. The component could be place anywhere in the job since it is process during the BEGIN phase. The content of the groovy script will be as follow:

globalMap.put("GroovyObject", new MyGroovyObject())
class MyGroovyObject {
def myMethod(row) {
// Do anything you want with the row
// update the row directly like
row.counter++
row.name = row.firstName + row.name
}
}

the globalMap is bind to the groovy context.

At the point where one want to process the row with the groovy method, just use a tJavaFlex component with the following code:

groovy.lang.GroovyObject groovyObj = (groovy.lang.GroovyObject)globalMap.get("GroovyObject");
groovyObj.invokeMethod("myMethod", rowInput);

where myMethod& is the name of the method to call and rowInput the name of the input row.

The output row of the tJavaFlex will be the row passed to the groovy method.

Comments are closed.