Thursday, December 26, 2013

SCD Type 1 Implementation using Informatica PowerCenter

Unlike SCD Type 2, Slowly Changing Dimension Type 1 do not preserve any history versions of data. This methodology overwrites old data with new data, and therefore stores only the most current information. In this article lets discuss the step by step implementation of SCD Type 1 using Informatica PowerCenter.

The number of records we store in SCD Type 1 do not increase exponentially as this methodology overwrites old data with new data  Hence we may not need the performance improvement techniques used in the SCD Type 2 Tutorial.

Understand the Staging and Dimension Table.

For our demonstration purpose, lets consider the CUSTOMER Dimension. Below are the detailed structure of both staging and dimension table.
Staging Table
In our staging table, we have all the columns required for the dimension table attributes. So no other tables other than Dimension table will be involved in the mapping. Below is the structure of our staging table.
Informatica Source Definition

Key Points

  1. Staging table will have only one days data. Change Data Capture is not in scope.
  2. Data is uniquely identified using CUST_ID.
  3. All attribute required by Dimension Table is available in the staging table

Dimension Table

Here is the structure of our Dimension table.
SCD Type 1 Implementation using Informatica

Key Points

  1. CUST_KEY is the surrogate key.
  2. CUST_ID is the Natural key, hence the unique record identifier.

Mapping Building and Configuration

Step 1
Lets start the mapping building process. For that pull the CUST_STAGE source definition into the mapping designer.
Slowly Changing Dymention Type 1
Step 2
Now using a LookUp Transformation fetch the existing Customer columns from the dimension table T_DIM_CUST. This lookup will give NULL values if the customer is not already existing in the Dimension tables.
  • LookUp Condition : IN_CUST_ID = CUST_ID
  • Return Columns : CUST_KEY
Slowly Changing Dymention Type 1
Step 3
Use an Expression Transformation to identify the records for Insert and Update using below expression. 
    • INS_UPD :- IIF(ISNULL(CUST_KEY),'INS', 'UPD')    
Additionally create two output ports.
    • CREATE_DT :- SYSDATE
    • UPDATE_DT :- SYSDATE
See the structure of the mapping in below image.
Slowly Changing Dymention Type 1
Step 4
Map the columns from the Expression Transformation to a Router Transformation and create two groups (INSERT, UPDATE) in Router Transformation using the below expression. The mapping will look like shown in the image.
  • INSERT :- IIF(INS_UPD='INS',TRUE,FALSE)
  • UPDATE :- IIF(INS_UPD='UPD',TRUE,FALSE)
Slowly Changing Dymention Type 1

INSERT Group

Step 5
Every records coming through the 'INSERT Group' will be inserted into the Dimension table T_DIM_CUST.

Use a Sequence generator transformation to generate surrogate key CUST_KEY as shown in below image. And map the columns from the Router Transformation to the target as shown below image.
Slowly Changing Dymention Type 1

Note : Update Strategy is not required, if the records are set for Insert.

UPDATE Group

Step 6
Records coming from the 'UPDATE Group' will update the customer Dimension with the latest customer attributes. Add an Update Strategy Transformation before the target instance and set it as DD_UPDATE. Below is the structure of the mapping.
Slowly Changing Dymention Type 1
We are done with the mapping building and below is the structure of the completed mapping.
Slowly Changing Dymention Type 1

Workflow and Session Creation

There is not any specific properties required to be given during the session configuration.
informatica worklet
Below is a sample data set taken from the Dimension table T_DIM_CUST.
Initial Inserted Value for CUSTI_ID 1003
Slowly Changing Dymention Type 1
Updated Value for CUSTI_ID 1003
Slowly Changing Dymention Type 1

Design approach to Update Huge Tables Using Oracle MERGE

High level Design Approach.
1.     Use Database JOIN to identify the records to be updated.
2.     Insert the records into TEMP table, which is identified for UPDATE.
3.     Use post session SQL to update the target table.


Design Assumption.
  1. Source and Target tables are relational table.
  2. Both source and target table is on the same database.
  3. Tables are accessible using a single database user. 
Informatica Implementation.
For the demonstration purpose lets consider the Customer Dimension table T_DIM_CUST, which has 100 M records. Each load we are expecting to update 100 K Records records in the Dimension table.

Lets start with the mapping building. As the first step, lets OUTER Join the source table CUST_STAGE and target table T_DIM_CUST. Use the SQL below as the SQL override in source qualifier. 

SELECT
--Columns From Source Tables
CUST_STAGE.CUST_ID,
CUST_STAGE.CUST_NAME,
CUST_STAGE.ADDRESS1,
CUST_STAGE.ADDRESS2,
CUST_STAGE.CITY,
CUST_STAGE.STATE,
CUST_STAGE.ZIP,
--Columns from Target Tables.
--If any column from T_DIM_CUST has NULL value, record to be set as INSERT else UPDATE
T_DIM_CUST.CUST_ID,
T_DIM_CUST.AS_OF_START_DT,
T_DIM_CUST.AS_OF_END_DT
T_DIM_CUST.CUST_NAME,
T_DIM_CUST.ADDRESS1,
T_DIM_CUST.ADDRESS2,
T_DIM_CUST.CITY,
T_DIM_CUST.STATE,
T_DIM_CUST.ZIP
FROM CUST_STAGE
--Outer Join is Used
LEFT OUTER JOIN T_DIM_CUST
ON CUST_STAGE.CUST_ID = T_DIM_CUST.CUST_ID
AND T_DIM_CUST.AS_OF_END_DT = TO_DATE('12-31-4000','MM-DD-YYYY')



Now using a Router Transformation, route the records to INSERT/UPDATE path. Records identified as INSERT will be mapped to T_DIM_CUST and identified as UPDATE will be mapped to T_DIM_CUST_TEMP.

Use T_DIM_CUST_CUST_ID, which is the column from the target table to identify the records to be inserted/updated. If it is NULL, record will be set for insert else record will be set for update. Below is the Router Group Filter Condition and you can see how the mapping looks like in the below image (Below mapping image has not any transformation logic in it).
  • INSERT : IIF(ISNULL( T_DIM_CUST_CUST_ID ), TRUE, FALSE)
  • UPDATE : IIF(NOT ISNULL( T_DIM_CUST_CUST_ID ), TRUE, FALSE)


Now the mapping development is complete,  during the session configuration process, add the below SQL as part of the Post session SQL statement as shown below. This MERGE INTO SQL will update the records in T_DIM_CUST table with the values from T_DIM_CUST_TEMP.
MERGE INTO T_DIM_CUST
USING T_DIM_CUST_TEMP
ON T_DIM_CUST.CUST_ID = T_DIM_CUST_TEMP.CUST_ID
WHEN MATCHED THEN
  UPDATE
     SET T_DIM_CUST.AS_OF_END_DT = T_DIM_CUST_TEMP.AS_OF_END_DT,
         T_DIM_CUST.UPDATE_DT    = T_DIM_CUST_TEMP.UPDATE_DT,
         T_DIM_CUST.CUST_ID      = T_DIM_CUST_TEMP.CUST_ID,
         T_DIM_CUST.CUST_NAME    = T_DIM_CUST_TEMP.CUST_NAME,
         T_DIM_CUST.ADDRESS1     = T_DIM_CUST_TEMP.ADDRESS1,
         T_DIM_CUST.ADDRESS2     = T_DIM_CUST_TEMP.ADDRESS2,
         T_DIM_CUST.CITY         = T_DIM_CUST_TEMP.CITY,
         T_DIM_CUST.STATE        = T_DIM_CUST_TEMP.STATE,
         T_DIM_CUST.ZIP          = T_DIM_CUST_TEMP.ZIP
   WHERE T_DIM_CUST.AS_OF_END_DT = TO_DATE('12-31-4000', 'MM-DD-YYYY')


Friday, November 1, 2013

Look-up On Multiple Records In Informatica

Need to lookup on File with multiple records and extract records with specific condition (For example in this case only account number starting with “12”)

 Source File
ID
User_Name
1
James
2
Williams
3
Ravi

Lookup file
ID
Account_Name
Account_number
1
Acc _James_1
123232
1
Acc _James_2
45778266
2
Acc _ Williams_1
5455546
2
Acc _ Williams_2
1234343
3
Acc _ Ravi_1
254589
3
Acc _ Ravi_2
12544456

Expected Output
ID
Account_Name
Account_number
User_Name
1
Acc _James_1
123232
James
2
Acc _ Williams_2
1234343
Williams
3
Acc _ Ravi_2
12544456
Ravi

a)In the Lookup condition give the option for Multiple Output for the matching records (this option only available for Informatica 9).The output in Lookup will be as below(Data in Expression)

ID
Account_Name
Account_number
User_Name
1
Acc _James_1
123232
James
1
Acc _James_2
45778266
James
2
Acc _ Williams_1
5455546
Williams
2
Acc _ Williams_2
1234343
Williams
3
Acc _ Ravi_1
254589
Ravi
3
Acc _ Ravi_2
12544456
Ravi

b)In expression check for the account starting with “12” using below condition
IIF (SUBSTR (Account_number, 1, 1) =’12’, 1, 0)
c)Next step is quite simple. We can use a filter and take records only were the flag is 1.The output will be as below

ID
Account_Name
Account_number
User_Name
1
Acc _James_1
123232
James
2
Acc_ Williams_2
1234343
Williams
3
Acc _ Ravi_2
12544456
Ravi

Pivoting of records(Pivoting of Employees by Department)

In this scenario we will discuss about how to pivot a set of records based on column:
Source Records: 
Dept_id
Emp_name
10
CLARK
10
KING
10
MILLER
20
JONES
20
FORD
20
ADAMS
20
SMITH
20
SCOTT
30
WARD
30
TURNER
30
ALLEN
30
BLAKE
30
MARTIN
30
JAMES

Expected Output
DEPT_ID
EMP_NAME
10
CLARK|KING|MILLER
20
JONES|FORD|ADAMS|SMITH|SCOTT
30
WARD|TURNER|ALLEN|JAMES|BLAKE|MARTIN