1

需要使用 activerecord-import 和 ruby​​zip gem 将 zip 文件中的 csv 数据导入我的产品模型。

此代码有效(下载 zip 并显示 csv 名称)

desc "Import products data from web"
task import_product: :environment do
    url = "https://example.com"
    dir = "db/example_zip.zip"

    File.open(dir, "wb") do |f|
        f.write HTTParty.get(url).body
    end

    Zip::File.open(dir) do |zip|
        zip.each do |entry|
            entry.name
        end
    end
end

在“zip.each 循环”中,我尝试了这个:

items = []
CSV.foreach(entry, headers: true) do |row|
  items << Item.new(row.to_h)
end
Item.import(items)

我有以下错误 TypeError: no implicit conversion of Zip::Entry into String

根据本教程:https ://mattboldt.com/importing-massive-data-into-rails/

用这个 csv 刷新我的产品模型数据的最佳方法是什么?我必须将文件读入内存(entry.get_input_stream.read)还是保存文件然后导入?

谢谢你的帮助

4

2 回答 2

2

TypeError: no implicit conversion of Zip::Entry into String引发的异常是因为CSV.foreach方法接受文件路径(它是一个String对象)作为参数,但您将其发送一个Zip::Entry对象。

您可以简单地提取 zip 文件并将其内容直接加载到内存中:

Zip::File.open(dir) do |zip|
  zip.each do |entry|
    items = []
    CSV.new(entry.get_input_stream.read, headers: true).each do |row|
      items << Item.new(row.to_h)
    end
    Item.import(items)
  end
end

或者如果csv文件太大,你可以把解压后的文件持久化,然后CSV.foreach用来加载这些文件:

Zip::File.open(dir) do |zip|
  zip.each do |entry|
    csv_file = File.join(File.dirname(dir), entry.name)
    entry.extract(csv_file)
    items = []
    CSV.foreach(csv_file, headers: true) do |row|
      items << Item.new(row.to_h)
    end
    Item.import(items)
  end
end

您可以在这些文档中阅读更多内容:

于 2018-12-21T11:13:06.377 回答
0

最后,这是我下载 zip 文件并将数据导入我的产品模型的代码

require 'zip'
require 'httparty'
require 'active_record'
require 'activerecord-import'

namespace :affiliate_datafeed do
    desc "Import products data from Awin"
    task import_product_awin: :environment do
        url = "https://productdata.awin.com"
        dir = "db/affiliate_datafeed/awin.zip"

        File.open(dir, "wb") do |f| 
            f.write HTTParty.get(url).body
        end

        zip_file = Zip::File.open(dir)
        entry = zip_file.glob('*.csv').first
        csv_text = entry.get_input_stream.read
        products = []

        CSV.parse(csv_text, :headers=>true).each do |row|
            products << Product.new(row.to_h)
        end
        Product.import(products)
  end
end

但是下一个问题是,只有在产品不存在或 last_updated 字段中有新日期的情况下,如何更新产品数据库?刷新大型数据库的最佳方法是什么?谢谢

于 2018-12-26T11:00:10.020 回答