github.com/apache/beam/sdks/v2@v2.48.2/go/examples/grades/grades.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one or more 2 // contributor license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright ownership. 4 // The ASF licenses this file to You under the Apache License, Version 2.0 5 // (the "License"); you may not use this file except in compliance with 6 // the License. You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package main 17 18 // beam-playground: 19 // name: Grades 20 // description: An example that combines grades data. 21 // multifile: false 22 // context_line: 41 23 // categories: 24 // - Debugging 25 // - Combiners 26 // - Filtering 27 // complexity: MEDIUM 28 // tags: 29 // - combine 30 // - map 31 // - strings 32 // - numbers 33 34 import ( 35 "context" 36 "flag" 37 38 "github.com/apache/beam/sdks/v2/go/pkg/beam" 39 "github.com/apache/beam/sdks/v2/go/pkg/beam/log" 40 "github.com/apache/beam/sdks/v2/go/pkg/beam/register" 41 "github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/stats" 42 "github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/top" 43 "github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx" 44 "github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug" 45 ) 46 47 func init() { 48 register.Function2x0(printTopFn) 49 register.Function2x1(less) 50 register.Function2x1(alphabetically) 51 register.Function1x2(keyGrade) 52 register.Function1x1(getGPA) 53 } 54 55 type Grade struct { 56 Name string 57 GPA float32 58 } 59 60 func less(a, b Grade) bool { 61 return a.GPA < b.GPA 62 } 63 64 func alphabetically(a, b Grade) bool { 65 return a.Name < b.Name 66 } 67 68 func printTopFn(ctx context.Context, list []Grade) { 69 log.Infof(ctx, "TOP %v student(s):", len(list)) 70 for i, student := range list { 71 log.Infof(ctx, " %v:\t%v\t(GPA: %v)", i+1, student.Name, student.GPA) 72 } 73 } 74 75 func keyGrade(g Grade) (string, Grade) { 76 return g.Name[:1], g 77 } 78 79 func getGPA(g Grade) float32 { return g.GPA } 80 81 func main() { 82 flag.Parse() 83 beam.Init() 84 85 ctx := context.Background() 86 87 data := []Grade{ 88 {"Adam", 2.3}, 89 {"Alice", 3.8}, 90 {"Alex", 2.5}, 91 {"Bart", 3.2}, 92 {"Bob", 3.9}, 93 {"Brittney", 3.1}, 94 {"Brenda", 3.5}, 95 {"Chad", 1.1}, 96 } 97 98 log.Info(ctx, "Running grades") 99 100 p := beam.NewPipeline() 101 s := p.Root() 102 students := beam.CreateList(s, data) 103 104 // (1) Print top 3 students overall by GPA 105 106 best := top.Largest(s, students, 3, less) 107 beam.ParDo0(s, printTopFn, best) 108 109 // (2) Print top student per initial (then ordered by name) 110 111 keyed := beam.ParDo(s, keyGrade, students) 112 keyedBest := top.LargestPerKey(s, keyed, 1, less) 113 unkeyed := beam.Explode(s, beam.DropKey(s, keyedBest)) 114 115 altBest := top.Smallest(s, unkeyed, 30, alphabetically) 116 beam.ParDo0(s, printTopFn, altBest) 117 118 // (3) Print Chad 119 120 chad := top.Smallest(s, students, 1, less) 121 beam.ParDo0(s, printTopFn, chad) 122 123 // (4) Print min/max/sum/mean grades 124 125 grades := beam.ParDo(s, getGPA, students) 126 debug.Printf(s, "Min: %v", stats.Min(s, grades)) 127 debug.Printf(s, "Max: %v", stats.Max(s, grades)) 128 debug.Printf(s, "Sum: %v", stats.Sum(s, grades)) 129 debug.Printf(s, "Mean: %v", stats.Mean(s, grades)) 130 131 if err := beamx.Run(ctx, p); err != nil { 132 log.Exitf(ctx, "Failed to execute job: %v", err) 133 } 134 }