PowerUp
"How to create TOS components" Tutorial : Index

Creating Talend Data Integration Routines


Some time ago I started a tutorial about the creation of java components for TOS.
Quickly the tutorial became quite popular and I received some valuable feedback (thanks!).
I noticed, however, that many coders tend to try the component creation even when apparently it's not really needed, meaning they were not trying to achieve a task that would require a new component or that could be better achieved that way.
In many cases this is ok, especially if the coder was actually trying to learn how to code a component, so achieving the task itself was not the main purpose.
However I believe in some cases there are simple ways, one of which is by using Routines.

Routines
Routines are still written with java code and can be freely used in talend jobs, however they are used within the existing components.
They can be extremely powerful and can achieve things that components can't, such as integrate easily wherever you can "type something" in a job definition.
This includes any text-based parameter you have in your components, any field / variable definition in a tMap or they can be freely used in tJavaxx components...
They are normally not used to generate data and they do not deal with row data connections, so there are limitations of what you can possibly do with them, but the interesting thing is that they are normally easier and quicker to develop and to debug than components.
To give you a quick understanding of these differences :
- They are created editing a single file
- You don't need to mess around with java jet code
- They are installed in the repository, within a project


I created my first routine when I had to deal with some webservice calls returning data in XML.
Sure you can achieve that using the standard TOS components, but I was not happy with that solution because it required in the specific case quite some effort in processing data etc.
So I implemented everything using a few routines and then I managed the process with a couple of tJavaFlex components.
In many cases routines are easier to use than components for the final user, I know that can sound strange, but have you ever tried to press crl_space when editing a component parameter?
Yup, those tings with names xxxx.xxxx are routines.
They even come with guided input and a quick description of what they do.


Some basic concepts

You should have understood by now, that routine functions can be used straight away anywhere in the job, but that kind of clashes with some basic java peculiarities : Objects are instances of classes, they need to be created, eventually initialized and somehow destroyed after usage.
That does not apply entirely to classes with static methods and these are exactly what is used to create routines.
In the case of "StringHandling.DOWNCASE()", a default routine, "StringHandling" represents the class (it is usually NOT the class name, even if it will refer to a class) and "DOWNCASE" is a static method of that class.

As usual, the best way of learning something is looking around some existing examples (at least this is what I believe).
I mentioned the StringHandling.DOWNCASE() routine, it could be a good idea to start looking into it.
It's a standard talend routine, just open your repository, navigate to code , Routines, System and you should see a lits of routine classes, one of them is StringHandling.
If you double click it, the editor will open the single file needed to define the functions contained.


If you look at the beginning of the file you should see something like this :


// ============================================================================
//
// Copyright (c) 2005-2010, Talend Inc.
package routines;

public class StringHandling {



Nothing particularly fancy, right? Just a common class declaration, just notice the package is called routines, we will use this package when creating our own routines.
If you quickly check the various methods, you will notice they are normally defined as public and static.
Something good to have in mind is that, unlikely components, routines ARE classes, so the normal rules that apply to classe also apply to them (I don't know why, but that kind of makes me feel a bit more comfortable... it's a known land : ).

Another thing you may notice is that, again differently from components, routines have a "header comment" at each method, which is quite surprising if you are used to dig into Talend code :P
Moreover you will notice that the format of those comments (javadoc style) is extremely consistent across all the methods, I found it amazing.
There is a good reason for that : althought they are technically comments for what concerns java, they are not used as simple comments in Talend (yeah, you guessed it... coders that place nicely formatted comments at each method? Not likely to happen, right?)
Let's have a look at those comment lines :



/**
* Determines whether the expression is an alphabetic or nonalphabetic.
*
* {talendTypes} boolean | Boolean
*
* {Category} StringHandling
*
* {param} string("abc") input: String need to be known whether is an alphabetic or not.
*
* {example} ALPHA("abc") # true
*/
public static boolean IS_ALPHA(String input) {




Those are the lines that precede the IS_ALPHA method, let's try to use it in a job :

I entered in the parameter page of a component (a mySQLInput in my case, any component with parameters would do), clicked on a text parameter (in my case "table name") and the pressed ctrl_space.
A list of routines becomes visible, I scrolled down and selected the StringHandlin.IS_ALPHA function, a box on the right displays some useful information, it's not by chance that the infor displayed matches with what we have in comment sections.
Particularly, the {Category} identifies the name of the routine "family", in this case it matches the name of the class, however you should know that when you list routines, they will be prefixed with the Category name, not the class name.
The other information, such as talendTypes and param is also used for the code "autocmpletion" functionality.
Now, what about our "DOWNCASE" function?



/**
* Converts all uppercase letters in an expression to lowercase.
*
* {talendTypes} String
*
* {Category} StringHandling
*
* {param} string("Hello") string: String
*
* {example} DOWNCASE("Hello") # hello
*/
public static String DOWNCASE(String string) {
return string == null ? null : string.toLowerCase();
}



Yup, that's it, told you routines are normally simpler than components.
If you look around into the existing routine files, you could get some inspiration to create your own routines... just one thing before you start : I strongly suggest you create your own folder and your own class instead of modifying the system default routines

If someone is interested I could provide one or a few more lessons on the topic, creating an example routine with some more functionlity into it (i.e. using an external jar file etc)

Happy coding!

Component Creation;