-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart3.js
89 lines (63 loc) · 2.19 KB
/
Part3.js
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
//-------------------- MAP-REDUCE JOB 1 --------------------//
var mapFunction1 = function() {
// A list of stopwords to ingore in word count.
var stop_words = ['of', 'and', 'a', 'an', 'the', 'in', 'on', 'for']
// Loop for each course of the current student.
for (var i = 0; i < this.courses.length; i++) {
// Get the current course's name.
var key = this.courses[i].course_title;
// Get each word of the course's title.
var tokens = key.split(' ');
// Loop for every word.
for(var j = 0; j < tokens.length; j++) {
// If current word is not a stopword, emit it.
if ( tokens.indexOf(tokens[j].toLowerCase()) < 0 ) {
emit(tokens[j].toLowerCase(), 1);
}
}
}
};
var reduceFunction1 = function(key, values) {
return Array.sum(values);
};
db.students.mapReduce(
mapFunction1,
reduceFunction1,
{ out: "map_reduce_1" }
);
//----------------------------------------------------------//
//-------------------- MAP-REDUCE JOB 2 --------------------//
var mapFunction2 = function() {
// Loop for each course of the current student.
for (var i = 0; i < this.courses.length; i++) {
// If the status of the current course is "Complete", then emit a new pair.
if (this.courses[i].course_status == "Complete") {
// Get the type of the current course.
var courseType = this.courses[i].course_code.substr(0, 1).toUpperCase();
// Emit a new key-value pair.
emit( { 'city': this.home_city, 'course_type': courseType },
{ 'count': 1, 'grade': this.courses[i].grade } );
}
}
};
var reduceFunction2 = function(key, values) {
// Create an object which will keep the count and sum of grades for the current key.
reducedValue = { 'count': 0, 'grade': 0 };
for (var i = 0; i < values.length; i++) {
reducedValue.count += values[i].count;
reducedValue.grade += values[i].grade;
}
return reducedValue;
};
var finalizeFunction2 = function(key, reducedValue) {
return reducedValue.grade / reducedValue.count;
};
db.students.mapReduce(
mapFunction2,
reduceFunction2,
{
out: "map_reduce_2",
finalize: finalizeFunction2
}
);
//----------------------------------------------------------//