github.com/apache/beam/sdks/v2@v2.48.2/go/examples/streaming_wordcap/wordcap.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  // streaming_wordcap is a toy streaming pipeline that uses PubSub. It
    17  // does the following:
    18  //
    19  //	(1) create a topic and publish a few messages to it
    20  //	(2) start a streaming pipeline that converts the messages to
    21  //	    upper case and logs the result.
    22  //
    23  // NOTE: it only runs on Dataflow and must be manually cancelled.
    24  package main
    25  
    26  import (
    27  	"context"
    28  	"flag"
    29  	"os"
    30  	"strings"
    31  
    32  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    33  	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/pubsubio"
    34  	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
    35  	"github.com/apache/beam/sdks/v2/go/pkg/beam/options/gcpopts"
    36  	"github.com/apache/beam/sdks/v2/go/pkg/beam/util/pubsubx"
    37  	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
    38  	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug"
    39  )
    40  
    41  var (
    42  	input = flag.String("input", os.ExpandEnv("$USER-wordcap"), "Pubsub input topic.")
    43  )
    44  
    45  var (
    46  	data = []string{
    47  		"foo",
    48  		"bar",
    49  		"baz",
    50  	}
    51  )
    52  
    53  func main() {
    54  	flag.Parse()
    55  	beam.Init()
    56  
    57  	ctx := context.Background()
    58  	project := gcpopts.GetProject(ctx)
    59  
    60  	log.Infof(ctx, "Publishing %v messages to: %v", len(data), *input)
    61  
    62  	defer pubsubx.CleanupTopic(ctx, project, *input)
    63  	sub, err := pubsubx.Publish(ctx, project, *input, data...)
    64  	if err != nil {
    65  		log.Fatal(ctx, err)
    66  	}
    67  
    68  	log.Infof(ctx, "Running streaming wordcap with subscription: %v", sub.ID())
    69  
    70  	p := beam.NewPipeline()
    71  	s := p.Root()
    72  
    73  	col := pubsubio.Read(s, project, *input, &pubsubio.ReadOptions{Subscription: sub.ID()})
    74  	str := beam.ParDo(s, func(b []byte) string {
    75  		return (string)(b)
    76  	}, col)
    77  	cap := beam.ParDo(s, strings.ToUpper, str)
    78  	debug.Print(s, cap)
    79  
    80  	if err := beamx.Run(context.Background(), p); err != nil {
    81  		log.Exitf(ctx, "Failed to execute job: %v", err)
    82  	}
    83  }