lostluck commented on a change in pull request #11499: URL: https://github.com/apache/beam/pull/11499#discussion_r413293884
########## 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: size /5 might not divide evenly? ---------------------------------------------------------------- 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: [email protected]
