Implementing For Loop in Informatica
Implementing For Loop in Informatica
Implementing For Loop in Informatica
com
Java TransIormation
AppIies to:
nformatica PowerCenter
Summary
This article briefs about implementing FOR loop in nformatica using Java Transformation.
Learn nformatica Power Center: www.info-etl.com 1
Implementing FOR loop in Informatica PowerCenter
TabIe of Contents
NTRODUCTON ................................................................................................................................................ 3
REQUREMENT ................................................................................................................................................. 3
SOURCE ......................................................................................................................................................... 3
TARGET.......................................................................................................................................................... 3
SOLUTON OVERVEW ..................................................................................................................................... 4
SOLUTON STEPS............................................................................................................................................. 4
SOURCE ......................................................................................................................................................... 4
EXPRESSON TRANSFORMATON .............................................................................................................. 4
JAVA TRANSFORMATON ............................................................................................................................ 5
TARGET.......................................................................................................................................................... 6
OUTPUT ......................................................................................................................................................... 6
ADVANTAGES ................................................................................................................................................... 7
DSCLAMER AND LABLTY NOTCE ............................................................................................................. 8
Learn nformatica Power Center: www.info-etl.com 2
Implementing FOR loop in Informatica PowerCenter
INTRODUCTION
This article briefs about implementing FOR loop in nformatica using Java Transformation.
REQUIREMENT
Consider that we are receiving a '|' delimited flat file that contains the sales person's tour information.
SOURCE
SaIesperson Name | Tour from Date | Tour to Date | PIace
Sukumar | 20091117 | 20091119 | Chennai
Ram | 20091117 | 20091119 | Bangalore
Sukumar | 20091120 | 20091122 | Bangalore
Ram | 20091120 | 20091122 | Chennai
Our requirement is to generate Tour Table TARGET that contains the following
TARGET
SaIesperson Name
Date
PIace
Sukumar
20091117
Chennai
Sukumar
20091118
Chennai
Sukumar
20091119
Chennai
Sukumar
20091120
Bangalore
Sukumar
20091121
Bangalore
Sukumar
20091122
Bangalore
Ram
20091117
Bangalore
Ram
20091118
Bangalore
Ram
20091119
Bangalore
Ram
20091120
Chennai
Ram
20091121
Chennai
Ram
20091122
Chennai
Learn nformatica Power Center: www.info-etl.com 3
Implementing FOR loop in Informatica PowerCenter
SOLUTION OVERVIEW
To achieve the above requirement we will be using the Java Transformation available in nformatica
PowerCenter.
SOLUTION STEPS
SOURCE
Delimited Flat File Tournfo.txt
SaIesperson Name | Tour from Date | Tour to Date | PIace
Sukumar | 20091117 | 20091119 | Chennai
Ram | 20091117 | 20091119 | Bangalore
Sukumar | 20091120 | 20091122 | Bangalore
Ram | 20091120 | 20091122 | Chennai
EXPRESSION TRANSFORMATION
Port Name
Data Type
Type
Expression
Description
String(255)
Salesperson Name
String(50)
O
Use the substring, instr function to
retrieve the sales person name
using the | delimiter.
FromDate
String(20)
V
Use the substring, instr function to
retrieve the 'From Date' using the |
delimiter.
ToDate
String(20)
V
Use the substring, instr function to
retrieve the 'To Date' name using
the | delimiter.
Place
String(20)
O
Use the substring, instr function to
retrieve the 'Place' using the |
delimiter.
FromDate_out
Date time
O
To_date(FromDate,YYYYMMDD)
ToDate_out
Date time
O
To_date(ToDate,YYYYMMDD)
Expression transformation is used for retrieving the values from the flat file source and assigns values to
individual ports.
Learn nformatica Power Center: www.info-etl.com 4
Implementing FOR loop in Informatica PowerCenter
JAVA TRANSFORMATION
Drag the java transformation into the Mapping.
Create the following ports.
Port Name
Data Type
Type
Salesperson Name
String(50)
/O
FromDate
String(20)
ToDate
String(20)
Place
String(20)
/O
FromDate_out
Date time
O
ToDate_out
Date time
O
TourDate
DateTime
O
Link the FromDate_out, toDate_out from the Expression Transformation to the From Date and To
Date ports of the Java transformation.
Under the Java code tab; mport Package tab place the below code:
mport java.util.*;
mport java.text.*;
This is to import relevant java packages.
Under "On InputRow " tab place the Iollowing code
DateFormat formatter ;
formatter = new SimpleDateFormat("dd-MMM-yy");
FromDate_out =formatter.format(FromDate);
ToDate_out =formatter.format(ToDate);
Calendar startCal = Calendar.getnstance ( ) ;
Calendar endCal = Calendar.getnstance ( ) ;
Date date_start=new Date(FromDate.longValue());
startCal.setTime ( date_start) ;
Date date_end=new Date(ToDate.longValue());
endCal.setTime ( date_end) ;
for ( Calendar c = startCal; c.compareTo ( endCal ) <= 0; c.add
(Calendar.DAY_OF_WEEK, 1 ) )
{
Learn nformatica Power Center: www.info-etl.com 5
Implementing FOR loop in Informatica PowerCenter
TourDate= formatter.format(c.getTime ());
generateRow();
}
The above code snippet uses the java calendar function.
You can see for loop construction based on the from date and the to date,
We need to link the salesperson name, Tour Date and Place ports from this java transformation
to the target.
TARGET
ReIationaI TabIe TOUR
Description
DataType
Sales Person name
String
Tour Date
Date time
Place
String
OUTPUT
Source:
Sukumar | 20091117 | 20091119 | Chennai
Target:
SaIesperson Name
Date
PIace
Sukumar
20091117
Chennai
Sukumar
20091118
Chennai
Sukumar
20091119
Chennai
Java transformation takes the 'from date' and 'to date' as input and a FOR loop is constructed.
For each input row based on the 'from date' and 'to date' java transformation will generate
multiple rows.
n our case for a single row in source we will be generating 3 rows in target.
Learn nformatica Power Center: www.info-etl.com 6
Implementing FOR loop in Informatica PowerCenter
ADVANTAGES
Java transformation can be used to generate multiple rows based on a condition.
Normalizer can generate rows based on the 'Occurs' clause where we will specify a static number
but if you want to generate multiple rows based on a dynamic value then go for java transformation.