- 浏览: 420713 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (269)
- 原创 (7)
- Java (51)
- Java Concurrency (2)
- IDE (16)
- Linux (46)
- Database (23)
- NoSQL (35)
- Web服务器 (23)
- Log日志 (11)
- HTTP (11)
- HTML (2)
- XML (1)
- Test (7)
- Mina (0)
- Amoeba (4)
- Cobar (1)
- 序列化 (2)
- Python (5)
- PHP (1)
- Socket通信 (1)
- Network (3)
- Struts (2)
- Web前端 (10)
- Maven (6)
- SVN (15)
- Json (1)
- XMPP (2)
- Go (1)
- Other (4)
- 未整理 (5)
最新评论
-
u012374672:
[color=darkred][/color][flash=2 ...
Mongo的ORM框架的学习Morphia(annotations) -
b_l_east:
很有问题啊
利用redis的transaction功能,实现分布式下加锁
MapReduce函数的用法如下:
db.users.mapReduce(map, reduce [, {option}] )
后边的 option参数可选,但 out参数必须要有,否则会报没有指定输出的错误,out的值有以下几种:
{ replace : "collectionName" } - the output will be inserted into a collection which will atomically replace any existing collection with the same name.
- { merge : "collectionName" } - This option will merge new data into the old output collection. In other words, if the same key exists in both the result set and the old collection, the new key will overwrite the old one.
- { reduce : "collectionName" } - If documents exists for a given key in the result set and in the old collection, then a reduce operation (using the specified reduce function) will be performed on the two values and the result will be written to the output collection. If a finalize function was provided, this will be run after the reduce as well.
- { inline : 1} - With this option, no collection will be created, and the whole map-reduce operation will happen in RAM. Also, the results of the map-reduce will be returned within the result object. Note that this option is possible only when the result set fits within the 16MB limit of a single document. In v2.0 , this is your only available option on a replica set secondary.
另外,在使用 ./mongo登录到客户端上,map和reduce函数都不能被引号引起来,否则就是字符串,而不是函数了,这点就是纯粹的javascript
举个例子:
对于类似如下形式的collection(名为:example)
{_id:4,type:'cat',num:1}
{_id:11,type:'dog',num:3}
{_id:34,type:'pig',num:1}
{_id:40,type:'cat',num:2}
> map=function(){emit(this._id,1)}
function () {
emit(this._id, 1);
}
> reduce=function(key,values){return {count:1}}
function (key, values) {
return {count:2}; //在这修改要输出的值(1)
}
> res=db.example.mapReduce(map,reduce,{out:"temp"}); //此处的{out:"temp"}必须要加上,表示将结果暂时保存到“temp”集合中;也可以使用{out:{inline:1}},即将结果输出到内存中
{
"result" : "temp",
"timeMillis" : 492,
"counts" : {
"input" : 12453,
"emit" : 12453,
"output" : 12076
},
"ok" : 1,
}
> db.temp.find()
{ "_id" : 4, "value" : 1 }
{ "_id" : 11, "value" : 1 }
{ "_id" : 34, "value" : 1 }
{ "_id" : 40, "value" : 1 }
> res
{
"result" : "temp",
"timeMillis" : 492,
"counts" : {
"input" : 12453,
"emit" : 12453,
"output" : 12076
},
"ok" : 1,
}
> //也可以使用如下形式查询
> res.find()
{ "_id" : 4, "value" : 1 }
{ "_id" : 11, "value" : 1 }
{ "_id" : 34, "value" : 1 }
{ "_id" : 40, "value" : 1 }
注意:上边输出的结果,并没有变成reduce函数所返回的 {count:2} 值,而是返回了map中emit方法弹出的“1”。原因可能是因为对于某个key,如果他的值在符合条件的结果集合中如果只有一条数据,那么mapReduce函数将不会去调用reduce函数进行计算。另外,mapReduce函数返回的结果,其collection结构一般情况下会跟map函数中emit(key,value)方法中的value参数的结构保持一致的
而如果map和reduce函数换成如下形式,reduce函数将被调用:
>map=function(){
emit(this.type,{totalNum:this.num});
}
>reduce=function(key,values){
var totalNum=0;
values.forEach(function(val){
totalNum+=val
});
return totalNum;
}
>res=db.example.mapReduce(map,reduce,{out:"temp"});
>res.find();
{_id:"cat",value:{totalNum:3}}
{_id:"dog",value:{totalNum:3}}
{_id:"pig",value:{totalNum:1}}
如果想使用sort和limit等功能,可以使用 res.find().sort({"value.totalNum":-1}).limit(2)来达到目的
再补充一点使用java客户端连接mongodb,使用MapReduceOutput.getCollection().find()返回的结果中会以如下DBObject的形式出现:
{_id:"cat",value:{totalNum:3}}
但如果你使用 DBObject object=MapReduceOutput.getCollection().find() ;//其中的MapReduceOutput应该为Collection.mapReduce()函数返回的对象,此处简便的写成这样,不要误会
System.out.println(object.get("value.totalNum")); //此处将打印出null
因为object.get("value")将返回一个Object,在这个对象没有强制转换成DBObject之前,它是不能简单的使用“.”操作符来获取值的,正确的做法如下:
DBObject value=(DBObject)object.get("value");
System.out.println(value.get("totalNum"));
发表评论
-
mongodb 地理位置处理
2016-05-16 13:39 1387我只记录我用到的部分,没有完整分析mongodb对地理位置 ... -
Redis配置文件redis.conf
2014-11-14 14:10 1840# Redis configuration file ex ... -
Redis高可用部署及监控
2014-11-12 13:25 1076一、 Re ... -
JCS官方文档的简单笔记,仅供自己参考
2014-09-26 20:08 7491. 基本配置 jcs.default=DCjcs.de ... -
JCS基本配置
2014-09-26 19:39 9171、默认的内存缓存 ... -
NoSQL解决方案比较(MongoDB vs Redis, Tokyo Cabinet, and Berkeley DB)
2013-09-30 14:20 1312NoSQL解决方案比较 NoSQL Solution: E ... -
morphia与spring的整合
2012-12-07 15:06 1460转自: http://www.blogjava.net/wat ... -
Mongo的ORM框架的学习Morphia(十五)Morphia+spring整合
2012-12-07 15:06 1627转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十二) morphia的Query和Update
2012-12-07 15:06 1839转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十) morphia应用
2012-12-05 14:47 1434转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(九) morphia简单使用
2012-12-05 14:44 1345转自 http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(八) morphia数据库访问接口
2012-12-05 14:35 1991转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(annotations)
2012-12-05 14:33 2504一:@Entity的使用 @Entity ... -
Instagram的Redis实践(内存占用优化)
2012-11-30 10:43 1171转自:http://blog.nosqlfan.com/htm ... -
SQL 和Mongo 对比图表
2012-11-28 14:54 2180参看官方说明: http://www.mongodb ... -
MongoDB 入门指南、示例
2012-11-23 10:38 828转自:http://www.cnblogs.com/hoojo ... -
python的redis用法
2012-11-22 15:48 1141#! /usr/bin/env python #coding ... -
Python连接redis
2012-11-22 15:46 5581一、Redis是流行的NOSQL内存数据库,以Key-Valu ... -
【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
2012-08-29 10:42 1345转自 http://www.bwkeji.com/a/wang ... -
利用redis的transaction功能,实现分布式下加锁
2012-08-29 09:57 2346package memcached; import ja ...
相关推荐
1.分篇章进行学习,内容控制30分钟内 2.1个月疗程,不要放弃治疗哦 3.图文并茂,有问题请发到邮箱
MongoDB MapReduce MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE)。这样做的好处是可以在任务被分解后,可以通过大量机器进行并行计算,减少...
MapReduce应该算是MongoDB操作中比较复杂的了,下面这篇文章主要给大家介绍了关于MongoDB中MapReduce使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起看看吧。
#资源达人分享计划#
MongoDB的MapReduce.pdf 学习资料 复习资料 教学资源
Mongodb是针对大数据量环境下诞生的用于保存大数据量的非关系型数据库,针对大量的数据。接下来通过本文给大家介绍Mongodb中MapReduce实现数据聚合方法详解,感兴趣的朋友一起学习吧
作为一个优秀的编程模型,MapReduce在大数据处理中有很大的优势,而mongodb也支持这一编程模型,本文通过简单的单词计数示例论述在mongodb中如何使用MapReduce
标题:在 MongoDB 中使用 Map Reduce 检查波兰语和英语句子中的字母分布 动机 我们越来越多地听到各种网站遭到攻击以及密码非常薄弱的管理员的不负责任。 如何创建一个强密码:有一种观点认为你应该造一个句子,...
计算机后端-PHP视频教程. mongodb10 MapReduce 统计栏目下的商品.wmv
主要介绍了MongoDB学习笔记之MapReduce使用示例,本文直接给出实例代码,需要的朋友可以参考下
NULL 博文链接:https://superhuo.iteye.com/blog/1193485
mongodb mapreduce 实例,该例子主要用来做订单统计的。具体问题请到博客提问。
MapReduce 是 Google 在 2004 年发布的一个软件框架,用于支持大规模数据的分布式计算。 MongoDB 是一个开源的面向文档的 NoSQL 数据库系统,使用 C++ 编写。
计算机后端-PHP视频教程. mongodb08 导出导入.wmv
MongoDB五分钟教程:MongoDB Shell入门 基于MongoDB进行分布式数据存储的步骤 MongoDB分布式存储的MapReduce并行查询 实例:MongoDB与Tomcat的结合更便捷
Mongodb是主流的NOSQL数据库之一,Mongodb最佳实践,详细介绍了Mongodb使用以及底层原理,和运维管理; 1.Mongodb数据结构,以及存储方式 2.增删改查使用,分页,排序,投影,以及多种扩展使用 3.丰富查询语句,比如...