In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
It is easy to use Map/Reduce for parallel "statistics" on MongoDB.
Using MapReduce to implement two functions: Map function and Reduce function, Map function calls emit (key, value)
Iterate through all the records in collection, passing key and value to the Reduce function for processing.
The Map function and Reduce function can be implemented using JavaScript, and a MapReduce operation can be performed through the db.runCommand or mapReduce command:
Db.runCommand ({mapReduce:, map:, reduce:, out:, query:, sort:, limit:, finalize:, scope:, jsMode:, verbose:})
Parameter description:
Mapreduce: the collection of targets to operate on.
Map: mapping function (generates a sequence of key-value pairs as arguments to the reduce function).
Reduce: statistical function.
Out: statistical results store collections (if not specified, temporary collections are used, which are automatically deleted when the client is disconnected).
Query: target record filtering
Sort: sort the target records.
Limit: limit the number of target records.
Finalize: final handling function (final collation of the results returned by reduce into the result set)
Scope: import external variables into map, reduce, finalize.
JsMode: whether to convert Bson format between map and reduce execution
Verbose: displays detailed time statistics.
Let's give an example:
Prepare some data:
Next, we will show you how to count the number of students in each class.
Map:
The Map function must call emit (key, value) to return the key-value pair and use this to access the Document currently to be processed.
M = function () {emit (this.classid,1);}
Value can be passed using JSON Object (multiple attribute values are supported).
For example: emit (this.classid, {count:1})
Reduce:
The parameters received by the Reduce function are similar to the Group effect, combining the sequence of keys returned by Map into {key, [value1]
Value2, value3, value...]} is passed to reduce.
R = function (key,values) {var x = 0; values.forEach (function (v) {x + = v}); return x;}
The Reduce function performs "statistical" operations on these values, and the returned results can be returned using JSON Object.
Result:
MapReduce () stores the results in the "students_res" table.
Finalize:
With finalize (), we can further process the results of reduce ().
F = function (key,value) {return {classid:key,count:value}}
Let's recalculate it again and see the results:
Options:
We can also add more control details.
Db.runCommand ({mapreduce: "stu", map:m, reduce:r, out: "stu_res", finalize:f, query: {age: {$gt:10})
You can see that only the data with age > 10 is filtered first, and then counted, so there is no number of age=9.
According to.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.