summaryrefslogtreecommitdiff
path: root/jstests/sharding/mapReduce_inSharded_outSharded.js
blob: 92dae92f5f0e06a337eaea0bd13d8dc51b7452f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function() {
    "use strict";

    var verifyOutput = function(out) {
        printjson(out);
        assert.eq(out.counts.input, 51200, "input count is wrong");
        assert.eq(out.counts.emit, 51200, "emit count is wrong");
        assert.gt(out.counts.reduce, 99, "reduce count is wrong");
        assert.eq(out.counts.output, 512, "output count is wrong");
    };

    var st = new ShardingTest(
        {shards: 2, verbose: 1, mongos: 1, other: {chunkSize: 1, enableBalancer: true}});

    var admin = st.s0.getDB('admin');

    assert.commandWorked(admin.runCommand({enablesharding: "mrShard"}));
    st.ensurePrimaryShard('mrShard', st.shard1.shardName);
    assert.commandWorked(
        admin.runCommand({shardcollection: "mrShard.srcSharded", key: {"_id": 1}}));

    var db = st.s0.getDB("mrShard");

    var bulk = db.srcSharded.initializeUnorderedBulkOp();
    for (var j = 0; j < 100; j++) {
        for (var i = 0; i < 512; i++) {
            bulk.insert({j: j, i: i});
        }
    }
    assert.writeOK(bulk.execute());

    function map() {
        emit(this.i, 1);
    }
    function reduce(key, values) {
        return Array.sum(values);
    }

    // sharded src sharded dst
    var suffix = "InShardedOutSharded";

    var out =
        db.srcSharded.mapReduce(map, reduce, {out: {replace: "mrReplace" + suffix, sharded: true}});
    verifyOutput(out);

    out = db.srcSharded.mapReduce(map, reduce, {out: {merge: "mrMerge" + suffix, sharded: true}});
    verifyOutput(out);

    out = db.srcSharded.mapReduce(map, reduce, {out: {reduce: "mrReduce" + suffix, sharded: true}});
    verifyOutput(out);

    out = db.srcSharded.mapReduce(map, reduce, {out: {inline: 1}});
    verifyOutput(out);
    assert(out.results != 'undefined', "no results for inline");

    out = db.srcSharded.mapReduce(
        map, reduce, {out: {replace: "mrReplace" + suffix, db: "mrShardOtherDB", sharded: true}});
    verifyOutput(out);

    out = db.runCommand({
        mapReduce: "srcSharded",  // use new name mapReduce rather than mapreduce
        map: map,
        reduce: reduce,
        out: "mrBasic" + "srcSharded",
    });
    verifyOutput(out);

    st.stop();

})();