【发布时间】:2012-03-26 03:52:36
【问题描述】:
我正在使用 Perl v5.12 和 MongoDB 包 v0.45。
我想运行一个 MapReduce 作业来创建一个新集合,然后我将创建一个游标供以后使用。 我的另一个愿望是这个作业在副本而不是主服务器上运行。
根据 perl 文档中的定义,MapReduce 作业将使用run_command 方法执行。
当我执行 perl 脚本时,我得到:
Mongo error: not master at perlib/Connections.pm line 63.
阅读 CPAN 上的 MongoDB 文档后,似乎只有一种方法可以使游标从副本中读取。因此该方法不适用于对run_command() 的调用。
这是我的代码:
sub get_data {
my $self = shift;
my $dbh = shift;
my $collection_h = shift;
my $since_time = $self->get_date_time(shift);
my $loop_limit = $self->get_data_limit(shift);
my %data = ();
my $ctr = 0;
my $temp_collection='temp_collection';
my $ids = $dbh->run_command([
"mapreduce" => $collection_h->{'name'}
,"map" => _get_map()
,"reduce" => _get_reduce()
,"query" => {'to.id' => {'$exists'=>'true'}, 'updatedDate' => { '$gte' => $since_time }}
,"out" => $temp_collection
]);
die ("Mongo error: $ids") unless ref($ids) eq 'HASH';
# next we create a cursor to this new collection
my $cfs_h = $dbh->$temp_collection;
my $id_cursor = $cfs_h->find()->limit($loop_limit);
$id_cursor->slave_okay(1);
$id_cursor->immortal(1);
...
}
sub _get_map {
return "function() {emit(this.to.id, 1);}";
}
sub _get_reduce {
return "function(k,vals) {return 1;}"
}
有没有人遇到过在副本上尝试使用 MapReduce 的问题? 你成功了吗? 如果是这样,您能否分享一下您是如何做到的。
【问题讨论】:
标签: perl mongodb mapreduce master-slave