所以这很奇怪.我正在尝试使用mapreduce在一个唯一的端口下对datetime/metrics进行分组:
文件布局:
{
"_id" : ObjectId("5069d68700a2934015000000"),
"port_name" : "CL1-A",
"metric" : "340.0",
"port_number" : "0",
"datetime" : ISODate("2012-09-30T13:44:00Z"),
"array_serial" : "12345"
}
Run Code Online (Sandbox Code Playgroud)
和mapreduce函数:
var query = {
'array_serial' : array,
'port_name' : { $in : ports },
'datetime' : { $gte : from, $lte : to}
}
var map = function() {
emit( { portname : this.port_name } , { datetime : this.datetime,
metric : this.metric });
}
var reduce = function(key, values) {
var res = { dates : [], metrics : [], count : 0}
values.forEach(function(value){
res.dates.push(value.datetime);
res.metrics.push(value.metric);
res.count++;
})
return res;
}
var command = {
mapreduce : collection,
map : map.toString(),
reduce : reduce.toString(),
query : query,
out : { inline : 1 }
}
mongoose.connection.db.executeDbCommand(command, function(err, dbres){
if(err) throw err;
console.log(dbres.documents);
res.json(dbres.documents[0].results);
})
Run Code Online (Sandbox Code Playgroud)
如果请求少量记录,比如说5或10,甚至60,我会得到所有数据,这是我所期待的.较大的查询返回截断值....
我刚做了一些测试,似乎它将记录输出限制为100?这是一个微小的数据,当我运行一个24小时的查询时,我会期待1440条记录回来......我只是把它收到了80.:
这是预期的吗?我没有在任何地方指明限制......
更多数据:
查询2012-10-01T23:00 - 2012-10-02T00:39(100分钟)的记录正确返回:
[
{
"_id": {
"portname": "CL1-A"
},
"value": {
"dates": [
"2012-10-01T23:00:00.000Z",
"2012-10-01T23:01:00.000Z",
"2012-10-01T23:02:00.000Z",
...cut...
"2012-10-02T00:37:00.000Z",
"2012-10-02T00:38:00.000Z",
"2012-10-02T00:39:00.000Z"
],
"metrics": [
"1596.0",
"1562.0",
"1445.0",
...cut...
"774.0",
"493.0",
"342.0"
],
"count": 100
}
}
]
Run Code Online (Sandbox Code Playgroud)
...再向查询添加一分钟2012-10-01T23:00 - 2012-10-02T00:39(101分钟):
[
{
"_id": {
"portname": "CL1-A"
},
"value": {
"dates": [
null,
"2012-10-02T00:40:00.000Z"
],
"metrics": [
null,
"487.0"
],
"count": 2
}
}
]
Run Code Online (Sandbox Code Playgroud)
该dbres.documents对象显示正确的预期发出记录:
[ { results: [ [Object] ],
timeMillis: 8,
counts: { input: 101, emit: 101, reduce: 2, output: 1 },
ok: 1 } ]
Run Code Online (Sandbox Code Playgroud)
......数据在某处丢失了吗?
Asy*_*sky 13
MapReduce的第一条规则:
您将从Reduce返回与Map中的密钥完全相同的格式.
MapReduce的第二条规则:
你应该减少传递的值数组,以便根据需要减少次数.可以多次调用减少功能.
你在reduce的实现中已经破坏了这两个规则.
您的Map函数正在发出键值对.
key:端口名称(您只需将名称作为键而不是文档发出)
值:表示需要累积的三件事的文档(日期,度量,计数)
试试这个:
map = function() { // if you want to reduce to an array you have to emit arrays
emit ( this.port_name, { dates : [this.datetime], metrics : [this.metric], count: 1 });
}
reduce = function(key, values) { // for each key you get an array of values
var res = { dates: [], metrics: [], count: 0 }; // you must reduce them to one
values.forEach(function(value) {
res.dates = value.dates.concat(res.dates);
res.metrics = value.metrics.concat(res.metrics);
res.count += value.count; // VERY IMPORTANT reduce result may be re-reduced
})
return res;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3033 次 |
| 最近记录: |