Saturday, November 17, 2012

Importing the csv data from a specific location through Rake Task

1. Importing the csv data from a specific location through Rake Task:

We can import data from csv file which is placed in csv folder in our Rails application root directory. Here we checks that csv file name should start with eq2 and ends with .csv, only these csv files should be picked to parse. All such csv files will be parsed. Place the given below code in "lib/tasks" folder as a rake task and run to import data.

Here are code details:


namespace :import do

  desc 'import csv files of eq2'
  task :eq2 => :environment do
    root = Rails.root.to_s
    require 'csv'

    #importing eq2  csv files where name starting from eq2 and ending with csv
    eq_files = Dir.glob("#{root}/csv/eq2*.csv")
    puts "Found eq2 csv file named  #{eq_files}"

    if !eq_files.blank?
      eq_files.each do |eq_file|
        puts "eq1 file is not blank #{eq_file}"
       
        @parsed_file = CSV::Reader.parse(File.open(eq_file, "rb"))
        n = 0
        @parsed_file.each_with_index do |row, i|
          next if i == 0 || i == 1
          break if row[2].blank?

          rsymbol = row[2].strip if !row[0].blank?
          rseries = row[3]

          presentinmaster = Masterdata.find(:first, :conditions =>["nse_symbol='#{rsymbol}' "])
          
          #Accept only those records which fulfill following conditions
          if !presentinmaster.blank? !rsymbol.blank? && !rseries.blank? && (rseries == "symbol" or rseries == "EQ")
            c=Eqmasterdata.new
            c.symbol= rsymbol
            c.series=rseries
            c.masterdatas_id = presentinmaster.id
            if c.save
              puts "CSV Import Successful,  #{n} new records added to data base"
              n=n+1
              GC.start if n%50==0
            else
              puts "EQ2 could not be save, error is #{c.errors}"
              #else writing error in different table
              AllError.create(:error_of => 'EQ2', :error_detail => c.errors, :error_summary => "Data not saved")
            end
          end

        end
      end
    end
  end
end

No comments:

Post a Comment