【问题标题】:Can you use MongoDB map/reduce to migrate data?可以使用 MongoDB map/reduce 迁移数据吗?
【发布时间】:2012-05-30 22:53:28
【问题描述】:

我有一个大型集合,我想通过填充一个字段来修改所有文档。

一个简单的例子可能是缓存每个帖子的评论数:

class Post
  field :comment_count, type: Integer
  has_many :comments
end
class Comment
  belongs_to :post
end

我可以通过以下方式串行运行它:

Post.all.each do |p|
  p.udpate_attribute :comment_count, p.comments.count
end

但运行需要 24 小时(大型集合)。我想知道 mongo 的 map/reduce 是否可以用于此?但是我还没有看到一个很好的例子。

我想你会映射出 cmets 集合,然后将减少的结果存储在 posts 集合中。我在正确的轨道上吗?

【问题讨论】:

    标签: ruby-on-rails mapreduce mongoid


    【解决方案1】:

    您可以使用 MongoDB map/reduce 来“帮助”迁移数据, 不幸的是,您不能使用它来进行完全的服务器端迁移。 你走在正确的轨道上,基本的想法是:

    1. 将每条评论映射到emit(post_id, {comment_count: 1}) ---> {_id: post_id, value: {comment_count: 1}}
    2. 减少到值 {comment_count: N} N 是计数和 ---> {_id: post_id, value: {comment_count: N}}
    3. 指定输出选项 {reduce: 'posts'} 以将 map/reduce comment_counts 的结果减少回帖子集合中

    经过一番广泛的调查,我发现你可以接近, 但是有一个问题会阻止您完全进行服务器端迁移。 reduce 的结果具有形状 {_id: KEY, value: MAP_REDUCE_VALUE}。 我们现在被这个形状困住了,似乎没有办法绕过它。 因此,您既不能获取此形状之外的完整原始文档作为减少的输入(实际上,您将丢失此形状之外的数据), 由于减少,也不会更新此形状之外的文档。 因此,您的帖子集合的“最终”更新必须通过客户端以编程方式完成。 看起来修复这个问题将是一个很好的修改请求。

    下面找到一个工作示例,该示例演示了在 Ruby 中使用 MongoDB map/reduce 来计算所有 comment_counts。 然后我以编程方式使用 map_reduce_results 集合来更新帖子集合中的 comment_count。 reduce 函数被从尝试中剥离出来: {reduce: 'posts'}

    您可以通过一些实验来验证我的答案, 或者,如果您愿意,我可以根据要求发布完全不工作的服务器端尝试, 配有固定型号。 希望这有助于理解 Ruby 中的 MongoDB map/reduce。

    test/unit/comment_test.rb

    require 'test_helper'
    
    class CommentTest < ActiveSupport::TestCase
      def setup
        @map_reduce_results_name = 'map_reduce_results'
        delete_all
      end
    
      def delete_all
        Post.delete_all
        Comment.delete_all
        Mongoid.database.drop_collection(@map_reduce_results_name)
      end
    
      def dump(title = nil)
        yield
        puts title
        Post.all.to_a.each do |post|
          puts "#{post.to_json} #{post.comments.collect(&:text).to_json}"
        end
      end
    
      def generate
        (2+rand(2)).times do |p|
          post = Post.create(text: 'post_' + p.to_s)
          comments = (2+rand(3)).times.collect do |c|
            Comment.create(text: "post_#{p} comment_#{c}")
          end
          post.comments = comments
        end
      end
    
      def generate_and_migrate(title = nil)
        dump(title + ' generate:') { generate }
        dump(title + ' migrate:') { yield }
      end
    
      test "map reduce migration" do
        generate_and_migrate('programmatic') do
          Post.all.each do |p|
            p.update_attribute :comment_count, p.comments.count
          end
        end
        delete_all
        generate_and_migrate('map/reduce') do
          map = "function() { emit( this.post_id, {comment_count: 1} ); }"
          reduce = <<-EOF
            function(key, values) {
              var result = {comment_count: 0};
              values.forEach(function(value) { result.comment_count += value.comment_count; });
              return result;
            }
          EOF
          out = @map_reduce_results_name #{reduce: 'posts'}
          result_coll = Comment.collection.map_reduce(map, reduce, out: out)
          puts "#{@map_reduce_results_name}:"
          result_coll.find.each do |doc|
            p doc
            Post.find(doc['_id']).update_attribute :comment_count, doc['value']['comment_count'].to_i
          end
        end
      end
    end
    

    测试输出(抱歉混用 JSON 和 Ruby 检查)

    Run options: --name=test_map_reduce_migration
    
    # Running tests:
    
    programmatic generate:
    {"_id":"4fcae3bde4d30b21e2000001","comment_count":null,"text":"post_0"} ["post_0 comment_0","post_0 comment_1","post_0 comment_2"]
    {"_id":"4fcae3bde4d30b21e2000005","comment_count":null,"text":"post_1"} ["post_1 comment_1","post_1 comment_0","post_1 comment_2","post_1 comment_3"]
    {"_id":"4fcae3bde4d30b21e200000a","comment_count":null,"text":"post_2"} ["post_2 comment_1","post_2 comment_3","post_2 comment_0","post_2 comment_2"]
    programmatic migrate:
    {"_id":"4fcae3bde4d30b21e2000001","comment_count":3,"text":"post_0"} ["post_0 comment_0","post_0 comment_1","post_0 comment_2"]
    {"_id":"4fcae3bde4d30b21e2000005","comment_count":4,"text":"post_1"} ["post_1 comment_1","post_1 comment_0","post_1 comment_2","post_1 comment_3"]
    {"_id":"4fcae3bde4d30b21e200000a","comment_count":4,"text":"post_2"} ["post_2 comment_1","post_2 comment_3","post_2 comment_0","post_2 comment_2"]
    map/reduce generate:
    {"_id":"4fcae3bee4d30b21e200000f","comment_count":null,"text":"post_0"} ["post_0 comment_0","post_0 comment_1"]
    {"_id":"4fcae3bee4d30b21e2000012","comment_count":null,"text":"post_1"} ["post_1 comment_2","post_1 comment_0","post_1 comment_1"]
    {"_id":"4fcae3bee4d30b21e2000016","comment_count":null,"text":"post_2"} ["post_2 comment_0","post_2 comment_1","post_2 comment_2","post_2 comment_3"]
    map_reduce_results:
    {"_id"=>BSON::ObjectId('4fcae3bee4d30b21e200000f'), "value"=>{"comment_count"=>2.0}}
    {"_id"=>BSON::ObjectId('4fcae3bee4d30b21e2000012'), "value"=>{"comment_count"=>3.0}}
    {"_id"=>BSON::ObjectId('4fcae3bee4d30b21e2000016'), "value"=>{"comment_count"=>4.0}}
    map/reduce migrate:
    {"_id":"4fcae3bee4d30b21e200000f","comment_count":2,"text":"post_0"} ["post_0 comment_0","post_0 comment_1"]
    {"_id":"4fcae3bee4d30b21e2000012","comment_count":3,"text":"post_1"} ["post_1 comment_2","post_1 comment_0","post_1 comment_1"]
    {"_id":"4fcae3bee4d30b21e2000016","comment_count":4,"text":"post_2"} ["post_2 comment_0","post_2 comment_1","post_2 comment_2","post_2 comment_3"]
    .
    
    Finished tests in 0.072870s, 13.7231 tests/s, 0.0000 assertions/s.
    
    1 tests, 0 assertions, 0 failures, 0 errors, 0 skips
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多