github.com/apache/beam/sdks/v2@v2.48.2/go/examples/native_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 // native_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 // This is a portable PubSub option and does not need to be run on Dataflow. 24 package main 25 26 import ( 27 "context" 28 "flag" 29 "os" 30 "strings" 31 32 "github.com/apache/beam/sdks/v2/go/examples/native_wordcap/nativepubsubio" 33 "github.com/apache/beam/sdks/v2/go/pkg/beam" 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 defer pubsubx.CleanupTopic(ctx, project, *input) 61 sub, err := pubsubx.Publish(ctx, project, *input, data...) 62 if err != nil { 63 log.Fatal(ctx, err) 64 } 65 66 log.Infof(ctx, "Running streaming native wordcap with subscription: %v", sub.ID()) 67 68 p := beam.NewPipeline() 69 s := p.Root() 70 71 col := nativepubsubio.Read(ctx, s, project, *input, sub.ID()) 72 str := beam.ParDo(s, func(b []byte) string { 73 return (string)(b) 74 }, col) 75 cap := beam.ParDo(s, strings.ToUpper, str) 76 debug.Print(s, cap) 77 78 if err := beamx.Run(context.Background(), p); err != nil { 79 log.Exitf(ctx, "Failed to execute job: %v", err) 80 } 81 }