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/04/23 21:19:53 UTC

[GitHub] [beam] youngoli commented on a change in pull request #11499: [BEAM-9775] Go SDF Example

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



##########
File path: sdks/go/examples/stringsplit/stringsplit.go
##########
@@ -0,0 +1,230 @@
+// 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.
+
+// An example of using a Splittable DoFn in the Go SDK with a portable runner.
+//
+// The following instructions describe how to execute this example in the
+// Flink local runner.
+//
+// 1. From a command line, navigate to the top-level beam/ directory and run
+// the Flink job server:
+//    ./gradlew :runners:flink:1.10:job-server:runShadow -Djob-host=localhost -Dflink-master=local
+//
+// 2. The job server is ready to receive jobs once it outputs a log like the
+// following: `JobService started on localhost:8099`. Take note of the endpoint
+// in that log message.
+//
+// 3. While the job server is running in one command line window, create a
+// second one in the same directory and run this example with the following
+// command, using the endpoint you noted from step 2:
+//    go run sdks/go/examples/stringsplit/stringsplit.go --runner=universal --endpoint=localhost:8099
+//
+// 4. Once the pipeline is complete, the job server can be closed with ctrl+C.
+// To check the output of the pipeline, search the job server logs for the
+// phrase "StringSplit Output".
+package main
+
+import (
+	"context"
+	"flag"
+	"reflect"
+
+	"github.com/apache/beam/sdks/go/examples/stringsplit/offsetrange"
+	"github.com/apache/beam/sdks/go/pkg/beam"
+	"github.com/apache/beam/sdks/go/pkg/beam/log"
+	"github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
+)
+
+func init() {
+	beam.RegisterType(reflect.TypeOf((*StringSplitFn)(nil)).Elem())
+}
+
+// StringSplitFn is a Splittable DoFn that splits strings into substrings of the
+// specified size (for example, to be able to fit them in a small buffer).
+// See ProcessElement for more details.
+type StringSplitFn struct {
+	BufSize int64
+}
+
+// CreateInitialRestriction creates an offset range restriction for each element
+// with the size of the restriction corresponding to the length of the string.
+func (fn *StringSplitFn) CreateInitialRestriction(s string) offsetrange.Restriction {
+	return offsetrange.Restriction{Start: 0, End: int64(len(s))}
+}
+
+// SplitRestriction performs initial splits so that each restriction is split
+// into 5.
+func (fn *StringSplitFn) SplitRestriction(s string, rest offsetrange.Restriction) []offsetrange.Restriction {
+	size := rest.End - rest.Start
+	splitPts := []int64{
+		rest.Start,
+		rest.Start + (size / 5),

Review comment:
       I don't think that's an issue. It might not always divide evenly, and in small enough inputs it could create restrictions with nothing to emit, but it still gives correct output even in those cases (which I did test out). What I was really aiming for was just giving a basic example of what the initial split should do, while also splitting in a way that doesn't evenly lie on claimable blocks, so I can include the logic in ProcessElement to find the nearest claimable block, and include comments about how only the start of the claimed block needs to lie in the restriction.




----------------------------------------------------------------
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