github.com/apache/beam/sdks/v2@v2.48.2/go/examples/xlang/combine_globally/combine_globally.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_globally exemplifies using a cross-language combine global 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(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  	in := beam.CreateList(s, []int64{1, 2, 3})
    68  
    69  	// Using the cross-language transform
    70  	c := xlang.CombineGlobally(s, *expansionAddr, in)
    71  
    72  	formatted := beam.ParDo(s, formatFn, c)
    73  	passert.Equals(s, formatted, "6")
    74  
    75  	if err := beamx.Run(context.Background(), p); err != nil {
    76  		log.Fatalf("Failed to execute job: %v", err)
    77  	}
    78  }