100% found this document useful (2 votes)
587 views18 pages

Informatica Scenarios

The document describes 10 real-time Informatica scenarios including: 1) Getting the previous row value when processing the current row 2) Loading all rows except the last N rows 3) Calculating the cumulative sum using an expression transformation 4) Converting multiple rows into a single row with multiple columns 5) Loading only the last N rows of a source file into the target table

Uploaded by

Raghu Nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
587 views18 pages

Informatica Scenarios

The document describes 10 real-time Informatica scenarios including: 1) Getting the previous row value when processing the current row 2) Loading all rows except the last N rows 3) Calculating the cumulative sum using an expression transformation 4) Converting multiple rows into a single row with multiple columns 5) Loading only the last N rows of a source file into the target table

Uploaded by

Raghu Nath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Informatica Scenarios - Real Time Scenarios

The following list of informatica scenarios helps you in learning informatica transformations and
also for facing interviews easily.

1. Get Previous Row Value

Retrieve the previous row value when processing the current row.

2. Load all rows except last N rows

Skip Last N rows from the source and load the reamining rows

3. Cumulative Sum Calculation

Find the cumulative sum of values using expression transformation.

4. Convert multiple Rows to single row

Concatenate multiple rows into a single column.

5. Load Last N Records of File into Target Table

Load the last N rows from the source into the target.

6. Load Alternative Records into Multiple Targets

Load odd number rows into one target and even numbered rows into another target.

7. Load Source File Name in Target

Generate the source file name and load into the target.

8. Generate rows based on a column value - Informatica

This is to duplicate each row based on the value in the column.

9. Dynamic Target Flat File Name Generation

Without specifying the target file name, generate dynamically in the mapping.

10. Reverse the Contents of Flat File

Last row should become first row and first row should become last row in the target.

1. Get Previous Row Value in Informatica

How to get the previous row value while processing the current row in informatica?

One of my blog readers asked this question. The source data is shown below:
Table Name: Customers

cust_id, Year, City

-----------------------

10, 2001, BLR

10, 2002, MUM

10, 2003, SEA

10, 2004, NY

20, 2001, DEL

20, 2002, NCR

20, 2003, HYD

The question is for each customer when processing the record for current row, you have to get the
previous row city value. If there is no previous row, then make the previous row value as null. The
output data is shown below:

Table Name: Customers_TGT

cust_id, Year, City, prev_city

------------------------------

10, 2001, BLR, NULL

10, 2002, MUM, BLR

10, 2003, SEA, MUM

10, 2004, NY, SEA

20, 2001, DEL, NuLL,

20, 2002, NCR, DEL


20, 2003, HYD, NCR

Getting Previous Row Value Informatica Mapping Logic

Solution:

 Connect the source qualifier transformation to the sorter transformation and sort the data
on cust_id, year ports in ascending order.
 Connect the sorter transformation to the expression transformation. In the expression
transformation, create the below additional ports and assign the corresponding
expressions:

cust_id (input/output port)

year (input/output port)

city (input/output port)

v_current_cust_id (variable port) = cust_id

v_act_previous_city (variable port ) = IIF(v_current_cust_id = v_previous_cust_id, v_previous_city,


NULL)

v_previous_city (variable port) = city

v_previous_cust_id (variable port) = cust_id

o_previous_city (output port) = v_act_previous_city

 Connect the output ports of expression transformation to the target.

2. Load all records except last N – Informatica


Q) I want to load all the records from my source, which is a file, except the last 5 records. This
question can be asked interview as "How to remove the footer record which is last record"

Example: My source file contains the following records:

Name

----

A
B

After excluding the last 5 records, i want to load A,B into the target. How to implement a mapping
logic for this in informatica?

Solution: Follow the below steps


 Connect the source qualifier transformation, NEXTVAL port of sequence generator to the
sorter transformation.
 In the sorter transformation, check the key box corresponding to NEXTVAL port and change
the direction to Descending.
 Create one more sequence generator transformation and a filter transformation.
 Connect the NEXTVAL port of the second sequence generator transformation to the filter
and Name port of sorter transformation to filter.
 Specify the filter condition as NEXTVAL > 5.
 Save the mapping. Create a workflow and session. Save the workflow and run the workflow.
 You can use the same approach to remove the footer record from the source by specifying
the filter condition as NEXVAL>1. If you have any issues in solving this problem, please do
comment here.

3. Cumulative Sum Calculation in Informatica


Q) How to find the cumulative sum of salaries of employees in informatica?

I have employees table as a source. The data in the employees table is shown below:

Table name: employees

Dept_Id, emp_id, salary

---------------------

10, 201, 10000

10, 202, 20000

10, 203, 30000


20, 301, 40000

20 302, 50000

I want to sort the data on the department id, employee id and then find the cumulative sum of
salaries of employees in each department. The output i shown below:

Dept_id emp_id salary, Cum_salary

---------------------------------

10, 201, 10000, 10000

10, 202, 20000, 30000

10, 203, 30000, 60000

20, 301, 40000, 40000

20 302, 50000, 90000

Solution: Follow the below steps for implementing mapping logic in informatica.

 Connect the source qualifier transformation to a sorter transformation. Sort the rows on the
dept_id and emp_id ports in ascending order.
 Connect the sorter transformation to the expression transformation. In the expression
transformation, create the following additional ports and assign the corresponding
expressions:

v_salary (variable port) = IIF(dept_id = v_last_dept_id, v_salary + salary, salary)

v_last_dept_id (variable port) = dept_id

o_cum_salary (output port) = v_salary

 Connect the expression transformation ports to the target. Save the mapping.

4. Convert multiple Rows to single row (multiple Columns) in


Informatica
I have the sales table as a source. The sales table contains the sales information of products for each
year and month. The data in the source table is shown below:

Source Data: Sales table

year product month amount


-------------------------

1999 A Jan 9600

1999 A Feb 2000

1999 A Mar 2500

2001 B Jan 3000

2001 B Feb 3500

2001 B Mar 4000

The sales information of a product for each month is available in a separate row. I want to convert
the rows for all the months in a specific year to a single row. The output is shown below:

Target Data:

year product Jan_month Feb_month2 Mar_month

-------------------------------------------

1999 A 9600 2000 2500

2001 B 3000 3500 4000

How to implement a mapping logic for this in informatica?

Solution:
Follow the below steps to implement the mapping logic for the above scenario in informatica:
 Create a new mapping.
 Drag the source into the mapping.
 Create an expression transformation.
 Drag the ports of source qualifier into the expression transformation.
 Create the below additional ports in the expression transformation and assign the
corresponding expressions:

Jan_Month (output port) = IIF(month='Jan', amount, null)

Feb_Month (output port) = IIF(month='Feb', amount, null)

Mar_Month (output port) = IIF(month='Mar', amount, null)


 Connect the expression transformation to an aggregator transformation. Connect only the
ports year, product, Jan_Month, Feb_Month,Mar_Month ports of expression to aggregator
transformation. Group by on year and product in aggregator transformation.
 Create the below additional ports in aggregator transformation and assign the
corresponding expressions:

o_Jan_Month (output port) = MAX(Jan_Month)

o_Feb_Month (output port) = MAX(Feb_Month)

o_Mar_Month (output port) = MAX(Mar_Month)

 Now connect the ports year, product, o_Jan_Month, o_Feb_Month, o_Mar_Month of


aggregator transformation to the target.
 Save the mapping.

5. Load Last N Records of File into Target Table – Informatica


Q) How to load only the last N rows from source file into the target table using the mapping in
informatica?

First take a look at the below data in the source file:

Products

--------

Windows

Linux

Unix

Ubuntu

Fedora

Centos

Debian

I want to load only the last record or footer into the target table. The target should contain only the
product "Debain". Follow the below steps for implementing the mapping logic in informatica:

 The mapping flow and the transformations are shown below:


SRC->SQ->EXPRESSION->SORTER->EXPRESSION->FILTER->TGT

 Create a new mapping and drag the source into the mapping. By default, it creates the
source qualifier transformation.
 Now create an expression transformation and drag the ports from source qualifier into the
expression transformation. In the expression transformation, create the below additional
ports and assign the corresponding expressions:

v_count (variable port) = v_count+1

o_count (output port) = v_count

 The output of expression transformation is

Products, o_count

-----------------

Windows, 1

Linux, 2

Unix, 3

Ubuntu, 4

Fedora, 5

Centos, 6

Debian, 7

 Now connect the expression transformation to a sorter transformation and sort the rows on
the o_count port in descending order. The output of sorter transformation is shown below:

Products

--------

Debian

Centos

Fedora
Ubuntu

Unix

Linux

Windows

 Create another expression transformation and connect the Products port of sorter to
expression transformation. Create the following ports in the expression transformation:

v_count (variable port) = v_count+1

o_count (output port) = v_count

 Connect the expression to a filter transformation and specify the filter condition as o_count
= 1.
 Connect the filter to the target and save the mapping.

6. Load Alternative Records / Rows into Multiple Targets – Informatica


Q) How to load records alternatively into multiple targets in informatica? Implement mapping logic
for this.

I have a source file which contains N number of records. I want to load the source records into two
targets, such that first row goes into target 1, second row goes into target2, third row goes into
target3 and so on.

Let see how to create a mapping logic for this in informatica with an example. Consider the
following source flat file as an example:

Products

---------

Informatica

Datastage

Pentaho

MSBI

Oracle

Mysql
The data in the targets should be:

Target1

-------

Informatica

Pentaho

Oracle

Target2

-------

Datastage

MSBI

Mysql

Solution:

The mapping flow and the transformations used are mentioned below:

SRC->SQ->EXP->RTR->TGTS

 First create a new mapping and drag the source into the mapping.
 Create an expression transformation. Drag the ports of source qualifier into the expression
transformation. Create the following additional ports and assign the corresponding
expressions:

v_count (variable port) = v_count+1

o_count (output port) = v_count

 Create a router transformation and drag the ports (products, v_count) from expression
transformation into the router transformation. Create an output group in the router
transformation and specify the following filter condition:

MOD(o_count,2) = 1
 Now connect the output group of the router transformation to the target1 and default group
to target2. Save the mapping.

In the above solution, I have used expression transformation for generating numbers. You can also
use sequence generator transformation for producing sequence values.

7. Load Source File Name in Target – Informatica


Q) How to load the name of the current processing flat file along with the data into the target using
informatica mapping?

We will create a simple pass through mapping to load the data and "file name" from a flat file into
the target. Assume that we have a source file "customers" and want to load this data into the target
"customers_tgt". The structures of source and target are

Source file name: customers.dat

Customer_Id

Location

Target: Customers_TBL

Customer_Id

Location

FileName

The steps involved are:


 Login to the powercenter mapping designer and go to the source analyzer.
 You can create the flat file or import the flat file.
 Once you created a flat file, edit the source and go to the properties tab. Check the option
"Add Currently Processed Flat File Name Port". This option is shown in the below image.
 A new port, "CurrentlyProcessedFileName" is created in the ports tab.
 Now go to the Target Designer or Warehouse Designer and create or import the target
definition. Create a "Filename" port in the target.
 Go to the Mapping designer tab and create new mapping.
 Drag the source and target into the mapping. Connect the appropriate ports of source
qualifier transformation to the target.
 Now create a workflow and session. Edit the session and enter the appropriate values for
source and target connections.
 The mapping flow is shown in the below image

The loading of the filename works for both Direct and Indirect Source filetype. After running the
workflow, the data and the filename will be loaded in to the target. The important point to note is
the complete path of the file will be loaded into the target. This means that the directory path and
the filename will be loaded(example: /informatica/9.1/SrcFiles/Customers.dat).

If you don’t want the directory path and just want the filename to be loaded in to the target, then
follow the below steps:
 Create an expression transformation and drag the ports of source qualifier transformation
into it.
 Edit the expression transformation, go to the ports tab, create an output port and assign the
below expression to it.

REVERSE

SUBSTR

REVERSE(CurrentlyProcessedFileName),
1,

INSTR(REVERSE(CurrentlyProcessedFileName), '/') - 1

 Now connect the appropriate ports of expression transformation to the target definition.

8. Generate rows based on a column value – Informatica


Q) How to generate or load values in to the target table based on a column value using informatica
etl tool.

I have the products table as the source and the data of the products table is shown below.

Table Name: Products

Product Quantity

-----------------

Samsung NULL

Iphone 3

LG 0

Nokia 4

Now i want to duplicate or repeat each product in the source table as many times as the value in the
quantity column. The output is

product Quantity

----------------

Iphone 3

Iphone 3

Iphone 3

Nokia 4
Nokia 4

Nokia 4

Nokia 4

The Samsung and LG products should not be loaded as their quantity is NULL, 0 respectively.

Now create informatica workflow to load the data in to the target table?

Solution:
Follow the below steps

 Create a new mapping in the mapping designer


 Drag the source definition in to the mapping
 Create the java transformation in active mode
 Drag the ports of source qualifier transformation in to the java transformation.
 Now edit the java transformation by double clicking on the title bar of the java
transformation and go to the "Java Code" tab.
 Enter the below java code in the "Java Code" tab.

if (!isNull("quantity"))

double cnt = quantity;

for (int i = 1; i <= quantity; i++)

product = product;

quantity = quantity;

generateRow();

}
 Now compile the java code. The compile button is shown in red circle in the image.
 Connect the ports of the java transformation to the target.
 Save the mapping, create a workflow and run the workflow.

9. Dynamic Target Flat File Name Generation in Informatica


Informatica 8.x or later versions provides a feature for generating the target files dynamically. This
feature allows you to
 Create a new file for every session run
 create a new file for each transaction.
 Informatica provides a special port,"FileName" in the Target file definition. This port you
have to add explicitly. See the below diagram for adding the "FileName" port.

Go to the Target Designer or Warehouse builder and edit the file definition. You have to click on the
button indicated in red color circle to add the special port.

Now we will see some informatica mapping examples for creating the target file name dynamically
and load the data.
1. Generate a new file for every session run.

Whenever the session runs you need to create a new file dynamically and load the source data into
that file. To do this just follow the below steps:

STEP1: Connect the source qualifier to an expression transformation. In the expression


transformation create an output port (call it as File_Name) and assign the expression as
'EMP_'||to_char(sessstarttime, 'YYYYMMDDHH24MISS')||'.dat'

STPE2: Now connect the expression transformation to the target and connect eh File_Name port of
expression transformation to the FileName port of the target file definition.

STEP3: Create a workflow and run the workflow.

Here I have used sessstarttime, as it is constant throughout the session run. If you have used
sysdate, a new file will be created whenever a new transaction occurs in the session run.

The target file names created would look like EMP_20120101125040.dat.

2. Create a new file for every session run. The file name should contain suffix as numbers
(EMP_n.dat)

In the above mapping scenario, the target flat file name contains the suffix as 'timestamp.dat'. Here
we have to create the suffix as a number. So, the file names should looks as EMP_1.dat, EMP_2.dat
and so on. Follow the below steps:

STPE1: Go the mappings parameters and variables -> Create a new variable, $$COUNT_VAR and its
data type should be Integer

STPE2: Connect the source Qualifier to the expression transformation. In the expression
transformation create the following new ports and assign the expressions.

v_count (variable port) = v_count+1

v_file_count (variable port) = IIF(v_count = 1,


SETVARIABLE($$COUNT_VAR,$$COUNT_VAR+1),$$COUNT_VAR)

o_file_name (output port) = 'EMP_'||v_file_count||'.dat'

STEP3: Now connect the expression transformation to the target and connect the o_file_name port
of expression transformation to the FileName port of the target.

3. Create a new file once a day.

You can create a new file only once in a day and can run the session multiple times in the day to
load the data. You can either overwrite the file or append the new data.

This is similar to the first problem. Just change the expression in expression transformation to
'EMP_'||to_char(sessstarttime, 'YYYYMMDD')||'.dat'. To avoid overwriting the file, use Append If
Exists option in the session properties.
4. Create a flat file based on the values in a port.

You can create a new file for each distinct values in a port. As an example consider the employees
table as the source. I want to create a file for each department id and load the appropriate data into
the files.

STEP1: Sort the data on department_id. You can either use the source qualifier or sorter
transformation to sort the data.

STEP2: Connect to the expression transformation. In the expression transformation create the
below ports and assign expressions.

v_curr_dept_id (variable port) = dept_id

v_flag (variable port) = IIF(v_curr_dept_id=v_prev_dept_id,0,1)

v_prev_dept_id (variable port) = dept_id

o_flag (output port) = v_flag

o_file_name (output port) = dept_id||'.dat'

STEP4: Now connect the expression transformation to the transaction control transformation and
specify the transaction control condition as

IIF(o_flag = 1, TC_COMMIT_BEFORE, TC_CONTINUE_TRANSACTION)


STEP5: Now connect to the target file definition.

10. Reverse the Contents of Flat File – Informatica


Q1) I have a flat file, want to reverse the contents of the flat file which means the first record should
come as last record and last record should come as first record and load into the target file.

As an example consider the source flat file data as

Informatica Enterprise Solution

Informatica Power center

Informatica Power exchange

Informatica Data quality

The target flat file data should look as

Informatica Data quality


Informatica Power exchange

Informatica Power center

Informatica Enterprise Solution

Solution:
Follow the below steps for creating the mapping logic

 Create a new mapping.


 Drag the flat file source into the mapping.
 Create an expression transformation and drag the ports of source qualifier transformation
into the expression transformation.
 Create the below additional ports in the expression transformation and assign the
corresponding expressions

Variable port: v_count = v_count+1

Output port o_count = v_count

 Now create a sorter transformation and drag the ports of expression transformation into it.
 In the sorter transformation specify the sort key as o_count and sort order as DESCENDING.
 Drag the target definition into the mapping and connect the ports of sorter transformation
to the target.

Q2) Load the header record of the flat file into first target, footer record into second target and the
remaining records into the third target.

The solution to this problem I have already posted by using aggregator and joiner. Now we will see
how to implement this by reversing the contents of the file.

Solution:

 Connect the source qualifier transformation to the expression transformation. In the


expression transformation create the additional ports as mentioned above.
 Connect the expression transformation to a router. In the router transformation create an
output group and specify the group condition as o_count=1. Connect this output group to a
target and the default group to sorter transformation.
 Sort the data in descending order on o_count port.
 Connect the output of sorter transformation to expression transformation (don’t connect
o_count port).
 Again in the expression transformation create the same additional ports mentioned above.
 Connect this expression transformation to router and create an output group. In the output
group specify the condition as o_count=1 and connect this group to second target. Connect
the default group to the third group.

You might also like