How to generate multiple rows from a single row in Talend?

0 votes

I have a file with data in the following format:

id  | name  | dateFrom   | dateTo

101 | Allen | 12-04-2018 | 17-04-2018

Can someone help me in splitting up this single row of into multiple rows where the number of output rows will depend on the difference between the dates? For example, the output must look like:

New_id | id  | name |   dateFrom   |  dateTo

1        101  Allen    12-04-2018    13-04-2018

2        101  Allen    13-04-2018    14-04-2018

3        101  Allen    14-04-2018    15-04-2018

4        101  Allen    15-04-2018    16-04-2018

5        101  Allen    16-04-2018    17-04-2018

 

Apr 18, 2018 in Talend by misc.edu04
• 1,420 points
1,452 views

1 answer to this question.

0 votes

I think using tJavaFlex, tMap and tFlowToIterate components you can easily split the data. You can start off by arranging the components as shown below:

image

Open the Map Editor and map the data as shown:

image

In the Map Editor output table add a new row called “iterations” of “long” type to hold the difference between the dates.

tFlowToIterate components need no configurations to be done. It will by default pass on the values to the tJavaFlex.

Add the below code to the tJavaFlex component:

Start Code:

 // start part of your Java code

int currentId = (Integer)globalMap.get("res.id");

String currentName = (String)globalMap.get("res.name");

Long iterations = (Long)globalMap.get("res.iterations");

Date dateFrom = (java.util.Date)globalMap.get("res.dateFrom");

Date dateTo = (java.util.Date)globalMap.get("res.dateTo");

for(int i=0; i<((Long)globalMap.get("res.iterations")); i++) { 

Main Code:

row2.new_id = i+1;

row2.id = currentId;

row2.name = currentName;

row2.dateFrom = TalendDate.addDate(dateFrom, i, "dd");

row2.dateTo = TalendDate.addDate(dateFrom, i+1, "dd");

End Code:

}

Like the tFlowToIterate component tLogRow also do not need any configurations to be done. You can just go ahead and execute the job which will give you the below output. Hope this was what you were looking for. Cheers!!

image

answered Apr 18, 2018 by code.reaper12
• 3,450 points

Related Questions In Talend

0 votes
1 answer

In Talend, how to specify multiple conditions as expression

@Palaniswamy, have a look at this discussion ...READ MORE

answered Aug 5 in Talend by Vardhan
• 12,730 points
162 views
0 votes
1 answer

Using tHiveRow to execute multiple Hive queries in Talend

In order to execute multiple queries, you ...READ MORE

answered Apr 14, 2018 in Talend by misc.edu04
• 1,420 points
829 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Looping through all the schemas in Talend

It is really simple to make tOracleInput ...READ MORE

answered Apr 10, 2018 in Talend by code.reaper12
• 3,450 points
123 views
0 votes
1 answer

Parsing JSON in Talend

While working with JSON files, one thing ...READ MORE

answered Apr 2, 2018 in Talend by code.reaper12
• 3,450 points
552 views
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

How to load a set of files into database using Talend

Using tFileList component,  you can read all the files present ...READ MORE

answered Apr 16, 2018 in Talend by code.reaper12
• 3,450 points
698 views