-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapreduce.sql
126 lines (122 loc) · 3.4 KB
/
mapreduce.sql
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
db.students.insertMany([
{ name: 'Ryan', age: 21,gender: 'M', marks : 78 },
{ name: 'Joanna', age: 20,gender: 'F', marks : 67 },
{ name: 'Harsh', age: 22,gender: 'M', marks : 80 },
{ name: 'Chinmay', age: 20,gender: 'M', marks : 97 },
{ name: 'John', age: 21,gender: 'M', marks : 88 },
{ name: 'Riya', age: 20,gender: 'F', marks : 77 },
{ name: 'Soham', age: 22,gender: 'M', marks : 83},
{name : 'Atharwa',age: 20, gender :'M',marks : 74},
{name : 'Priya',age : 21,gender : 'F',marks : 98}
]);
db.students.find().pretty();
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("635a06f369917430cc58e282"),
ObjectId("635a06f369917430cc58e283"),
ObjectId("635a06f369917430cc58e284"),
ObjectId("635a06f369917430cc58e285"),
ObjectId("635a06f369917430cc58e286"),
ObjectId("635a06f369917430cc58e287"),
ObjectId("635a06f369917430cc58e288"),
ObjectId("635a06f369917430cc58e289"),
ObjectId("635a06f369917430cc58e28a")
]
}
{
"_id" : ObjectId("635a06f369917430cc58e282"),
"name" : "Ryan",
"age" : 21,
"gender" : "M",
"marks" : 78
}
{
"_id" : ObjectId("635a06f369917430cc58e283"),
"name" : "Joanna",
"age" : 20,
"gender" : "F",
"marks" : 67
}
{
"_id" : ObjectId("635a06f369917430cc58e284"),
"name" : "Harsh",
"age" : 22,
"gender" : "M",
"marks" : 80
}
{
"_id" : ObjectId("635a06f369917430cc58e285"),
"name" : "Chinmay",
"age" : 20,
"gender" : "M",
"marks" : 97
}
{
"_id" : ObjectId("635a06f369917430cc58e286"),
"name" : "John",
"age" : 21,
"gender" : "M",
"marks" : 88
}
{
"_id" : ObjectId("635a06f369917430cc58e287"),
"name" : "Riya",
"age" : 20,
"gender" : "F",
"marks" : 77
}
{
"_id" : ObjectId("635a06f369917430cc58e288"),
"name" : "Soham",
"age" : 22,
"gender" : "M",
"marks" : 83
}
{
"_id" : ObjectId("635a06f369917430cc58e289"),
"name" : "Atharwa",
"age" : 20,
"gender" : "M",
"marks" : 74
}
{
"_id" : ObjectId("635a06f369917430cc58e28a"),
"name" : "Priya",
"age" : 21,
"gender" : "F",
"marks" : 98
}
[Execution complete with exit code 0]
var mapfunction = function(){emit(this.age,this.marks)}
var reducefunction = function(key,values){return Array.sum(values)}
db.students.mapReduce(mapfunction,reducefunction,{'out':'Result1_mapreduce'})
db.Result1_mapreduce.find()
{ "result" : "Result1_mapreduce", "ok" : 1 }
{ "_id" : 21, "value" : 264 }
{ "_id" : 22, "value" : 163 }
{ "_id" : 20, "value" : 315 }
[Execution complete with exit code 0]
var mapfunction = function(){emit(this.age,this.marks)}
var reducefunction = function(key,values){return Array.avg(values)}
db.students.mapReduce(mapfunction,reducefunction,{query:{age:{$gt:21}},out:'Result2_mapreduce'})
db.Result2_mapreduce.find()
{ "result" : "Result2_mapreduce", "ok" : 1 }
{ "_id" : 22, "value" : 81.5 }
[Execution complete with exit code 0]
var mapfunction = function(){emit(this.gender,this.marks)}
var reducefunction = function(key,values){return Array.sum(values)}
db.students.mapReduce(mapfunction,reducefunction,{'out':'Result3_mapreduce'})
db.Result3_mapreduce.find()
{ "result" : "Result3_mapreduce", "ok" : 1 }
{ "_id" : "M", "value" : 500 }
{ "_id" : "F", "value" : 242 }
[Execution complete with exit code 0]
var mapfunction = function(){emit(this.gender,this.marks)}
var reducefunction = function(key,values){return Array.avg(values)}
db.students.mapReduce(mapfunction,reducefunction,{'out':'Result4_mapreduce'})
db.Result4_mapreduce.find()
{ "result" : "Result4_mapreduce", "ok" : 1 }
{ "_id" : "M", "value" : 83.33333333333333 }
{ "_id" : "F", "value" : 80.66666666666667 }
[Execution complete with exit code 0]