You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lo...@apache.org on 2022/06/22 23:20:29 UTC

[beam-starter-go] 07/17: register DoFn

This is an automated email from the ASF dual-hosted git repository.

lostluck pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/beam-starter-go.git

commit f54f14e2c3c6c06252700ac210588e5e87ad3a8e
Author: David Cavazos <dc...@google.com>
AuthorDate: Thu Jun 16 11:56:11 2022 -0700

    register DoFn
---
 main.go | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/main.go b/main.go
index 79fd81e..c435cb4 100644
--- a/main.go
+++ b/main.go
@@ -14,9 +14,19 @@ var (
 	input_text = flag.String("input-text", "Default input text", "Input text to print.")
 )
 
+func init() {
+	// DoFns should be registered with Beam.
+	beam.RegisterFunction(printAndEmit)
+}
+
+func printAndEmit(element string, emit func(string)) {
+	fmt.Println(element)
+	emit(element)
+}
+
 func my_pipeline(scope beam.Scope, input_text string) beam.PCollection {
 	elements := beam.Create(scope, "Hello", "World!", input_text)
-	elements = beam.ParDo(scope, func(elem string, emit func(string)) { fmt.Println(elem); emit(elem) }, elements)
+	elements = beam.ParDo(scope, printAndEmit, elements)
 	return elements
 }