github.com/apache/beam/sdks/v2@v2.48.2/go/examples/xlang/transforms.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 xlang contains functionality for testing cross-language transforms. 17 package xlang 18 19 import ( 20 "reflect" 21 22 "github.com/apache/beam/sdks/v2/go/pkg/beam" 23 "github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex" 24 "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx" 25 ) 26 27 func init() { 28 beam.RegisterType(reflect.TypeOf((*prefixPayload)(nil)).Elem()) 29 } 30 31 // prefixPayload is a struct used to represent the payload of the Prefix 32 // cross-language transform. 33 // 34 // This must match the struct that the expansion service is expecting to 35 // receive. For example, at the time of writing this comment, that struct is 36 // the one in the following link. 37 // https://github.com/apache/beam/blob/v2.29.0/sdks/java/testing/expansion-service/src/test/java/org/apache/beam/sdk/testing/expansion/TestExpansionService.java#L191 38 type prefixPayload struct { 39 Data string 40 } 41 42 // Prefix wraps a cross-language transform call to the Prefix transform. This 43 // transform takes a PCollection of strings as input, and a payload defining a 44 // prefix string, and appends that as prefix to each input string. 45 // 46 // This serves as an example of a cross-language transform with a payload. 47 func Prefix(s beam.Scope, prefix string, addr string, col beam.PCollection) beam.PCollection { 48 s = s.Scope("XLangTest.Prefix") 49 50 pl := beam.CrossLanguagePayload(prefixPayload{Data: prefix}) 51 outT := beam.UnnamedOutput(typex.New(reflectx.String)) 52 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:prefix", pl, addr, beam.UnnamedInput(col), outT) 53 return outs[beam.UnnamedOutputTag()] 54 } 55 56 func CoGroupByKey(s beam.Scope, addr string, col1, col2 beam.PCollection) beam.PCollection { 57 s = s.Scope("XLangTest.CoGroupByKey") 58 namedInputs := map[string]beam.PCollection{"col1": col1, "col2": col2} 59 outT := beam.UnnamedOutput(typex.NewCoGBK(typex.New(reflectx.Int64), typex.New(reflectx.String))) 60 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:cgbk", nil, addr, namedInputs, outT) 61 return outs[beam.UnnamedOutputTag()] 62 } 63 64 func CombinePerKey(s beam.Scope, addr string, col beam.PCollection) beam.PCollection { 65 s = s.Scope("XLangTest.CombinePerKey") 66 outT := beam.UnnamedOutput(typex.NewKV(typex.New(reflectx.String), typex.New(reflectx.Int64))) 67 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:compk", nil, addr, beam.UnnamedInput(col), outT) 68 return outs[beam.UnnamedOutputTag()] 69 } 70 71 func CombineGlobally(s beam.Scope, addr string, col beam.PCollection) beam.PCollection { 72 s = s.Scope("XLangTest.CombineGlobally") 73 outT := beam.UnnamedOutput(typex.New(reflectx.Int64)) 74 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:comgl", nil, addr, beam.UnnamedInput(col), outT) 75 return outs[beam.UnnamedOutputTag()] 76 } 77 78 func Flatten(s beam.Scope, addr string, col1, col2 beam.PCollection) beam.PCollection { 79 s = s.Scope("XLangTest.Flatten") 80 namedInputs := map[string]beam.PCollection{"col1": col1, "col2": col2} 81 outT := beam.UnnamedOutput(typex.New(reflectx.Int64)) 82 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:flatten", nil, addr, namedInputs, outT) 83 return outs[beam.UnnamedOutputTag()] 84 } 85 86 func GroupByKey(s beam.Scope, addr string, col beam.PCollection) beam.PCollection { 87 s = s.Scope("XLangTest.GroupByKey") 88 outT := beam.UnnamedOutput(typex.NewCoGBK(typex.New(reflectx.String), typex.New(reflectx.Int64))) 89 outs := beam.CrossLanguage(s, "beam:transforms:xlang:test:gbk", nil, addr, beam.UnnamedInput(col), outT) 90 return outs[beam.UnnamedOutputTag()] 91 } 92 93 func Multi(s beam.Scope, addr string, main1, main2, side beam.PCollection) (mainOut, sideOut beam.PCollection) { 94 s = s.Scope("XLangTest.Multi") 95 namedInputs := map[string]beam.PCollection{"main1": main1, "main2": main2, "side": side} 96 outT := typex.New(reflectx.String) 97 namedOutputs := map[string]typex.FullType{"main": outT, "side": outT} 98 multi := beam.CrossLanguage(s, "beam:transforms:xlang:test:multi", nil, addr, namedInputs, namedOutputs) 99 return multi["main"], multi["side"] 100 } 101 102 func Partition(s beam.Scope, addr string, col beam.PCollection) (out0, out1 beam.PCollection) { 103 s = s.Scope("XLangTest.Partition") 104 outT := typex.New(reflectx.Int64) 105 namedOutputs := map[string]typex.FullType{"0": outT, "1": outT} 106 c := beam.CrossLanguage(s, "beam:transforms:xlang:test:partition", nil, addr, beam.UnnamedInput(col), namedOutputs) 107 return c["0"], c["1"] 108 } 109 110 func Count(s beam.Scope, addr string, col beam.PCollection) beam.PCollection { 111 s = s.Scope("XLang.Count") 112 outT := beam.UnnamedOutput(typex.NewKV(typex.New(reflectx.String), typex.New(reflectx.Int64))) 113 c := beam.CrossLanguage(s, "beam:transforms:xlang:count", nil, addr, beam.UnnamedInput(col), outT) 114 return c[beam.UnnamedOutputTag()] 115 }