Expert Consultancy from Yellow Pelican

Talend tLibraryLoad Component Example

A site about Talend

Talend tLibraryLoad Component Example

In this example, we use Google's Phone Number Handling Library, to validate and format telephone numbers, that we'll read from a file. This example demostrates how to load and use a custom Java Library. This allows the functionality of Talend to be extended beyond that of the basic product.

Create a Sample Input File

The first thing to do, for this example, is to create an input file of data that contains phone numbers. You may use the small example given below or create your own. Create this file using a Text Editor.

Name;PhoneNumber
John Smith;07769 555 321
Robert Jones;Call Mobile
Jane Smith;+447721789555
Paul Jones;Paul.Jones123554@gmail.com
James Cooper;07754555987
Jane Marks;01214445551
Richard Jones;0

Download Google Library

Download Google's Phone Number Handling Library. This is a JAR (file format) file.

Create Job

Create a new Job named LoadJavaLibraryExample.

Add & Configure tLibraryLoad Component

Add a tLibaryLoad Component to your Job. Select tLibraryLoad_1 and then select the Component tab and you'll see that there is a drop-down list where you can select one of the Libraries that is available with a standard Talend Installation. Select the elipses ... to the right of Library. This allows you to add a new Library. Select the Java Archive file that you have just downloaded.

When you select the Java Archive, Talend will immediately take a copy of the file and place it in the .../lib/java directory .e.g. /Applications/TOS_DI-r100420-V5.3.0RC1/lib/java/libphonenumber-5.4.jar

Image 1

Next, select Advanced settings, from the Component tab. Here, you can import the required Classes. Enter import com.google.i18n.phonenumbers.PhoneNumberUtil; in the Import box. This will import the Class that we will be using for this example.

Image 2

Read & Display Data

You'll now add tFileInputDelimited, tMap and tLogRow Components, to read, map and display your data. If you are unfamiliar with some of these components, view the tutorial My First Talend Job.

  • Select the Component tab for tFileInputDelimited and then select Basic settings, if it is not already selected.
  • Set File name / Stream to the location of your input file, for example, /tmp/TalendByExample/Examples/tLibraryLoad/Contact.csv.
  • Select Edit schema and define the schema as Name, String PhoneNumber, String. There is no need to make any other schema settings for this example.
  • Set Header to a value of 1 as we have a header row in our input file.
  • Connect tLibraryLoad_1 to tInputFileDelimited_1 with Trigger - On Subjob Ok.
  • Connect tInputFileDelimited_1 to tMap_1g> with Row - Main.
  • Connect tMap_1 to tLogRow_1 with Row - *New Output* (Main) and name the output with a name of your choice.
  • Select tMap_1 and select the Map Editor from the Component tab. You may also double-click the component.
  • Copy the input schema to the output schema, press Auto map! and then press the Ok button. Talend will ask if you want to propogate your changes. This means that the schema will be copied to tLogRow_1. Select Yes. See Schema Reference for information on copying a schema.

Your correctly completed Mapping Editor should look like the screenshot below.

Image 3

Run Your Job

Your should now be able to run your Job. Your Job and the execution results hould look similar to the image shown below. Your Job has loaded the new Library and has displayed the content of your input file. You have not used ther functionality of the library to modify your data.

Image 4

Modify Input Phone Number

Google's Phone Number Handling Library provides various phone number functionality. For this example, you're going to use PhoneNumberUtil to convert a British telephone number in to E.164 format.

To use PhoneNumberUtil, you need to instantiate the PhoneNumberUtil Class. There are several approaches that we may take using Talend. For the purposes of this example, we will add a tJavaRow component. Often, you'll be able to simply use the functionality of your imported library directly in tMap. In the case of PhoneNumberUtil, an Exception may be thrown if certain input data is received. We need to handle this Exception and using tJavaRow is suitable for the purpose of this example.

To add this new component, we're going to use a convenient feature of Talend for inserting a new Component into a data stream. Search the Component Palette for tJavaRow. Drag this new Component until it is over the connector between the tMap_1 and tLogRow_1 Components. You will see that the connector is highlighted. When you see this highlighting, release the Component. You will see that Talend inserts the Component as shown in the image below. Talend will ask if you want to get the schema from the target Component. Select No.

Image 5

  • Select the Component tab for tJavaRow_1 and then select the Component tab, if it is not already selected.
  • Press the Sync Columns button. This will copy the input schema to the output schema.
  • Select Edit schema. and add a new Column MappedPhoneNumber, String. Press the Ok button and allow Talend to propogate changes.
  • Back on the Component tab, press Generate code. This will write the code for basic input->output mapping. Talend displays a warning each time you generate code.

Your generated code should now look like the following code.

//Code generated according to input schema and output schema
output_row.Name = input_row.Name;
output_row.PhoneNumber = input_row.PhoneNumber;
output_row.MappedPhoneNumber = input_row.PhoneNumber;

You'll now modify this code, as follows (or simply replace the entire code section).

com.google.i18n.phonenumbers.PhoneNumberUtil pNU = com.google.i18n.phonenumbers.PhoneNumberUtil.getInstance();

output_row.Name = input_row.Name;
output_row.PhoneNumber = input_row.PhoneNumber;

try {
   output_row.MappedPhoneNumber = pNU.format(pNU.parse(input_row.PhoneNumber, "GB"), com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat.E164);
}
catch (Exception e)
{
   output_row.MappedPhoneNumber = "bad number";
}

You'll see from the code above that we've instantiated the Class PhoneNumberUtil and changed the mapping of MappedPhoneNumber. You'll also note that the Google API may thow an Exception and you need to catch this to prevent your Job from terminating.

Re-run Your Job

Your should now be able to re-run your Job. This time, you should see the newly mapped Phone Numbers, as shown in the screenshot below.

Image 6




Expert Consultancy from Yellow Pelican
comments powered by Disqus

© www.TalendByExample.com