map型字段 mongodb_记一次MongoDB Map&Reduce入门操作

需求说明

用Map&Reduce计算几个班级中,每个班级10岁和20岁之间学生的数量:

需求分析

学生表的字段:

db.students.insert({classid:1, age:14, name:'Tom'})

将classid随机1和2、age在8-25岁之间随机,name在3-7个字符之间随机。

数据写入

数据写入java脚本

往mrtask库中students写入1000万条数据:

package org.test;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.DBObject;

import com.mongodb.MongoClient;

import com.mongodb.ServerAddress;

public class TestMongoDBReplSet {

public static void main(String[] args) {

try {

List addresses = new ArrayList();

ServerAddress address1 = new ServerAddress("172.16.16.89", 27017);

addresses.add(address1);

MongoClient client = new MongoClient(addresses);

DB db = client.getDB("mrtask");

DBCollection coll = db.getCollection("students");

// 数据写入

BasicDBObject object = new BasicDBObject();

for (int i = 1; i <= 10000000; i++) {

object.append("classid", 1 + (int) (Math.random() * 2));

object.append("age", 8 + (int) (Math.random() * 17));

object.append("name", getName());

coll.insert(object);

object.clear();

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static String getName() {

ArrayList list = new ArrayList();

for (char c = 'a'; c <= 'z'; c++) {

list.add(c);

}

String str = "";

int end = 3 + (int) (Math.random() * 4);

for (int i = 0; i < end; i++) {

int num = (int) (Math.random() * 26);

str = str + list.get(num);

}

return str;

}

}

查看数据写入

经查看,mrtask库中students表中有1000万条的数据:

[root@localhost bin]# ./mongo

MongoDB shell version: 2.6.11

connecting to: test

> show dbs

admin   (empty)

local   0.078GB

mrtask  3.952GB

test    0.453GB

> use mrtask

switched to db mrtask

> db.students.find().count()

10000000

Map&Reduce计算

Map计算

> mapfun = function(){emit(this.classid,1)}

Reduce计算

> reducefun=function (key, values) { var count = 0; values.forEach(function (v) {count += v;}); return count; }

> ff = function (key, value) { return {classid:key, count:value}; }

Result输出

> classid_res = db.runCommand({

mapreduce:"students",

map:mapfun,

reduce:reducefun,

out:"students_classid_res",

finalize:ff,

query:{age:{$gt:10,$lt:20}}

});

计算结果

> db.students_classid_res.find()

{ "_id" : 1, "value" : { "classid" : 1, "count" : 2643128 } }

{ "_id" : 2, "value" : { "classid" : 2, "count" : 2650870 } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值