0

基巴真的很帅!

我正在尝试在我的 Rails 应用程序中设置一个 ETL 流程,我将在其中将大量数据从我的 SQL DB 转储到 CSV 文件中。如果我自己实现这一点,我会使用类似的东西find_each一次加载 1000 条记录并将它们分批写入文件。有没有办法使用 Kiba 做到这一点?根据我的理解,默认情况下,所有rows来自 Source 的信息都会传递到 Destination,这对我的情况来说是不可行的。

4

1 回答 1

1

很高兴你喜欢木场!

我会说你的理解是不正确的,这会让你高兴。

这些行在 Kiba 中被一一生成和处理。

要看看事情是如何工作的,我建议你试试这个代码:

class MySource
  def initialize(enumerable)
    @enumerable = enumerable
  end

  def each
    @enumerable.each do |item|
      puts "Source is reading #{item}"
      yield item
    end
  end
end

class MyDestination
  def write(row)
    puts "Destination is writing #{row}"
  end
end

source MySource, (1..10)
destination MyDestination

运行这个,你会看到每个项目都是先读后写的。

现在到您实际的具体案例 - 上面的意思是您可以通过这种方式实现您的源代码:

class ActiveRecord
  def initialize(model:)
    @model = model
  end

  def each
    @model.find_each do |record|
      yield record
    end
  end
end

那么你可以像这样使用它:

source ActiveRecordSource, model: Person.where("age > 21")

(如果您希望每行是多个记录的数组,您也可以利用find_in_batches,但这可能不是您需要的)。

希望这能正确回答您的问题!

于 2019-03-08T17:19:31.003 回答