Talend Tutorials

Creating And Using Java Routines

Creating And Using Java Routines

Sometimes, the components in Talend need a little more from you to get the job done. There are plenty of String transformations that you may need to custom build for your Talend jobs. In today’s tutorial, we will go through the process of creating a Talend Routine that will allow you to create custom and reusable Java code. This example will create code that implements a basic Caesar Cipher.

Step 1: Start Talend Open Studio

Step 2: Create a Routine

On the right side of your screen, under “Repository”, find the “Code” section.

Expand it, and right click on “Routines” and select “Create Routine”. Then, choose a name for your for Routine, such as “RoutineDemo”.

image 1

Step 3: Edit your Java Code

Those of you who may have used Eclipse before, may recognize the layout, as it more or less is an Eclipse environment to write java in. The outline of the job is already set up for you, with comments to explain how to define Routines (this is useful for quickly identifying your custom routines later).

image 2

Though not completely necessary for this tutorial, its good practice to fill out the information under your class declaration. This information will appear later when selecting your Routine.

image 3

Underneath the “demo” header, we will create the functionality of our Routine, in this case a Caesar Cipher that will move characters 16 letters forward in the alphabet. The code is as follows:

image 4

String index = “abcdefghijklmnopqrstuvwxyz”;
String caesar = “”;
if (message != null) {

message.toLowerCase();
int len = message.length();

for (int i=0; i < len; i++){
int x = index.indexOf(message.charAt(i));

if ((x + 16) > 26) x = x-26;

caesar = caesar + index.charAt(x+16);
}

//$NON-NLS-1$
}
System.out.println(caesar); //$NON-NLS-1$ //$NON-NLS-2$

Save your routine when you’re done.

Step 4: Create your Job

Back in your Respository, right-click “Job Designs” and then select “Create Job”. Fill out a name for it.

Then, in the Palette on the right side of your screen, search for a “java” component. You’ll find tJava under “Custom Code”. Drag the icon into the canvas in the middle of your screen to create the component.

image 5

From here, the job is relatively simple: Select the tJava component and navigate to the “Code” textbox under the “Component” tab.

Delete anything that may be there already, and then press the Ctrl+Space shortcut to bring up a list of Routines. You can find your User Defined Routine near the bottom of the list.

When you select it, it will display the information about the job that you entered in earlier.

image 6

While selecting your Routine, click Enter to insert the routine into the tJava component. Be sure to end it with a semicolon!

image 7

The default string is set to “world” but you can use any alphabetic string.

Step 5: Run the Job

Switch to the “Run” tab, and click the “Run” button under “Execution” to display your results.

image 8

Looking For More Talend Resources and Help?
Ask An Expert