forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grades.go
105 lines (83 loc) · 2.81 KB
/
grades.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"github.com/apache/beam/sdks/go/pkg/beam"
"github.com/apache/beam/sdks/go/pkg/beam/log"
"github.com/apache/beam/sdks/go/pkg/beam/transforms/stats"
"github.com/apache/beam/sdks/go/pkg/beam/transforms/top"
"github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
"github.com/apache/beam/sdks/go/pkg/beam/x/debug"
)
type Grade struct {
Name string
GPA float32
}
func less(a, b Grade) bool {
return a.GPA < b.GPA
}
func alphabetically(a, b Grade) bool {
return a.Name < b.Name
}
func printTopFn(ctx context.Context, list []Grade) {
log.Infof(ctx, "TOP %v student(s):", len(list))
for i, student := range list {
log.Infof(ctx, " %v:\t%v\t(GPA: %v)", i+1, student.Name, student.GPA)
}
}
func main() {
flag.Parse()
beam.Init()
ctx := context.Background()
data := []Grade{
{"Adam", 2.3},
{"Alice", 3.8},
{"Alex", 2.5},
{"Bart", 3.2},
{"Bob", 3.9},
{"Brittney", 3.1},
{"Brenda", 3.5},
{"Chad", 1.1},
}
log.Info(ctx, "Running grades")
p := beam.NewPipeline()
s := p.Root()
students := beam.CreateList(s, data)
// (1) Print top 3 students overall by GPA
best := top.Largest(s, students, 3, less)
beam.ParDo0(s, printTopFn, best)
// (2) Print top student per initial (then ordered by name)
keyed := beam.ParDo(s, func(g Grade) (string, Grade) {
return g.Name[:1], g
}, students)
keyedBest := top.LargestPerKey(s, keyed, 1, less)
unkeyed := beam.Explode(s, beam.DropKey(s, keyedBest))
altBest := top.Smallest(s, unkeyed, 30, alphabetically)
beam.ParDo0(s, printTopFn, altBest)
// (3) Print Chad
chad := top.Smallest(s, students, 1, less)
beam.ParDo0(s, printTopFn, chad)
// (4) Print min/max/sum/mean grades
grades := beam.ParDo(s, func(g Grade) float32 { return g.GPA }, students)
debug.Printf(s, "Min: %v", stats.Min(s, grades))
debug.Printf(s, "Max: %v", stats.Max(s, grades))
debug.Printf(s, "Sum: %v", stats.Sum(s, grades))
debug.Printf(s, "Mean: %v", stats.Mean(s, grades))
if err := beamx.Run(ctx, p); err != nil {
log.Exitf(ctx, "Failed to execute job: %v", err)
}
}