-
Notifications
You must be signed in to change notification settings - Fork 13
/
merge.go
37 lines (33 loc) · 1021 Bytes
/
merge.go
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
package main
import (
"strconv"
"strings"
"github.com/BWbwchen/MapReduce/worker"
)
//
// The map function is called once for each file of input. The first
// argument is the name of the input file, and the second is the
// file's complete contents. You should ignore the input file name,
// and look only at the contents argument. The return value is a slice
// of key/value pairs.
//
func Map(filename string, contents string, ctx worker.MrContext) {
for _, line := range strings.Split(strings.TrimRight(contents, "\n"), "\n") {
words := strings.Split(line, " ")
ctx.EmitIntermediate(words[0], words[1])
}
}
//
// The reduce function is called once for each key generated by the
// map tasks, with a list of all the values created for that key by
// any map task.
//
func Reduce(key string, values []string, ctx worker.MrContext) {
// return the number of occurrences of this word.
total := 0
for _, s := range values {
num, _ := strconv.Atoi(s)
total += num
}
ctx.Emit(key, strconv.Itoa(total))
}