Monday, April 27, 2015

How to use tReplace component in Talend

tReplace component is used to replace a value it carries out a search & replace operation in the input columns defined.

This is Input Customer File.

Cust_First_Name,Cust_Address,Cust_City,Cust_Country
Lee,722 Bur Oak Avenue ,Berlin,Germany
William,543 ANJOU ANJOU H1K 2P4,Mexico,Mexico
Nick,40  Toronto M8Z 3Z7,Mexico,Mexico
Jian,11 Collaroy  2093,London,UK
Ming,223 Wellington  5011,Berlin,Germany
Joseph,324 Tman Street   Stafford Heights ,Mannheim,Germany
Justin,90 Ruapehu Road   Ohakune ,Strasbourg,France
Ming,32 Wilfred road,Madrid,Spain
Lee,Williamstown Road,Marseille,France

Firstly create a new job from Job Designs > Create Job.
Drag the schema of Customer.csv from Metadata > File Delimited > Suppliers and drop it to the design work space and select tfileInputDelimited option from pop window.This Customer.csv file is been taken as an input file
Or you can simply drag this component from the palette and double click on it to open the component properties and click [...] next to the File Name field to specify the path where you have created your Customer.csv file.
Then drag and drop the following components from the palette into the design workspace:-tReplace,tLogRow.
Connect each component by right clicking and select Row > Main.



Open the component properties of tReplace first click on Edit Schema button so that the input is routed directly to the output. Select the columns you want to replace and drag it to the tReplace_1(Output) and press the OK button.
Now this process can be done by two ways so let us see both the process one by one.

  1. Simple Mode
Select the Simple mode check box click the plus(+) sign to add some lines to the parameters table.
  • On the first parameter line, select Cust_First_Name as InputColumn. Type "Lee" in the Search field, and "Leelon" in the Replace field.
  • On the second parameter line, select Cust_Address as InputColumn. Type "2" in the Search field, and "4" in the Replace field.
  • On the third parameter line, select again Cust_City as InputColumn. Type "Mexico" in the Search field, and "California" in the Replace field.
  • On the fourth paramater line, select Cust_Country as InputColumn. Type "Germany" in the Search field, and "UK" in the Replace field.

2. Advanced Mode

Select the Advanced mode check box click the plus(+) sign to add some lines to the parameters table.

  • On the first parameter line, select Cust_First_Name as Source. Type "[A-Za-z]*ee" in the Pattern field, and "Leelon" in the Replace field.This means that the word which ends by "ee" will be replaced by "Leelon"
  • On the second parameter line, select Cust_Address as Source. Type "2" in the Pattern field, and "4" in the Replace field.
  • On the third parameter line, select again Cust_City as Source. Type  "[A-Za-z]*ico" in the Pattern field, and "California" in the Replace field.This means that the word which ends by "ico" will be replaced by "California".
  • On the fourth paramater line, select Cust_Country as Source. Type "[A-Za-z]*many" in the Pattern field, and "UK" in the Replace field.This means that the word which ends by "many" will be replaced by "UK",


Running both the Mode will give the same result as shown in below screen.


Here you can see that in Cust_First_Name "Lee" is replaced by "Leelon" and the word ended by *ee is also replaced by "Leelon".In Cust_Address 2 is replaced by 4.In Cust_City "Mexico" is replaced by "California" and the word ended by *ico is also replaced by "California".In Cust_Country "Germany" is replaced by "UK" and the word ended by *many is also replaced by "UK".

Both process of Simple mode and Advanced mode are explained. tReplace is used to swap out one value for another based on text patterns.

No comments:

Post a Comment