MapReduce 用于计算参数值

发布于 2024-10-08 22:29:20 字数 448 浏览 4 评论 0原文

我有这样的文档:

{
   "_id": ObjectId("4d17c7963ffcf60c1100002f"),
   "title": "Text",
   "params": {
     "brand": "BMW",
     "model": "i3"
    }
}

{
   "_id": ObjectId("4d17c7963ffcf60c1100002f"),
   "title": "Text",
   "params": {
     "brand": "BMW",
     "model": "i5"
    }
}

我需要的是每个参数值的计数。就像:

brand
---------
BMW (2)

model
---------
i3 (1)
i5 (1)

我想我必须编写映射/归约函数。我该怎么做?谢谢。

I have document like this:

{
   "_id": ObjectId("4d17c7963ffcf60c1100002f"),
   "title": "Text",
   "params": {
     "brand": "BMW",
     "model": "i3"
    }
}

{
   "_id": ObjectId("4d17c7963ffcf60c1100002f"),
   "title": "Text",
   "params": {
     "brand": "BMW",
     "model": "i5"
    }
}

What i need is the count of every params values. like:

brand
---------
BMW (2)

model
---------
i3 (1)
i5 (1)

I think i have to write map/reduce functions. How can i do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2024-10-15 22:29:20

我想我必须编写map/reduce函数。

是的,你需要为此进行映射缩减。对于一些简单的 Map-Reduce 示例,请查看此处

对于您的特定情况,您首先需要更改对输出的期望。 Map/Reduce 的输出是一个集合。该集合将看起来像这样(在您的情况下):

{ key : { 'brand' : 'bmw' }, value : 2 }
{ key : { 'model' : 'i5' }, value : 1 }

要生成此集合,您将需要一个“map”函数和一个“reduce”函数。 “map”函数将发出一个键和一个值。键是 params 的每个元素,值是 1 的计数。“reduce”函数接受一个键和一个值数组,并仅返回一个值。您的问题与 MongoDB 网站 上的示例基本相同:

map = function() {
    if (!this.params) {
        return;
    }
    for (index in this.params) {
        emit(this.params[index], 1);
    }
}

reduce = function(previous, current) {
    var count = 0;

    for (index in current) {
        count += current[index];
    }

    return count;
}

I think i have to write map/reduce functions.

Yes you need a map-reduce for this. For some simple map-reduce examples, please look here.

For your particular case, you first need to change your expectation of the output. The output of the map / reduce is a collection. The collection will look (in your case) something like this:

{ key : { 'brand' : 'bmw' }, value : 2 }
{ key : { 'model' : 'i5' }, value : 1 }

To generate this set you will need a "map" function and a "reduce" function. The "map" function will emit a key and a value. The key is each element of params, the value is the count of 1. The "reduce" function accepts a key and an array of values and returns just a single value. Your question is basically the same as this example on the MongoDB site:

map = function() {
    if (!this.params) {
        return;
    }
    for (index in this.params) {
        emit(this.params[index], 1);
    }
}

reduce = function(previous, current) {
    var count = 0;

    for (index in current) {
        count += current[index];
    }

    return count;
}
孤檠 2024-10-15 22:29:20

在您的地图函数中,枚举 this 对象的 params 属性的属性。对于您发现的每个属性,使用包含属性名称和属性值的键调用 emit 。传递 1 作为值。例如 emit({'brand','BMW'}, 1) 但显然使用变量而不是常量!

在您的reduce函数中,您将传递一个键和一个值数组。对这些值求和并返回总和。即使初始数组全为 1,也不要试图使用数组的长度,因为可以迭代调用 reduce 函数。

您可以随后对结果集合中的结果进行分组,如果需要提高性能,则应用索引。

In your map function enumerate the properties of the params property of the this object. For each property you find call emit with a key that contains both the name of the property and the value of the property. Pass 1 as the value. e.g. emit({'brand','BMW'}, 1) but obviously using variables not constants!

In your reduce function you are passed a key and an array of values. Sum these values and return the sum. Even though the initial array will be all 1's don't be tempted to use the length of the array because the reduce function can be called iteratively.

You can group the results afterwards from the result collection, applying an index if necessary for performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文