日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用MongoDB实现MapReduce

發(fā)布時(shí)間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用MongoDB实现MapReduce 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

來源:http://blog.csdn.net/star_zongke/article/details/7475455

MapReduce 是 Google 在 2004 年發(fā)布的一個軟件框架,用于支持大規(guī)模數(shù)據(jù)的分布式計(jì)算,詳情請看這里。

MongoDB 是一個開源的面向文檔的 NoSQL 數(shù)據(jù)庫系統(tǒng),使用 C++ 編寫,詳情請看這里。

1. 安裝 MangoDB

首先請按照官方這個文檔安裝 MongoDB 數(shù)據(jù)庫,在本文中,我們是在 Mac OS X 下安裝并測試無誤。

我使用?sudo port install mongodb?命令來安裝 MongoDB ,唯一碰到的一個問題是 xcode 的版本問題,升級到 xcode 的最新版本就好了。

2. 運(yùn)行 MongoDB

啟動 MongoDB 是很簡單的,只需要在終端窗口中執(zhí)行 mogod 即可。

默認(rèn) MongoDB 是運(yùn)行在 27017 端口上,使用 /data/db 作為默認(rèn)目錄來存放數(shù)據(jù)(我們已經(jīng)在第一步就創(chuàng)建了這個目錄)

如果你修改這些默認(rèn)的配置,你可以通過命令行參數(shù)來進(jìn)行修改:

mongod --port [your_port] --dbpath [your_db_file_path]?

你需要確認(rèn)的是數(shù)據(jù)目錄必須已經(jīng)存在并且在 mongodb 首次啟動時(shí)該目錄下沒有其他文件。

3. 啟動 MongoDB 交互環(huán)境

我們可以啟動 MongoDB 交互環(huán)境來連接到 MongoDB 服務(wù)器,并在命令行中直接運(yùn)行 MongoDB 命令。

在同一臺機(jī)器上,你只需要簡單的執(zhí)行 mongo 就可以進(jìn)入交互環(huán)境,如果想要連接不同機(jī)器上的 MongoDB 服務(wù)器,你可以使用下面的參數(shù)來指定目標(biāo)服務(wù)器的IP地址和端口:

mongo [ip_address]:[port]

例如 : mongo localhost:4000

4. 創(chuàng)建數(shù)據(jù)庫

接下來在交互環(huán)境中執(zhí)行下面命令來創(chuàng)建數(shù)據(jù)庫:

use?library

上述命令創(chuàng)建了一個名為 library 的數(shù)據(jù)庫。

然后我們可以通過下面的命令來查看剛創(chuàng)建的數(shù)據(jù)庫,下面命令列出系統(tǒng)中所有的數(shù)據(jù)庫:

show?dbs;?

你會注意到,你剛創(chuàng)建的數(shù)據(jù)庫并沒有列出來,這是因?yàn)?MongoDB 只有在需要的時(shí)候才會創(chuàng)建數(shù)據(jù)庫,因此你需要往數(shù)據(jù)庫里添加點(diǎn)數(shù)據(jù)。

5. 往數(shù)據(jù)庫中插入數(shù)據(jù)

首先我們通過以下命令創(chuàng)建兩本書:

> book1 = {name : "Understanding JAVA", pages : 100}
> book2 = {name : "Understanding JSON", pages : 200}

然后將這兩本書保持到名為 books 的集合中:

> db.books.save(book1)
> db.books.save(book2)

上述命令將在 library 數(shù)據(jù)庫中創(chuàng)建一個名為 books 的集合(也就是SQL數(shù)據(jù)庫中的表),下面命令將列出我們剛添加的兩本書:

> 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. 編寫 Map 函數(shù)

接下來我們編寫一個搜索功能,用來查找超過250頁的圖書:

1>?var?map =?function() {
2var?category;
3if?(?this.pages >= 250 )
4category =?'Big Books';
5else
6category =?"Small Books";
7emit(category, {name:?this.name});
8};

所返回的結(jié)果:

{"Big Books",[{name: "Understanding XML"}, {name : "Understanding Web Services"}]);
{"Small Books",[{name: "Understanding JAVA"}, {name : "Understanding JSON"},{name: "Understanding Axis2"}]);

7. 編寫 Reduce 函數(shù)

1>?var?reduce =?function(key, values) {
2var?sum = 0;
3values.forEach(function(doc) {
4sum += 1;
5});
6return?{books: sum};
7};

8. 在 books 集合中運(yùn)行 MapReduce

1>?var?count? = db.books.mapReduce(map, reduce, {out:?"book_results"});
2> db[count.result].find()
3?
4{?"_id"?:?"Big Books",?"value"?: {?"books"?: 2 } }
5{?"_id"?:?"Small Books",?"value"?: {?"books"?: 3 } }

上述結(jié)果表明我們有兩本大書和三本小書。

利用 MongoDB 交互環(huán)境可以做任何事情,用 Java 也一樣,但是你需要下載一些必須的jar包。

下面是 Java 的源碼:

01import?com.mongodb.BasicDBObject;
02import?com.mongodb.DB;
03import?com.mongodb.DBCollection;
04import?com.mongodb.DBObject;
05import?com.mongodb.MapReduceCommand;
06import?com.mongodb.MapReduceOutput;
07import?com.mongodb.Mongo;
08?
09public?class?MongoClient {
10?
11?/**
12??* @param args
13??*/
14?public?static?void?main(String[] args) {
15?
16??Mongo mongo;
17???
18??try?{
19???mongo =?new?Mongo("localhost",?27017);
20???DB db = mongo.getDB("library");
21?
22???DBCollection books = db.getCollection("books");
23?
24???BasicDBObject book =?new?BasicDBObject();
25???book.put("name",?"Understanding JAVA");
26???book.put("pages",?100);
27???books.insert(book);
28????
29???book =?new?BasicDBObject();?
30???book.put("name",?"Understanding JSON");
31???book.put("pages",?200);
32???books.insert(book);
33????
34???book =?new?BasicDBObject();
35???book.put("name",?"Understanding XML");
36???book.put("pages",?300);
37???books.insert(book);
38????
39???book =?new?BasicDBObject();
40???book.put("name",?"Understanding Web Services");
41???book.put("pages",?400);
42???books.insert(book);
43??
44???book =?new?BasicDBObject();
45???book.put("name",?"Understanding Axis2");
46???book.put("pages",?150);
47???books.insert(book);
48????
49???String map =?"function() { "+
50?????????????"var category; "?+?
51?????????????"if ( this.pages >= 250 ) "+?
52?????????????"category = 'Big Books'; "?+
53?????????????"else "?+
54?????????????"category = 'Small Books'; "+?
55?????????????"emit(category, {name: this.name});}";
56????
57???String reduce =?"function(key, values) { "?+
58????????????????????????????"var sum = 0; "?+
59????????????????????????????"values.forEach(function(doc) { "?+
60????????????????????????????"sum += 1; "+
61????????????????????????????"}); "?+
62????????????????????????????"return {books: sum};} ";
63????
64???MapReduceCommand cmd =?new?MapReduceCommand(books, map, reduce,
65?????null, MapReduceCommand.OutputType.INLINE,?null);
66?
67???MapReduceOutput out = books.mapReduce(cmd);
68?
69???for?(DBObject o : out.results()) {
70????System.out.println(o.toString());
71???}
72??}?catch?(Exception e) {
73???// TODO Auto-generated catch block
74???e.printStackTrace();
75??}
76?}
77}

轉(zhuǎn)載于:https://www.cnblogs.com/hasayaki/archive/2013/03/01/2938129.html

總結(jié)

以上是生活随笔為你收集整理的用MongoDB实现MapReduce的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。