Sunday, 1 March 2020

Read the First N Rows From the flat file


We are getting the CSV file and business want to load top 10 records in the table. With the help of script component, we can load the first n record into the table.
Let’s see this example
Below is the source file.
 

We want to load the first 10 records from this file into the database.
Now I am taking the Data flow task
 

Taking some variables like file path which I am going to read and the Number of records that I want to read.
   
 
Now I am taking Source as a Script component.


Taking both variables in the script component.
Now we need to create output with output columns.
 

By default data type of this column is a string (DT_STR). We need to convert according to our data type.
Go to the script and click on the edit script.
 

In Solution Explorer, we will get the 3 cs class.
 

Buffer Wrapper and Component Wrapper both classes we can’t write in this class.
Buffer Wrapper class
In this class output columns declare.
 


Component Wrapper
In this class variable declares.
 

Now I am writing the code in the main class.
In Pre Execute class we are assigning the variable value.
 

Now I am writing the code CreateNewOutputRows in class.

  public override void CreateNewOutputRows()
    {
      
        int i = 0;
       
        string[] lines = System.IO.File.ReadAllLines(fpath);
        foreach (string line in lines)
        {
            string[] columns = line.Split(',');
            if (i != 0) //skipping the header row
            {
                Output0Buffer.AddRow();
                Output0Buffer.SalesOrderNumber = columns[0];
                Output0Buffer.SalesAmount = float.Parse(columns[1]);
                Output0Buffer.UnitPrice = float.Parse(columns[2]);
                Output0Buffer.ExtendedAmount = float.Parse(columns[3]);
                Output0Buffer.TaxAmt = float.Parse(columns[4]);
            }           
            if (i == x)
                break;
         i = i + 1;
        }
Save and close this window.
Now I am taking the multicast to see the result.
 

Before running this task see the source file.
 

Now I am running this task.
 

I hope this helps this for Reading the First N rows from the flat file.
Thanks for reading!

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts