github.com/apache/beam/sdks/v2@v2.48.2/go/test/load/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  package main
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    24  	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/synthetic"
    25  	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
    26  	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
    27  	"github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/top"
    28  	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
    29  	"github.com/apache/beam/sdks/v2/go/test/load"
    30  )
    31  
    32  var (
    33  	fanout = flag.Int(
    34  		"fanout",
    35  		1,
    36  		"A number of combine operations to perform in parallel.")
    37  	topCount = flag.Int(
    38  		"top_count",
    39  		20,
    40  		"A number of greatest elements to extract from the PCollection.")
    41  	syntheticConfig = flag.String(
    42  		"input_options",
    43  		"",
    44  		"A JSON object that describes the configuration for synthetic source.")
    45  )
    46  
    47  func parseSyntheticConfig() synthetic.SourceConfig {
    48  	if *syntheticConfig == "" {
    49  		panic("--input_options not provided")
    50  	} else {
    51  		encoded := []byte(*syntheticConfig)
    52  		return synthetic.DefaultSourceConfig().BuildFromJSON(encoded)
    53  	}
    54  }
    55  
    56  func init() {
    57  	register.Function2x1(compareLess)
    58  	register.Function3x0(getElement)
    59  	register.Emitter2[[]byte, []byte]()
    60  }
    61  
    62  func compareLess(key []byte, value []byte) bool {
    63  	return bytes.Compare(key, value) < 0
    64  }
    65  
    66  func getElement(key []byte, value [][]byte, emit func([]byte, []byte)) {
    67  	emit(key, value[0])
    68  }
    69  
    70  func main() {
    71  	flag.Parse()
    72  	beam.Init()
    73  
    74  	ctx := context.Background()
    75  
    76  	p, s := beam.NewPipelineWithRoot()
    77  	src := synthetic.SourceSingle(s, parseSyntheticConfig())
    78  	src = beam.ParDo(s, &load.RuntimeMonitor{}, src)
    79  	for i := 0; i < *fanout; i++ {
    80  		pcoll := top.LargestPerKey(s, src, *topCount, compareLess)
    81  		pcoll = beam.ParDo(s, getElement, pcoll)
    82  		pcoll = beam.ParDo(s, &load.RuntimeMonitor{}, pcoll)
    83  		_ = pcoll
    84  	}
    85  
    86  	presult, err := beamx.RunWithMetrics(ctx, p)
    87  	if err != nil {
    88  		log.Fatalf(ctx, "Failed to execute job: %v", err)
    89  	}
    90  
    91  	if presult != nil {
    92  		metrics := presult.Metrics().AllMetrics()
    93  		load.PublishMetrics(metrics)
    94  	}
    95  }