forked from bcongdon/corral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amplab2.go
50 lines (40 loc) 路 913 Bytes
/
amplab2.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/rothaarlappen/corral"
)
const subStrX = 8
type amplab2 struct{}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (a amplab2) Map(key, value string, emitter corral.Emitter) {
fields := strings.Split(value, ",")
if len(fields) != 9 {
fmt.Printf("Invalid record: '%s'\n", value)
return
}
sourceIP := fields[0]
adRevenue := fields[3]
emitter.Emit(sourceIP[:min(subStrX, len(sourceIP))], adRevenue)
}
func (a amplab2) Reduce(key string, values corral.ValueIterator, emitter corral.Emitter) {
totalRevenue := 0.0
for value := range values.Iter() {
adRevenue, err := strconv.ParseFloat(value, 64)
if err == nil {
totalRevenue += adRevenue
}
}
emitter.Emit(key, fmt.Sprintf("%f", totalRevenue))
}
func main() {
job := corral.NewJob(amplab2{}, amplab2{})
driver := corral.NewDriver(job)
driver.Main()
}