Tag Archives: JAVA

How to generate a star pattern using Informatica with a Java Transformation

This post is in continuation to my earlier post where I described how to generate a star pattern without using a Java transformation. In this post I will describe the method to generate this pattern using Java transformation.

Just for the benefit of readers who have not read my previous post, below is the description of the problem which we will solve using a Java transformation this time.

Question:- To generate a star pattern using Informatica depending on the number of rows in the input file

Suppose there are 7 rows in the source file (any type of data),  the target should have 7 rows of stars in the following pattern:-

FinalStarPattern

Solution:- 

Solving this problem using java transformation is easier than the solution i presented without java transformation in my previous post here.

In this mapping also we will have one pipeline to get the row count of the file using aggregator and set it into a mapping variable.

Second pipeline will use a java transformation and some java code magic to generate the required pattern.

Below is the complete mapping screenshot

map_with_java

Mapping explanation:-

Pipeline 1:- To get the row count in the file. We will use an aggregator to get the count of the rows in the file and an expression to set this count into a mapping variable. Below are the screenshots of the aggregator and the expression used

agg_count

exp_set_var

Pipeline 2:- Here we use the row count, generate a row sequence in and expression and then pass these two values to the java transformation.

exp_sequence

Use Java transformation in Passive mode and create following input and output ports

java_count1

Use below code to generate the star pattern and connect the finalPattern port to target.

java_count2

The code snippet used above is pasted below:-

starpattern = "";
spacepattern = "";

for (int i =0; i < sequence ; i++){

starpattern = starpattern + “*”;

}

for (int j =0; j< rowcount – sequence; j++){

spacepattern = spacepattern + ” “;

}

for (int i =1; i < sequence ; i++){

starpattern = starpattern + “*”;

}

finalPattern = spacepattern + starpattern;

Well that is all about solving this problem using a java transformation.

let me know in comments if you need any clarifications.

How to generate a star pattern using Informatica

Question:- To generate a star pattern using Informatica depending on the number of rows in the input file

Suppose there are 7 rows in the source file (any type of data),  the target should have 7 rows of stars in the following pattern:-

FinalStarPattern

Fig1
Solution:-
There are two ways of doing this – first is to use a Java transformation in passive mode and second is without using a java transformation.

Doing it WITHOUT a Java transformation is challenging hence I will describe this method in this post.  Java transformation based solution will be posted later.

Solution with Java Transformation

We can observe the following about the pattern we are generating:-
1. First line has 7 spaces before the star, second has 6 and so on. Hence the spaces in front are decreasing by 1 for each row. The number of spaces in first row is equal to the number of rows in the source file and number of spaces in front for the last row is zero

2. The number of stars are increasing by two after the first row. 1,3,5 and so on

As we are doing this without a java transformation, the trick lies in how we generate variable number of stars and spaces for each row.

Algorithm:-

1. First generate only the stars as in below pattern

Int1StarPattern

Fig2

2. Sort the above pattern in descending order to generate the pattern below

InvInt1StarPattern

Fig 3

3. Add spaces to the pattern, starting with zero spaces in first line and increasing it by one for each subsequent row to get the following pattern:-

InvFinalStarPattern

Fig 4

4. Sort again, this time in ascending order to get the final pattern

FinalStarPattern

Fig 5

The mapping will also include one pipeline to get the count of the rows in the file. This will be used in the main flow to generate the star pattern.

Complete mapping will look like below (click on the image to enlarge):-

CompleMappingIconic

Mapping Variable:- $$ROWCOUNT

Target Load Plan:- SQ_FF_TEST_SRC first (to count the number of rows)

Mapping Explanation:-

First pipeline to count number of rows in the file

1. Use an aggregator to count the number of rows in the file like below (agg_count_rows)

agg_count_rows

2. Use an expression transformation to assign the count value to a mapping variable (exp_set_row_count_variable)

exp_set_row_count_var

3. Connect both ports from the above expression to a dummy target file.

Second Pipeline to produce star pattern

1. Expression to get the row count variable value in the pipeline and also generate a sequence for each row starting from one (exp_seq_row_count)

exp_seq_row_count

2. Expression to generate intermediate start pattern as in Fig 2 above (exp_int_star_pattern)

exp_int_star_pattern

3. Sorter to sort the intermediate pattern in descending order (sort_desc)

srt_desc

4. Expression to add spaces to the sorted pattern (ex_add_spaces_to_pattern)

exp_add_spaces_to_patt

5. Sorter to again sort the pattern in ascending order to generate the final star pattern (srt_final_pattern)

srt_final_pattern

Place a source file with any number of rows, result you will get is lot of stars!!

Let me know in comments if you need any clarifications or if you have any feedback.