为什么CouchDB reduce函数接收'keys'作为参数 [英] Why do CouchDB reduce functions receive 'keys' as an argument

查看:88
本文介绍了为什么CouchDB reduce函数接收'keys'作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有CouchDB减少功能:

With a CouchDB reduce function:

function(keys, values, rereduce) {
  // ...
}

被这样称呼:

reduce(  [[key1,id1], [key2,id2], [key3,id3]],    [value1,value2,value3],   false   ) 

问题1

Question 1

将键传递给reduce函数的原因是什么?我只编写了相对简单的带有reduce函数的CouchDB视图,并且想知道用什么来接收[key1, docid], [key2, docid], etc的列表.

What is the reason for passing keys to the reduce function? I have only written relatively simple CouchDB views with reduce functions and would like to know what the use case is for receiving a list of [key1, docid], [key2, docid], etc is.

也. key1 != key2 != keyX是否存在执行reduce函数的时间?

Also. is there ever a time when key1 != key2 != keyX when a reduce function executes?

问题2

Question 2

CouchDB的MapReduce实现允许rereduce=true,在这种情况下,reduce函数的调用方式如下:

CouchDB's implementation of MapReduce allows for rereduce=true, in which case the reduce function is called like this:

reduce(null,  [intermediate1,intermediate2,intermediate3],  true)

keys参数为null的地方(与rereduce=false不同).如果在rereduce=false时有用法,为什么在这种情况下没有keys自变量用例?

Where the keys argument is null (unlike when rereduce=false). Why would there not be a use case for a keys argument in this case if there was a use for when rereduce=false?

推荐答案

rereduce = truekeys参数的用例是什么?

What is the use case of keys argument when rereduce = true?

没有一个.这就是在这种情况下keys参数为null的原因.

There isn't one. That's why the keys argument is null in this case.

文档(添加了重点):

减少和减少功能

redfun(键,值[,减少])

Reduce and Rereduce Functions

redfun(keys, values[, rereduce])

  • keys –相关映射函数结果的多对docid-key数组. 如果正在运行rereduce(具有真实值),则始终为null.
  • values –映射函数结果值的数组.
  • rereduce –表示减少运行的布尔值标志.
  • 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.

也许您要问的意思是:为什么reducerereduce都使用相同的功能?我希望其中涉及一些历史,但是我也可以想象这是因为在两个函数中都可以使用相同的逻辑是很常见的,并且由于没有单独的函数定义,可以减少重复.假设一个简单的sum reduce函数:

Perhaps what you're meaning to ask is: Why is the same function used for both reduce and rereduce? I expect there's some history involved, but I can also imagine that it's because it's quite common that the same logic can be used for both functions, and by not having separate function definitions duplication can be reduced. Suppose a simple sum reduce function:

function(keys, values) {
    return sum(values);
}

keysrereduce都可以完全忽略.许多其他(重新)简化功能遵循相同的模式.如果必须使用两个功能,则必须指定两次相同的功能.

Here both keys and rereduce can be ignored entirely. Many other (re)reduce functions follow the same pattern. If two functions had to be used, then this identical function would have to be specified twice.

针对评论中的其他问题:

In response to the additional question in comments:

rereduce=false时,keys参数存在哪些用例?

what use cases exist for the keys argument when rereduce=false?

请记住,根据地图功能,keysvalues可以是任何东西.常见的模式是emit([foo,bar,baz],null).也就是说,如果密钥中已经存在您关心的所有数据,则该值可以为null.在这种情况下,任何比简单的sum更复杂的reduce函数都将需要使用键.

Remember, keys and values can be anything, based on the map function. A common pattern is to emit([foo,bar,baz],null). That is to say, the value may be null, if all the data you care about is already present in the key. In such a case, any reduce function more complex than a simple sum would require use of the keys.

此外,对于分组操作,使用键是有意义的.考虑具有emit(doc.countryCode, ... )的映射函数,这是一个可能的(不完整的)约简函数:

Further, for grouping operations, using the keys makes sense. Consider a map function with emit(doc.countryCode, ... ), a possible (incomplete) reduce function:

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", ...}
  • {"countryCode": "us", ...}
  • {"countryCode": "us", ...}
  • {"countryCode": "br", ...}

您将获得(从map函数中)的发射值:

You'd get emitted values (from the map function) of:

  • ["us", ...]
  • ["br", ...]
  • ["us", ...]
  • ["br", ...]

您将减少以下结果:

{"us": 2, "br": 1}

这篇关于为什么CouchDB reduce函数接收'keys'作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆