github.com/apache/beam/sdks/v2@v2.48.2/go/examples/xlang/flatten/flatten.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 // flatten exemplifies using a cross-language flatten transform from a test expansion service. 19 // 20 // Prerequisites to run wordcount: 21 // –> [Required] Job needs to be submitted to a portable runner (--runner=universal) 22 // –> [Required] Endpoint of job service needs to be passed (--endpoint=<ip:port>) 23 // –> [Required] Endpoint of expansion service needs to be passed (--expansion_addr=<ip:port>) 24 // –> [Optional] Environment type can be LOOPBACK. Defaults to DOCKER. (--environment_type=LOOPBACK|DOCKER) 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(c int64) string { 49 return fmt.Sprintf("%v", c) 50 } 51 52 func init() { 53 register.Function1x1(formatFn) 54 } 55 56 func main() { 57 flag.Parse() 58 beam.Init() 59 60 if *expansionAddr == "" { 61 log.Fatal("No expansion address provided") 62 } 63 64 p := beam.NewPipeline() 65 s := p.Root() 66 67 col1 := beam.CreateList(s, []int64{1, 2, 3}) 68 col2 := beam.CreateList(s, []int64{4, 5, 6}) 69 70 // Using the cross-language transform 71 c := xlang.Flatten(s, *expansionAddr, col1, col2) 72 73 formatted := beam.ParDo(s, formatFn, c) 74 passert.Equals(s, formatted, "1", "2", "3", "4", "5", "6") 75 76 if err := beamx.Run(context.Background(), p); err != nil { 77 log.Fatalf("Failed to execute job: %v", err) 78 } 79 }