-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.go
61 lines (48 loc) · 971 Bytes
/
wordcount.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"github.com/Zemanta/mrgob/job"
)
func main() {
job.InitByteJob(runMapper, runReducer)
}
func runMapper(w *job.ByteKVWriter, r io.Reader) {
job.Log.Print("Mapper run")
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
for _, word := range strings.Fields(line) {
w.Write([]byte(word), []byte("1"))
job.Count("mapper_word", 1)
}
}
if err := scanner.Err(); err != nil {
job.Log.Fatal(err)
}
}
func runReducer(w io.Writer, r *job.ByteKVReader) {
job.Log.Print("Reducer run")
for r.Scan() {
key, vr := r.Key()
count := 0
for vr.Scan() {
c, err := strconv.Atoi(string(vr.Value()))
if err != nil {
job.Log.Fatal(err)
}
count += c
}
if err := vr.Err(); err != nil {
job.Log.Fatal(err)
}
fmt.Fprintf(w, "%s\t%d\n", key, count)
job.Count("reducer_word", 1)
}
if err := r.Err(); err != nil {
job.Log.Fatal(err)
}
}