我看到一个令人费解的行为使用mongo执行渐进映射减少任务。输入集合是包含以下内容的大型文档集:
{_id:, url: '从我时髦的网站的一些url '}
下面是我的简单map函数:
map: function() {
emit(this.url, {count: 1, id: this._id});
}
和reduce(包含如下所示的日志的大量调试打印):
reduce: function (key, values) {
var count = 0;
var lastId = null;
var first = null;
if (typeof values[0].id == "undefined") {
print("bad id");
printjson(key);
printjson(values[0]);
return null;
} else {
print ("good id");
printjson(key);
printjson(values[0]);
}
first = ObjectId(values[0].id).getTimestamp();
values.forEach(function(v) {
count += v.count;
last = ObjectId(v.id).getTimestamp();
lastId = v.id;
});
return {
count: count,
first: first,
last: lastId,
lastCounted: lastId
};
}
下面是我调用mapreduce的方法:
mrparams.out = {reduce: this.output};
mrparams.limit = 100;
mrparams.query = {'_id': {'$gt': mongoId(lastId.toHexString())}};
mrparams.finalize = null;
mrdb.mapReduce(this.map, this.reduce, mrparams, function(d) {
console.log("Finished mr", d);
callback();
});
这是以cron类型的方式完成的,以便每个时间间隔,作业都以 lasttid之后的记录开始的限制数量的记录运行。
非常基本的增量映射减少的东西…
但是,当我运行它时,我看到reduce方法的返回值被传递回reduce方法。下面是日志的快照:
XXXgood id"http://www.nytimes.com/2013/04/23/technology/germany-fines-google-over-data-collection.html"{"count": 1, "id": ObjectId("5175a065b25f029a1d0927e6")}
id"http://www.nytimes.com/2013/04/23/world/middleeast/israel-hagel-iran.html"{"count": 1, "id": ObjectId("5175a065d7f115dd41097df6")}
id"http://www.nytimes.com/interactive/2013/04/22/sports/boston-moment.html"{"count": 1, "id": ObjectId("5175a0657c9c963654094d25")}
YYYThu Jun 20 11:42:11 [conn19938] query vox.system.indexes query: {ns: "vox.tmp.mr. "Pi_analytics_spark_trending_inventories_6667_inc "} nreturned:1 reslen:131 0ms[conn19938]查询vox.tmp.mr。Pi_analytics_spark_trending_inventories_6667 nreturned:9 reslen:1716 0ms
ZZZbad id"http://www.nytimes.com/2013/04/22/business/comedy-central-to-host-comedy-festival-on-twitter.html"{"count": 2,"first": ISODate("2013-04-22T20:41:11Z"),"last": ObjectId("5175a067b25f029a1d092802")," lastcounts ": ObjectId("5175a067b25f029a1d092802")}
坏id"http://www.nytimes.com/2013/04/22/business/media/in-boston-cnn-stumbles-in-rush-to-break-news.html"{"count": 7,"first": ISODate("2013-04-22T20:41:09Z"),"last": ObjectId("5175a067d7f115dd41097e3c")," lastcounts ": ObjectId("5175a067d7f115dd41097e3c")}
XXX -从我的map函数发出的一堆记录(包含计数和id的值)YYY——某种我不熟悉的蒙古包ZZZ -在事件发生后,reduce被调用,并输出以前的reduce作业…
TLDR,当我运行map reduce时,reduce运行得很好,直到一个mongo进程运行,然后我开始看到以前的reduce函数的返回值传递到我的reduce函数。
你知道为什么/怎么可能吗?
Running mongo 2.0.6
Thanks in advance
我弄清楚了情况。当将map reduce作业的输出放入已经存在的集合中时,mongo将通过reduce函数传递新简化的文档和已经在输出集合中具有相同键的文档。
如果你从map中发出的值和从reduce中返回的值有一致的格式,这就可以无缝地工作。
这一点并没有被很好地记录下来,但现在我已经明白了,我的沮丧已经转化为一种聪明的感觉。惨痛的教训。