最近在学习Mongodb,作为入门,选择了《Mongodb权威指南》这本书来进行参考学习。目前看到了MapReduce,由于我用的版本比较新,发现了和书上有些不同的地方。
应用场景:
使用MapReduce找出一个集合中所有的键的个数,mongo shell代码如下:
定义map function:
map = function () {
for (var key in this) {
emit(key, {count:1});
}
}
定义reduce function:
reduce = function (key, emits) {
total = 0;
for (var i in emits) {
total += emits[i].count;
}
return {count:total};
}
执行MapReduce操作:
mr = db.runCommand({"mapreduce":"users","map":map,"reduce":reduce})
以上都是书上的代码,但是执行后没有正常返回,而是返回了如下结果:
{
"assertion" : "'out' has to be a string or an object",
"assertionCode" : 13606,
"errmsg" : "db assertion failure",
"ok" : 0
}
网上百度了下才知道原来1.8以上的版本需要指定out的输出
修改了执行MapReduce命令后执行正常,命令如下:
mr = db.runCommand({"mapreduce":"users","map":map,"reduce":reduce,"out":{inline:1}})
也可以使用如下命令方式:
db.users.mapReduce(map,reduce,{out:{inline:1}})
执行后返回结果如下:
{
"results" : [
{
"_id" : "_id",
"value" : {
"count" : 3
}
},
{
"_id" : "age",
"value" : {
"count" : 1
}
},
{
"_id" : "email",
"value" : {
"count" : 1
}
},
{
"_id" : "emails",
"value" : {
"count" : 1
}
},
{
"_id" : "favorite book",
"value" : {
"count" : 1
}
},
{
"_id" : "hobby",
"value" : {
"count" : 1
}
},
{
"_id" : "name",
"value" : {
"count" : 3
}
},
{
"_id" : "score",
"value" : {
"count" : 2
}
}
],
"timeMillis" : 311,
"counts" : {
"input" : 3,
"emit" : 13,
"reduce" : 3,
"output" : 8
},
"ok" : 1
}
官网文档地址:http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Outputoptions
分享到:
相关推荐
mongodb-spark官方连接器,运行spark-submit --packages org.mongodb.spark:mongo-spark-connector_2.11:1.1.0可以自动下载,国内网络不容易下载成功,解压后保存到~/.ivy2目录下即可。
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
MongoDB程序员认证: MongoDB Certified Developer Study Guide - Study Guide - Exam Preparation - Practise Questions
04mongodb基本概念:文档.mp4
The Definitive Guide to MongoDB: A complete guide to dealing with Big Data using MongoDB The Definitive Guide to MongoDB, Third Edition, is updated for MongoDB 3 and includes all of the latest ...
mongoDB数据库资源:xc_cms.zip
根据 homebrew-brew 官方的解释得知,...Error: No available formula with the name ‘mongodb' 新的安装方式可以参考 github 主页,https://github.com/mongodb/homebrew-brew。 新的安装方式告诉我们,需要先执行:
第三方mongo spark连接器,运行spark-submit --packages com.stratio.datasource:spark-mongodb_2.10:0.11.2可以自动下载,国内网容易下载失败,把这个文件解压后拷贝到~/.ivy2目录下即可。 ...
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系...
With so many companies opting for MongoDB as their NoSQL database of choice, there's a need for a practical how-to combined with expert advice for getting the most out of the software. Beginning ...
MongoDB排序错误:Sort operation used more than the maximum 33554432 bytes of RAM错误描述我
mongodb 命令行mongod启动报错 about to fork child process, waiting until server is ready for connections. forked process: 3560 ERROR: child process failed, exited with error number 1 To see additional ...
Getting Started with Installation and coding with Mongo Shell 5: MongoDB Explained 6: Administering MongoDB 7: MongoDB Use Cases and How To’s – Know How To’s for using MongoDB to its best. ...
然后我使用以下命令将文件中的所有单词导入到 MongoDB 数据库中: time mongoimport --type csv -c Words --file word_list.txt --headerline ##### 操作时间:0.758 秒。 ### 搜索字谜 为了查找字谜,我编写,...
技术关键词: MongoDB 内容关键词: NoSQL文档数据库 用途: 用于存储和检索大量的分布式数据 内容描述: 一个基于分布式文件存储的开源数据库系统。MongoDB是一个NoSQL数据库,使用文档来存储数据,非常适合处理...
MongoDB数据库
mongodb mapreduce 实例,该例子主要用来做订单统计的。具体问题请到博客提问。
文档层使用MongoDB:registered:有线协议,允许通过现有的MongoDB:registered:客户端绑定使用MongoDB:registered:API。 所有持久数据都存储在FoundationDB键值存储中。 文档层实现了MongoDB:registered:API(v ...
MongoDB MapReduce MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE)。这样做的好处是可以在任务被分解后,可以通过大量机器进行并行计算,减少...