MapReduce 用于计算参数值
我有这样的文档:
{
"_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你需要为此进行映射缩减。对于一些简单的 Map-Reduce 示例,请查看此处。
对于您的特定情况,您首先需要更改对输出的期望。 Map/Reduce 的输出是一个集合。该集合将看起来像这样(在您的情况下):
要生成此集合,您将需要一个“map”函数和一个“reduce”函数。 “map”函数将发出一个键和一个值。键是 params 的每个元素,值是 1 的计数。“reduce”函数接受一个键和一个值数组,并仅返回一个值。您的问题与 MongoDB 网站 上的示例基本相同:
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:
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:
在您的地图函数中,枚举
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 callemit
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.