2012-09-12 5 views
1

거대한 csv 파일을 가져오고 있습니다. 따라서 가져 오기가 더 빠를 것입니다 (데이터베이스에 직접 가져 오지 않았습니다. 계산이 있습니다). 코드는 다음과 같습니다.Ruby 병렬 csv 가져 오기

def import_shatem 
    require 'csv' 





    CSV.foreach("/#{Rails.public_path}/uploads/hshatem2.csv", {:encoding => 'ISO-8859-15:UTF-8', :col_sep => ';', :row_sep => :auto, :headers => :first_row}) do | row | 

     @eur_cur = Currency.find_by_currency_name("EUR") 
     abrakadabra = row[0].to_s() 
     (ename,esupp) = abrakadabra.split(/_/) 
     eprice = row[6].to_f/@eur_cur.currency_value 
     eqnt = /(\d+)/.match(row[1])[0].to_f 


     if ename.present? && ename.size>3 
     search_condition = "*" + ename.upcase + "*"  

     if esupp.present? 
      #supplier = @suppliers.find{|item| item['SUP_BRAND'] =~ Regexp.new(".*#{esupp}.*") } 
      supplier = Supplier.where("SUP_BRAND like ?", "%#{esupp}%").first 
      logger.warn("!!! *** supp !!!") 

     end 

     if supplier.present? 

      @search = ArtLookup.find(:all, :conditions => ['MATCH (ARL_SEARCH_NUMBER) AGAINST(? IN BOOLEAN MODE) and ARL_KIND = 1', search_condition.gsub(/[^0-9A-Za-z]/, '')]) 
      @articles = Article.find(:all, :conditions => { :ART_ID => @search.map(&:ARL_ART_ID)}) 
      #@art_concret = @articles.find_all{|item| item.ART_ARTICLE_NR.gsub(/[^0-9A-Za-z]/, '').include?(ename.gsub(/[^0-9A-Za-z]/, '')) } 

      @aa = @articles.find{|item| item['ART_SUP_ID']==supplier.SUP_ID} #| @articles 
      if @aa.present? 
      @art = Article.find_by_ART_ID(@aa) 
      end 

      if @art.present? 
      #require 'time_diff' 
      #cur_time = Time.now.strftime('%Y-%m-%d %H:%M') 
      #time_diff_components = Time.diff(@art.datetime_of_update, Time.parse(cur_time)) 
      limit_time = Time.now + 3.hours 
      if (@art.PRICEM.to_f >= eprice.to_f || @art.PRICEM.blank?) #&& @art.datetime_of_update >= limit_time) 
       @art.PRICEM = eprice 
       @art.QUANTITYM = eqnt 
       @art.datetime_of_update = DateTime.now 
       @art.save 
      end 
      end 

     end  
     end 
    end 
    end 

어떻게 병렬 처리 할 수 ​​있습니까? 더 빨리 가져올 수 있습니까?

+1

(유닉스와'명령을 split')와 병렬로 여러 수입을 시작 ... –

+0

귀하의 의견은해야 이 질문에 대한 답이 되십시오. 나는 똑같은 문제를 겪었을 때 똑같은 일을했다. – DNNX

+0

가능한 중복 [Speed ​​up csv import] (http://stackoverflow.com/questions/12166389/speed-up-csv-import) –

답변

0

gem smarter_csv를 확인하십시오! 청크로 된 CSV 파일을 읽을 수 있고 그런 다음 청크 작업을 만들어 해당 청크를 처리하고 데이터베이스에 삽입 할 수 있습니다. 난 그냥 여러 파일로 CSV를 분할 (행 수백만) 비슷한에 있었을 때

https://github.com/tilo/smarter_csv