github.com/apache/beam/sdks/v2@v2.48.2/go/examples/xlang/combine/combine.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 // combine exemplifies using a cross-language combine per key transform from a test expansion service. 17 // 18 // Prerequisites to run wordcount: 19 // –> [Required] Job needs to be submitted to a portable runner (--runner=universal) 20 // –> [Required] Endpoint of job service needs to be passed (--endpoint=<ip:port>) 21 // –> [Required] Endpoint of expansion service needs to be passed (--expansion_addr=<ip:port>) 22 // –> [Optional] Environment type can be LOOPBACK. Defaults to DOCKER. (--environment_type=LOOPBACK|DOCKER) 23 package main 24 25 import ( 26 "context" 27 "flag" 28 "fmt" 29 "log" 30 31 "github.com/apache/beam/sdks/v2/go/examples/xlang" 32 "github.com/apache/beam/sdks/v2/go/pkg/beam" 33 "github.com/apache/beam/sdks/v2/go/pkg/beam/register" 34 "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert" 35 "github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx" 36 37 // Imports to enable correct filesystem access and runner setup in LOOPBACK mode 38 _ "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/gcs" 39 _ "github.com/apache/beam/sdks/v2/go/pkg/beam/io/filesystem/local" 40 _ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/universal" 41 ) 42 43 var ( 44 expansionAddr = flag.String("expansion_addr", "", "Address of Expansion Service") 45 ) 46 47 // formatFn is a DoFn that formats a word and its count as a string. 48 func formatFn(w string, c int64) string { 49 return fmt.Sprintf("%s:%v", w, c) 50 } 51 52 // KV used to represent KV PCollection values 53 type KV struct { 54 X string 55 Y int64 56 } 57 58 func getKV(kv KV, emit func(string, int64)) { 59 emit(kv.X, kv.Y) 60 } 61 62 func sumCounts(key string, iter func(*int64) bool) (string, int64) { 63 var count, sum int64 64 for iter(&count) { 65 sum += count 66 } 67 return key, sum 68 } 69 70 func init() { 71 register.Function2x1(formatFn) 72 register.Function2x0(getKV) 73 register.Function2x2(sumCounts) 74 75 register.Emitter2[string, int64]() 76 register.Iter1[int64]() 77 } 78 79 func main() { 80 flag.Parse() 81 beam.Init() 82 83 if *expansionAddr == "" { 84 log.Fatal("No expansion address provided") 85 } 86 87 p := beam.NewPipeline() 88 s := p.Root() 89 90 // Using the cross-language transform 91 kvs := beam.Create(s, KV{X: "a", Y: 1}, KV{X: "a", Y: 2}, KV{X: "b", Y: 3}) 92 ins := beam.ParDo(s, getKV, kvs) 93 c := xlang.CombinePerKey(s, *expansionAddr, ins) 94 95 formatted := beam.ParDo(s, formatFn, c) 96 passert.Equals(s, formatted, "a:3", "b:3") 97 98 if err := beamx.Run(context.Background(), p); err != nil { 99 log.Fatalf("Failed to execute job: %v", err) 100 } 101 }