Rails 4 - 解析大型电子表格时无法分配内存错误

时间:2015-11-22 20:15:25

标签: ruby-on-rails

我正在编写一个应用程序,涉及从远程服务器获取大约400k行的Excel电子表格,并将数据解析为rails应用程序。我正在使用Roo并且我已经搜索过但我还没有找到任何其他问题来解决Roo,Rails和这个特殊错误。我的代码是:

def self.import
f = open("http://www.bced.gov.bc.ca/reporting/odefiles/likert_responses_consolidated_2011to2014.xlsx")

xlsx = Roo::Spreadsheet.open(f, extension: :xlsx)

xlsx.each(name: "SCHOOL_NAME", question: 'SURVEY_QUESTION', many_or_all_responses: 'MANY_OR_ALL_RESPONSES',
          at_no_time_responses: 'AT_NO_TIME_RESPONSES', few_times_responses: 'FEW_TIMES_RESPONSES',
          some_times_responses: 'SOME_TIMES_RESPONSES') do |hash|
  school_name = hash[:name].split(' ')[0...-1].join(' ')
  if School.exists?(name: school_name)
    school = School.find_by_name(school_name)
    if !Survey.exists?(question: hash[:question]) && has_data?(hash)
      school.surveys.create(question: hash[:question], many_or_all_responses: hash[:many_or_all_responses], at_no_time_responses: hash[:at_no_time_responses],
                    few_times_responses: hash[:few_times_responses], some_times_responses: hash[:some_times_responses])
    elsif has_data?(hash)
      survey = Survey.find_by_question(hash[:question])
      survey.many_or_all_responses += hash[:many_or_all_responses]
      survey.at_no_time_responses += hash[:at_no_time_responses]
      survey.few_times_responses += hash[:few_times_responses]
      survey.some_times_responses += hash[:some_times_responses]
    end
  end
end
end

但是,如图所示从远程服务器提取文件时这不起作用,我得到:

[FATAL] failed to allocate memory

每当我尝试调用该函数时。然后我尝试只将几行复制到一个新文件并解析它,该函数工作正常。但是,我希望它能够从遥控器中抓取它而不必先手动修改它。我有什么方法可以这样做吗?

0 个答案:

没有答案