Mapreduce 分组时CouchDB减少错误?
我有一个map reduce查询,它将从多个文档中提取的值聚合到单个聚合文档中,并映射到客户端应用程序的对象结构 我对输出的关键是一个2元素数组,带有数据集标识符和数据集国家段 当我在“精确”分组上运行reduce时,它工作可靠 问题:只要我仅通过数据集标识符聚合数据,即使用组级别1,一些值不正确,大多数不正确的值加倍,但其中一些值具有其他值。是否存在任何已知问题,或者这是我的代码中的错误 运行地图查询后,我有一组值,如下所示:Mapreduce 分组时CouchDB减少错误?,mapreduce,couchdb,Mapreduce,Couchdb,我有一个map reduce查询,它将从多个文档中提取的值聚合到单个聚合文档中,并映射到客户端应用程序的对象结构 我对输出的关键是一个2元素数组,带有数据集标识符和数据集国家段 当我在“精确”分组上运行reduce时,它工作可靠 问题:只要我仅通过数据集标识符聚合数据,即使用组级别1,一些值不正确,大多数不正确的值加倍,但其中一些值具有其他值。是否存在任何已知问题,或者这是我的代码中的错误 运行地图查询后,我有一组值,如下所示: { [type]: { date: {
{ [type]:
{ date:
{ [metric]: [value],
[more metric value combinations]
}
}
}
我有几个文档,并运行了一个reduce,该reduce应该将它们连接起来,以便获得以下结构:
{ type1:
{ date:
{ [metric]: [value],
[more metric value combinations]
},
anotherdate:
{ [metric]: [value],
[more metric value combinations]
},
},
type2:
{ date:
{ [metric]: [value],
[more metric value combinations]
}
},
}
为此,我使用以下reduce查询:
function (keys, values) {
//return values[0];
var returndoc = values[0];
for (var i = 1; i < values.length; i++) {
//Merge the current and previous object
returndoc = MergeDocs(returndoc, values[i]);
}
return returndoc;
}
function MergeDocs(doc1, doc2) {
var types = ['Live', 'Benchmark'];
for (var i = 0; i < types.length; i++) {
var t = types[i];
// if the source document does not Benchmark or Live column,
// create it and add values from the other document.
if (!doc1[t] && doc2[t]) {
doc1[t] = doc2[t];
}
// if the source document has a value and the other
// document exists, sum values.
else if (doc1[t] && doc2[t]) {
doc1[t] = MergeReports(doc1[t], doc2[t]);
}
}
return doc1;
}
function MergeReports(report1, report2) {
// iterate over the dates in the report in the report
for (var date in report2) {
// if there is no value for
if (!report1[date]) {
report1[date] = report2[date];
} else {
for (var metric in report2[date]) {
if (!report1[date][metric]) {
report1[date][metric] = report2[date][metric];
} else {
report1[date][metric] =
report1[date][metric] + report2[date][metric];
}
}
}
}
return report1;
}
功能(键、值){
//返回值[0];
var returndoc=值[0];
对于(变量i=1;i
如果我是你,我会尝试另一条路。尝试使用复合键映射数据(例如,[类型、日期、度量]
)。更好的方法是更改顺序(例如[type,metric,date]),然后您可以使用组级别为2的相同索引来获得所有时间的最大值,最小值,平均值…Aurelian,我喜欢这个想法,并将对其进行测试。然而,对于我来说,仍然存在一个问题,即当我的组_水平准确时,上述方法中的汇总是正确的,但当我将组_水平设置为按国家段汇总时,它们是不正确的(在随机情况下)。因此,目前在我的reduce代码中找不到错误,我不知道如何信任组级别。我不确定您在国家/地区的问题是什么,但是,当您使用复合键时,只需使用组级别
和内置的reduce函数(如\u stats
)就可以完成惊人的事情。