Abhijit
Abhijit

Reputation: 153

Find RowNumber in Talend

How to find row number in Talend Open Studio 6.3?I want to insert rows in a order like

How to achive that?I have generated a sequence column.now how to proceed?is it can be done using tsamplerow? Suppose the Sourcefile is like this:-

EMPNO,EMPNAME,DEPTNO
10,A,1
11,B,2
12,C,3
13,D,4
14,E,1
15,F,1
16,G,2
17,H,3
18,I,4
19,J,2
20,K,3
21,L,1
22,M,2

Upvotes: 1

Views: 7606

Answers (1)

Viki888
Viki888

Reputation: 2774

You can get the current row number by defining an increment value in talend job using Numeric.sequence("s1",1,1).

Note: I have used OP's sample data and divided rows per file as 3. But in OP's scenario it is 10 rows

Below is the sample job which I tried out,

enter image description here

I am generating a sequence number to know the current row number like below

enter image description here

After getting the current row value, I have an another variable which will be incremented for every 3 rows (in my example) like below (in OP's example, it is for every 10 rows.)

enter image description here

This is the expression I am doing on SequenceRow context variable.

context.SequenceRow = (input_row.SequenceNumber > context.RowRangePerFile && input_row.SequenceNumber % context.RowRangePerFile == 1) ? context.SequenceRow+1 : context.SequenceRow;

Finally I am filtering the rows in tMap_2 based on the SequenceRow value, like below,

enter image description here

For out1, the filter condition is (out2.SequenceRow > context.TotalNoOfFiles && out2.SequenceRow % context.TotalNoOfFiles == 1) || out2.SequenceRow == 1

For out3, the filter condition is (out2.SequenceRow > context.TotalNoOfFiles && out2.SequenceRow % context.TotalNoOfFiles == 2) || out2.SequenceRow == 2

For out4, the filter condition is (out2.SequenceRow > context.TotalNoOfFiles && out2.SequenceRow % context.TotalNoOfFiles == 0) || out2.SequenceRow == 3

I have taken your sample data that you have provided in your question,

EMPNO,EMPNAME,DEPTNO
10,A,1
11,B,2
12,C,3
13,D,4
14,E,1
15,F,1
16,G,2
17,H,3
18,I,4
19,J,2
20,K,3
21,L,1
22,M,2

and I am writing every 3 rows in each file and the output I got is

enter image description here

Hope this may help you.

Upvotes: 6

Related Questions