-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_map_reduce.go
143 lines (118 loc) · 2.61 KB
/
common_map_reduce.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package mr
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"sort"
)
func doMap(jobName string, mapTaskNumber int, inFile string, nReduce int, mapF func(file string, contents string) []KeyValue) {
f, err := os.Open(inFile)
defer f.Close()
if err != nil {
fmt.Print(err)
return
}
fInfo, err := f.Stat()
if err != nil {
fmt.Print(err)
return
}
fcontent := make([]byte, fInfo.Size())
f.Read(fcontent)
contents := string(fcontent)
keyValue := mapF(inFile, contents)
fJson := make([]*json.Encoder, nReduce)
for i := range fJson {
filename := reduceName(jobName, mapTaskNumber, i)
file, err := os.Create(filename)
if err != nil {
fmt.Print(err)
return
}
defer f.Close()
fJson[i] = json.NewEncoder(file)
}
for _, kv := range keyValue {
index := ihash(kv.Key) % nReduce
fJson[index].Encode(&kv)
}
}
func doReduce(jobName string, reduceTaskNumber int, nMap int, reduceF func(key string, values []string) string) {
keyValue := make(map[string][]string)
for i := 0 ; i < nMap ; i++ {
filename := reduceName(jobName, i, reduceTaskNumber)
file, err := os.Open(filename)
if err != nil {
fmt.Print(err)
continue
}
jsonDecoder := json.NewDecoder(file)
for {
var kv KeyValue
err_ := jsonDecoder.Decode(&kv)
if err_ != nil {
break
}
if _, ok := keyValue[kv.Key]; ok {
keyValue[kv.Key] = append(keyValue[kv.Key], kv.Value)
} else {
newvalue := []string{kv.Value}
keyValue[kv.Key] = newvalue
}
}
file.Close()
}
file, err := os.Create(mergeName(jobName, reduceTaskNumber))
defer file.Close()
if err != nil {
fmt.Print(err)
return
}
keys := make([]string,0,len(keyValue))
for k := range keyValue {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(file, "%v %v\n", k, reduceF(k, keyValue[k]))
}
}
// merge combines the results of the many reduce jobs into a single output file
func doMerge(jobName string, nReduce int) {
kvs := make(map[string]string)
for i := 0; i < nReduce; i++ {
p := mergeName(jobName, i)
fmt.Printf("Merge: read %s\n", p)
file, err := os.Open(p)
if err != nil {
log.Fatal("Merge: ", err)
}
dec := json.NewDecoder(file)
for {
var kv KeyValue
err = dec.Decode(&kv)
if err != nil {
break
}
kvs[kv.Key] = kv.Value
}
file.Close()
}
var keys []string
for k := range kvs {
keys = append(keys, k)
}
sort.Strings(keys)
file, err := os.Create("mrtmp." + jobName)
if err != nil {
log.Fatal("Merge: create ", err)
}
w := bufio.NewWriter(file)
for _, k := range keys {
fmt.Fprintf(w, "%v %v\n", k, kvs[k])
}
w.Flush()
file.Close()
}