Dynamic Column Ordering Of Source File Using Talend

complete.job_
Hello Readers,
                      In of our last post we demonstrate how to handle dynamic column ordering from source file using Informatica and finally generating a defined output.So, In this post we will show you how to implement the same functionality using Talend Open Studio,

OVERALL JOB DESC:-

job desc

First, of all create some context variables which will help you to build up the complete job as given in the screen shot.

first variable count is used for some process which i will tell you later during the post, and produce/create a number of context variables as the number of columns in your source file, i have three columns for demo so i have created three variables as var1,var2,var3.

I have a source file which is a comma separated file, which is introduced to the environment using tFileInputDelimited component.
Remenber to bring the data from first row only do not skip the first row.

Now in tJavaRow Component build a schema as given in the snapshot attached.

Finally use the code given below in the tJavaRow component to successfully build the job.

*****************************CODE BEGINS*************************************

//used to perform checks for the first time on the basis of first row.
context.count=context.count+1;
if(context.count==1)
{
System.out.println(“in first if”);
//checks the position of column using first row coming from source file and assign variable value if found similar logic will be applied for all the columns involved.
            if(input_row.field1.equals(“ename”))
            {
            context.var1=1;
            }
            if(input_row.field2.equals(“ename”))
            {
            context.var2=1;
            System.out.println(“ename var2”);
            }
            if(input_row.field3.equals(“ename”))
            {
            context.var3=1;
            }
            if(input_row.field1.equals(“deptno”))
            {
            context.var1=2;
            }
            if(input_row.field2.equals(“deptno”))
            {
            context.var2=2;
            }
            if(input_row.field3.equals(“deptno”))
            {
            context.var3=2;
            }
            if(input_row.field1.equals(“salary”))
            {
            context.var1=3;
            }
            if(input_row.field2.equals(“salary”))
            {
            context.var2=3;
            }
            if(input_row.field3.equals(“salary”))
            {
            context.var3=3;
            }
}
 //at this stage your mapping variables already know which column contains which data so use all the mapping variables and forward the data to the defined schema columns.
if(context.var1.equals(1)){  output_row.ename = input_row.field1;}if(context.var2.equals(1)){  output_row.ename = input_row.field1;}if(context.var3.equals(1)){  output_row.ename = input_row.field1;}if(context.var1.equals(2)){  output_row.deptno = input_row.field2;}if(context.var2.equals(2)){  output_row.deptno = input_row.field2;}if(context.var3.equals(2)){  output_row.deptno = input_row.field2;}if(context.var1.equals(3)){  output_row.salary = input_row.field3;}if(context.var2.equals(3)){  output_row.salary = input_row.field3;}if(context.var3.equals(3)){  output_row.salary = input_row.field3;}
*Take all your source data as a string data type and later on using tConvertTyoe change the schema data type def.

******************************CODE ENDS***************************************

So, this is one of the way you can handle dynamic column ordering of your source file.

Thank u for reading…

Talend Interview Questions

Author : Ankit Kansal & Nayan Naik