为什么 CouchDB reduce 函数接收 'keys' 作为参数
Why do CouchDB reduce functions receive 'keys' as an argument
使用 CouchDB reduce 函数:
function(keys, values, rereduce) {
// ...
}
这样称呼:
reduce( [[key1,id1], [key2,id2], [key3,id3]], [value1,value2,value3], false )
问题一
将键传递给 reduce 函数的原因是什么?我只编写了具有 reduce 函数的相对简单的 CouchDB 视图,想知道接收 [key1, docid], [key2, docid], etc
列表的用例是什么。
还有。当 reduce 函数执行时,有没有 key1 != key2 != keyX
的时候?
问题2
CouchDB 的 MapReduce 实现允许 rereduce=true
,在这种情况下,reduce 函数的调用方式如下:
reduce(null, [intermediate1,intermediate2,intermediate3], true)
其中 keys 参数是 null
(与 rereduce=false
不同)。为什么在这种情况下 keys
参数没有用例,如果有 when rereduce=false
?
的用例
What is the use case of keys
argument when rereduce = true
?
没有一个。这就是为什么 keys
参数在这种情况下为空。
来自 the documentation(已强调):
Reduce and Rereduce Functions
redfun(keys, values[, rereduce])
Arguments:
keys
– Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).
values
– Array of map function result values.
rereduce
– Boolean flag to indicate a rereduce run.
也许你想问的是:为什么reduce
和rereduce
使用同一个函数?我希望其中涉及一些历史,但我也可以想象这是因为相同的逻辑可以用于两个函数是很常见的,并且通过不使用单独的函数定义可以减少重复。假设一个简单的 sum
reduce 函数:
function(keys, values) {
return sum(values);
}
这里keys
和rereduce
都可以完全忽略。许多其他(重新)减少功能遵循相同的模式。如果必须使用两个函数,那么这个相同的函数必须指定两次。
回复评论中的附加问题:
what use cases exist for the keys argument when rereduce=false
?
请记住,keys
和 values
可以是任何内容,具体取决于地图功能。一个常见的模式是 emit([foo,bar,baz],null)
。也就是说,如果您关心的所有数据都已存在于键中,则该值可能为空。在这种情况下,任何比简单 sum
更复杂的 reduce 函数都需要使用键。
此外,对于分组操作,使用键是有意义的。考虑一个带有 emit(doc.countryCode, ... )
的映射函数,一个可能的(不完整的)reduce 函数:
function(keys, values, rereduce) {
var sums = {},
if !rereduce {
for (var i = 0; i < keys.length; i++) {
sums[keys[i]]++;
}
}
return sums;
}
然后给出文件:
{"countryCode": "us", ...}
{"countryCode": "us", ...}
{"countryCode": "br", ...}
您将获得(来自 map 函数的)发射值:
["us", ...]
["br", ...]
你会减少结果:
{"us": 2, "br": 1}
使用 CouchDB reduce 函数:
function(keys, values, rereduce) {
// ...
}
这样称呼:
reduce( [[key1,id1], [key2,id2], [key3,id3]], [value1,value2,value3], false )
问题一
将键传递给 reduce 函数的原因是什么?我只编写了具有 reduce 函数的相对简单的 CouchDB 视图,想知道接收 [key1, docid], [key2, docid], etc
列表的用例是什么。
还有。当 reduce 函数执行时,有没有 key1 != key2 != keyX
的时候?
问题2
CouchDB 的 MapReduce 实现允许 rereduce=true
,在这种情况下,reduce 函数的调用方式如下:
reduce(null, [intermediate1,intermediate2,intermediate3], true)
其中 keys 参数是 null
(与 rereduce=false
不同)。为什么在这种情况下 keys
参数没有用例,如果有 when rereduce=false
?
What is the use case of
keys
argument whenrereduce = true
?
没有一个。这就是为什么 keys
参数在这种情况下为空。
来自 the documentation(已强调):
Reduce and Rereduce Functions
redfun(keys, values[, rereduce])
Arguments:
keys
– Array of pairs of docid-key for related map function results. Always null if rereduce is running (has true value).values
– Array of map function result values.rereduce
– Boolean flag to indicate a rereduce run.
也许你想问的是:为什么reduce
和rereduce
使用同一个函数?我希望其中涉及一些历史,但我也可以想象这是因为相同的逻辑可以用于两个函数是很常见的,并且通过不使用单独的函数定义可以减少重复。假设一个简单的 sum
reduce 函数:
function(keys, values) {
return sum(values);
}
这里keys
和rereduce
都可以完全忽略。许多其他(重新)减少功能遵循相同的模式。如果必须使用两个函数,那么这个相同的函数必须指定两次。
回复评论中的附加问题:
what use cases exist for the keys argument when
rereduce=false
?
请记住,keys
和 values
可以是任何内容,具体取决于地图功能。一个常见的模式是 emit([foo,bar,baz],null)
。也就是说,如果您关心的所有数据都已存在于键中,则该值可能为空。在这种情况下,任何比简单 sum
更复杂的 reduce 函数都需要使用键。
此外,对于分组操作,使用键是有意义的。考虑一个带有 emit(doc.countryCode, ... )
的映射函数,一个可能的(不完整的)reduce 函数:
function(keys, values, rereduce) {
var sums = {},
if !rereduce {
for (var i = 0; i < keys.length; i++) {
sums[keys[i]]++;
}
}
return sums;
}
然后给出文件:
{"countryCode": "us", ...}
{"countryCode": "us", ...}
{"countryCode": "br", ...}
您将获得(来自 map 函数的)发射值:
["us", ...]
["br", ...]
你会减少结果:
{"us": 2, "br": 1}