MongoDB是用C ++编写的面向开源文档的NoSQL数据库系统。 您可以从此处阅读有关MongoDB的更多信息。
1.安装MangoDB。
2.运行MongoDB
3.启动MongoDB shell
mongo [ip_address]:[端口]
例如:mongo localhost:4000
4.首先创建一个数据库。
在MangoDB Shell中输入以下内容…
> use library
上面应该创建一个名为“ library”的数据库。
现在查看是否已创建数据库,只需键入以下内容-应该列出所有数据库。
> show dbs;
5.将数据插入MongoDB。
首先,使用以下命令创建两本书。
> book1 = {name : "Understanding JAVA", pages : 100}
> book2 = {name : "Understanding JSON", pages : 200}
现在,让我们将这两本书插入到名为books的集合中。
> db.books.save(book1)
> db.books.save(book2)
上面的两个语句将在数据库库下创建一个称为books的集合。 以下语句将列出我们刚刚保存的两本书。
> db.books.find();{ "_id" : ObjectId("4f365b1ed6d9d6de7c7ae4b1"), "name" : "Understanding JAVA", "pages" : 100 }
{ "_id" : ObjectId("4f365b28d6d9d6de7c7ae4b2"), "name" : "Understanding JSON", "pages" : 200 }
让我们再添加一些记录。
> book = {name : "Understanding XML", pages : 300}
> db.books.save(book)
> book = {name : "Understanding Web Services", pages : 400}
> db.books.save(book)
> book = {name : "Understanding Axis2", pages : 150}
> db.books.save(book)
6.编写地图功能
让我们以某种方式处理该图书馆藏书,我们需要找到页数少于250页且大于250页的书籍数量。
> var map = function() {
var category;
if ( this.pages >= 250 )
category = 'Big Books';
else
category = "Small Books";
emit(category, {name: this.name});
};
在此,由Map函数生成的集合将具有以下成员的集合。
{"Big Books",[{name: "Understanding XML"}, {name : "Understanding Web Services"}]);
{"Small Books",[{name: "Understanding JAVA"}, {name : "Understanding JSON"},{name: "Understanding Axis2"}]);
7.编写减少功能。
> var reduce = function(key, values) {
var sum = 0;
values.forEach(function(doc) {
sum += 1;
});
return {books: sum};
};
8.针对books集合运行MapReduce。
> var count = db.books.mapReduce(map, reduce, {out: "book_results"});
> db[count.result].find(){ "_id" : "Big Books", "value" : { "books" : 2 } }
{ "_id" : "Small Books", "value" : { "books" : 3 } }
上面说,我们有2本大书和3本小书。
上面使用MongoDB Shell完成的所有操作,也可以使用Java完成。 以下是它的Java客户端。 您可以从此处下载所需的从属jar。
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;public class MongoClient {/*** @param args*/public static void main(String[] args) {Mongo mongo;try {mongo = new Mongo("localhost", 27017);DB db = mongo.getDB("library");DBCollection books = db.getCollection("books");BasicDBObject book = new BasicDBObject();book.put("name", "Understanding JAVA");book.put("pages", 100);books.insert(book);book = new BasicDBObject(); book.put("name", "Understanding JSON");book.put("pages", 200);books.insert(book);book = new BasicDBObject();book.put("name", "Understanding XML");book.put("pages", 300);books.insert(book);book = new BasicDBObject();book.put("name", "Understanding Web Services");book.put("pages", 400);books.insert(book);book = new BasicDBObject();book.put("name", "Understanding Axis2");book.put("pages", 150);books.insert(book);String map = "function() { "+ "var category; " + "if ( this.pages >= 250 ) "+ "category = 'Big Books'; " +"else " +"category = 'Small Books'; "+ "emit(category, {name: this.name});}";String reduce = "function(key, values) { " +"var sum = 0; " +"values.forEach(function(doc) { " +"sum += 1; "+"}); " +"return {books: sum};} ";MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,null, MapReduceCommand.OutputType.INLINE, null);MapReduceOutput out = books.mapReduce(cmd);for (DBObject o : out.results()) {System.out.println(o.toString());}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
参考: Facile Login博客上的JCG合作伙伴 Prabath Siriwardena的MongoDB与MapReduce 。
翻译自: https://www.javacodegeeks.com/2012/06/mapreduce-with-mongodb.html