You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/06 01:24:50 UTC

[GitHub] [beam] youngoli commented on a change in pull request #12445: [BEAM-9919] Added an External Transform API to Go SDK

youngoli commented on a change in pull request #12445:
URL: https://github.com/apache/beam/pull/12445#discussion_r466056636



##########
File path: sdks/go/examples/xlang/wordcount/xlang_wordcount.go
##########
@@ -0,0 +1,107 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// xlang_wordcount exemplifies using a cross language transform from Python to count words
+package main

Review comment:
       This example requires running an expansion service separately in order to work, right? I'd add instructions to the package comment on how to run that so people can run this example without existing knowledge of how xlang works. See the stringsplit example for an example of this. It requires running on a job service that supports splitting, so I included instructions for running an external job service.

##########
File path: sdks/go/examples/xlang/wordcount/xlang_wordcount.go
##########
@@ -0,0 +1,107 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// xlang_wordcount exemplifies using a cross language transform from Python to count words
+package main
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"log"
+	"regexp"
+	"strings"
+
+	"github.com/apache/beam/sdks/go/pkg/beam/core/typex"
+	"github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx"
+
+	"github.com/apache/beam/sdks/go/pkg/beam"
+	"github.com/apache/beam/sdks/go/pkg/beam/io/textio"
+	"github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
+
+	// Imports to enable correct filesystem access and runner setup in LOOPBACK mode
+	_ "github.com/apache/beam/sdks/go/pkg/beam/io/filesystem/gcs"
+	_ "github.com/apache/beam/sdks/go/pkg/beam/io/filesystem/local"
+	_ "github.com/apache/beam/sdks/go/pkg/beam/runners/universal"
+)
+
+var (
+	// Set this option to choose a different input file or glob.
+	input = flag.String("input", "./input", "File(s) to read.")
+
+	// Set this required option to specify where to write the output.
+	output = flag.String("output", "./output", "Output file (required).")
+)
+
+var (
+	wordRE  = regexp.MustCompile(`[a-zA-Z]+('[a-z])?`)
+	empty   = beam.NewCounter("extract", "emptyLines")
+	lineLen = beam.NewDistribution("extract", "lineLenDistro")
+)
+
+// extractFn is a DoFn that emits the words in a given line.
+func extractFn(ctx context.Context, line string, emit func(string)) {
+	lineLen.Update(ctx, int64(len(line)))
+	if len(strings.TrimSpace(line)) == 0 {
+		empty.Inc(ctx, 1)
+	}
+	for _, word := range wordRE.FindAllString(line, -1) {
+		emit(word)
+	}
+}
+
+// formatFn is a DoFn that formats a word and its count as a string.
+func formatFn(w string, c int64) string {
+	return fmt.Sprintf("%s: %v", w, c)
+}
+
+func init() {
+	beam.RegisterFunction(extractFn)
+	beam.RegisterFunction(formatFn)
+}
+
+func main() {
+	flag.Parse()
+	beam.Init()
+
+	if *output == "" {
+		log.Fatal("No output provided")
+	}
+
+	p := beam.NewPipeline()
+	s := p.Root()
+
+	lines := textio.Read(s, *input)
+	col := beam.ParDo(s, extractFn, lines)
+
+	// Using Cross-language Count from Python's test expansion service
+	// TODO(pskevin): Cleaner using-face API
+	outputType := typex.NewKV(typex.New(reflectx.String), typex.New(reflectx.Int64))
+	external := &beam.ExternalTransform{
+		In:            []beam.PCollection{col},
+		Urn:           "beam:transforms:xlang:count",
+		ExpansionAddr: "localhost:8118",

Review comment:
       Expansion address seems like a good candidate to be a flag instead.

##########
File path: sdks/go/pkg/beam/external.go
##########
@@ -16,10 +16,144 @@
 package beam
 
 import (
+	"context"
+	"fmt"
+
 	"github.com/apache/beam/sdks/go/pkg/beam/core/graph"
+	"github.com/apache/beam/sdks/go/pkg/beam/core/runtime/graphx"
 	"github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+	jobpb "github.com/apache/beam/sdks/go/pkg/beam/model/jobmanagement_v1"
+	pipepb "github.com/apache/beam/sdks/go/pkg/beam/model/pipeline_v1"
+	"google.golang.org/grpc"
 )
 
+// ExternalTransform represents the cross-language transform in and out of the Pipeline as a MultiEdge and Expanded proto respectively
+type ExternalTransform struct {
+	id                int
+	Urn               string
+	Payload           []byte
+	In                []PCollection
+	Out               []FullType
+	Bounded           bool
+	ExpansionAddr     string
+	Components        *pipepb.Components
+	ExpandedTransform *pipepb.PTransform
+	Requirements      []string
+}
+
+// CrossLanguage is the temporary API to execute external transforms
+func CrossLanguage(s Scope, p *Pipeline, e *ExternalTransform) []PCollection {
+	return MustN(TryCrossLanguage(s, p, e))
+}
+
+func TryCrossLanguage(s Scope, p *Pipeline, e *ExternalTransform) ([]PCollection, error) {
+	if e.ExpansionAddr == "" { // TODO(pskevin): Better way to check if the value was ever set
+		// return Legacy External API
+	}
+
+	// Add ExternalTransform to the Graph
+
+	// Validating scope and inputs
+	if !s.IsValid() {
+		return nil, errors.New("invalid scope")
+	}
+	for i, col := range e.In {
+		if !col.IsValid() {
+			return nil, errors.Errorf("invalid pcollection to external: index %v", i)
+		}
+	}
+
+	// Using exisiting MultiEdge format to represent ExternalTransform (already backwards compatible)

Review comment:
       Nit: Typo
   ```suggestion
   	// Using existing MultiEdge format to represent ExternalTransform (already backwards compatible)
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org