快速例子学习mongodb的mapreduce
来源:互联网 发布:大风刮过知乎 编辑:程序博客网 时间:2024/06/01 10:33
感谢有奉献精神的人
转自:http://jackyrong.iteye.com/blog/1408548
map和reduce是十分有用的操作,特别是在NOSQL中.本文简单小结下
在mongodb中对mapreduce的操作,以及在JAVA中如何操作.
1 启动mongodb
mongo启动即可
2 建立db
use test
3 加点记录
> book1 = {name : "Understanding JAVA", pages : 100}
> book2 = {name : "Understanding JSON", pages : 200}
> db.books.save(book1)
> db.books.save(book2)
继续加
> 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)
4 先来做MAP,这里是先归类,按页数去划分分类,如下:
- > var map = function() {
- var category;
- if ( this.pages >= 250 )
- category = 'Big Books';
- else
- category = "Small Books";
- emit(category, {name: this.name});
- };
5 然后再按reduce来统计个数
- > var reduce = function(key, values) {
- var sum = 0;
- values.forEach(function(doc) {
- sum += 1;
- });
- return {books: sum};
- };
6 然后再查看下,结果显示为:
> 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 } }
7 换用JAVA去实现之,注意下载mongodb的驱动,代码如下:
- 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 block
- e.printStackTrace();
- }
- }
- }
阅读全文
0 0
- 快速例子学习mongodb的mapreduce
- mongodb mapreduce 的例子
- MongoDB MapReduce学习笔记
- MongoDB学习笔记---MapReduce
- MongoDB MapReduce学习笔记
- MongoDB MapReduce学习笔记
- MongoDB MapReduce学习笔记
- MongoDB MapReduce学习笔记
- MongoDB MapReduce学习笔记
- mongodb的mapreduce
- MongoDB的MapReduce使用
- mongodb的mapreduce
- Mongodb的MapReduce优化
- mongoDB的MapReduce简介
- mongoDB的MapReduce简介
- mongodb的mapreduce使用
- mongodb 学习笔记五 MapReduce
- MongoDB 学习笔记五 MapReduce
- C语言实现括号匹配问题
- java.lang.Integer常见问题
- oracle触发器的启用和停用
- 【无用】随笔_摘抄
- 数据库操作练习2
- 快速例子学习mongodb的mapreduce
- dubbo的使用详解
- Python脚本 —Windows下文件备份
- Android TextView使用SpannableString设置复合文本的方法
- linux tty1-7理解
- 仿照百度网盘的分析
- Java学习记录–泛型擦除分析
- uva12298(Super Poker II)-FFT(快速傅里叶变换)
- POJ-3233 Matrix Power Series