In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use MapReduce in MongoDB. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The steps for MapReduce. Map- > Shuffle- > Reduce
Map: maps actions to each document in the collection. This operation either "does nothing" or produces "some keys and several values".
Shuffle: group the results of Map by keys and put the list of generated key values into the corresponding keys.
Reduce: simplifies the values in the list corresponding to each key to a single value. This value is returned, followed by Shuffle, until there is only one value in the list of each key, which is the final result.
The function of MapReduce looks like this:
Db.runCommand ({"mapReduce": "collection name", "map": "map function", "reduce": "reduce function"})
Different languages have corresponding interfaces: for example, in java, you can call the executeCommad () method directly, or after integrating SpringMongo, call the mapReduce () interface.
Call example (SpringMongoDB method):
SearchMongoTemplate.mapReduce (query, collection, map, reduce
Option.outputCollection (out), Map.class)
SearchMongoTemplate is a MongoOperations object
Query is an object of Query, and the mapReduce operation only deals with the query results.
Map is similar to "classpath:mapReduce/mapDayDetail.js" and is the storage address of map's js file, or you can read map's js file directly into String.
The meaning of reduce is similar to map, corresponding to the js file of reduce.
Out: is the collection name of the output file
Map.class: the output object type, which can be a custom object
Examples of Map.js and Reduce.js:
Map.js
Function () {
Var renewTime = this.renewTime
Var date = new Date (renewTime)
Var time = date.getFullYear () * 10000 + (date.getMonth () + 1) * 100+date.getDate ()
Emit ({
"year": date.getFullYear ()
"month": date.getMonth () + 1
"day": date.getDate ()
"level": this.productLevel
"time": time
}, {
'count': 1
});
Emit ({
"year": date.getFullYear ()
"month": date.getMonth () + 1
"day": date.getDate ()
"node": this.nodeName
"time": time
}, {
'count': 1
});
Emit ({
"year": date.getFullYear ()
"month": date.getMonth () + 1
"day": date.getDate ()
"satellite": this.satelliteName
"time": time
}, {
'count': 1
});
}
The script of Map.js acts on every document entered, and this refers to the current document, using this. Get the properties in the document in the way of the property name, and return the result of the key-value pair in the way of emit. In this case, three results are returned for each document. In the emit function, the first argument is key, and the second argument is value.
Reduce.js
Function (key, emits) {
Var result = {
"count": 0
}
For (var i in emits) {
Result.count + = emits [I] .count
}
Return result
}
The parameters passed to reduce can be the result of map or the result of reduce itself. So reduce must be called repeatedly, and the result returned by reduce must be an element in the second parameter of reduce. In Reduce, you only need to return value, and the returned key defaults to the incoming key.
On how to use MapReduce in MongoDB to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.