Skip to main content

Get N records from a CSV file and Store it in an Array

 

Objective of the task is to pick up N(for example 5,10,100, etc..) records from csv File and put in an array. This can also be generic, you can post those records to web API OR perform any operations as per your requirement.

Steps:

  •  Count total number of rows(records) in a file. [use component tFileRowCount].



  •  Define 3 variables. one for total count of records, second for header, third is to limit the records. [use component tSetGlobalVar]


in the screenshot, i have used Header as 1 as the csf file contains Header. Also, you can start from specific records based on this value. Also Limit is set 10. you can take this value from context and configure it as per your needs.

  • Apply loops until the variable Header is less than or equals to total row count. [use tLoop, in the condition use Header <= totalRowCount]





  •  Get the data from delimated file, add Header and Limit variables in the coppresponding place. [use component tFileInputDelimated]


The number in Header skips the number of rows specified in header and the limit extracts the number of rows specified in it.

  •  For storing those data in an array use component use component tJavaFlex. Define ArrayList in it and store the N items in an array.  




In the tJavaFlex Component, an ArrayList is defined first, and all the elements of the csv file are added in this array. And finally this value is set in a global Variable named List.
If you print the value list in next tJava component, you will get the desired result.

Output Screenshot:




Hope this tutorials helped you.. thanks for visiting the page..




Comments

Popular posts from this blog

Convert the Given Date to Any Format

 Suppose you have a date in the format yyyy/MM/DD HH:MM:ss, and you want to convert like DD-MM-yyyy HH:MM:ss. How will you do in Talend. I am going to show you demo. 1. If you want to display the current System date to any format, you can use talend Function as:  TalendDate.getDate("yyyy-MM-dd HH:MM:ss").  This will simply display the current date time in this format. For Example: 2021-09-23 15:09:56 Screenshot: Output of the Above code is: You can use this code whereever you want for example in the tJavaRow, tSetGlobalVar, tFixedFlowInput, tMap components etc... On the similar manner if we have date already given then for converting that date to specific format, first of all you need to parse the date and then give pattern of specific format. For parsing the date there is a function : suppose date = "2018/09/20 12:34:45" for parsing this date,  TalendDate.parseDate("yyyy/MM/DD HH:mm:ss", date); After parsing, you have to format the date using the specifi...

How to Create a Sequence in Talend

 The sequence routine allows you to create automatically incremented IDs, using a tJava component or any components for example tMap, tFixedFlowInput etc.. The code for defining the sequence is:  Numeric.sequence("s1",1,1). whenever you use this sequence Next time, it's value will be incremented by 1. For Example: following code will print 1,2,3 in a seperate line: System.out.println(Numeric.sequence("s1",1,1));  System.out.println(Numeric.sequence("s1",1,1)); System.out.println(Numeric.sequence("s1",1,1)); if you want your sequence to be printed from specific number, then use that number at the between, the last interger is for the interval. The following code starts a sequence from 102. Numeric.sequence("s1",102,1).