pcoet commented on a change in pull request #16001: URL: https://github.com/apache/beam/pull/16001#discussion_r751716181
########## File path: website/www/site/content/en/documentation/sdks/python-multi-language-pipelines.md ########## @@ -0,0 +1,213 @@ +--- +type: languages +title: "Python multi-language pipelines" +--- +<!-- +Licensed 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. +--> + +# Python multi-language pipelines + +This page provides a high-level overview of creating multi-language pipelines with the Apache Beam SDK for Python. To build and run a multi-language Python pipeline, you need a Python environment with the Beam SDK installed. If you don’t have an environment set up, first complete the [Apache Beam Python SDK Quickstart](/get-started/quickstart-py/). + +A *multi-language pipeline* is a pipeline that’s built in one Beam SDK language and uses one or more transforms from another Beam SDK language. These “other-language” transforms are called *cross-language transforms*. The idea is to make pipeline components easier to share across the Beam SDKs, and to grow the pool of available transforms for all the SDKs. In the examples below, the multi-language pipeline is built with the Beam Python SDK, and the cross-language transforms are built with the Beam Java SDK. + +## Create a cross-language transform + +Here's a simple Java transform that adds a prefix to an input string: + +```java +public class JavaPrefix extends PTransform<PCollection<String>, PCollection<String>> { + + final String prefix; + + public JavaPrefix(String prefix) { + this.prefix = prefix; + } + + class AddPrefixDoFn extends DoFn<String, String> { + + @ProcessElement + public void process(@Element String input, OutputReceiver<String> o) { + o.output(prefix + input); + } + } + + @Override + public PCollection<String> expand(PCollection<String> input) { + return input + .apply( + "AddPrefix", + ParDo.of(new AddPrefixDoFn())); + } +} +``` + +To make this available as a cross-language transform, you have to add a config object and a builder. + +> **Note:** Starting with Beam 2.34.0, Python SDK users can use some Java transforms without writing additional Java code. To learn more, see [Creating cross-language Java transforms](/documentation/programming-guide/#1311-creating-cross-language-java-transforms). + +The config object is a simple Java object (POJO) that has fields required by the transform. + +```java +public class JavaPrefixConfiguration { + + String prefix; + + public void setPrefix(String prefix) { + this.prefix = prefix; + } +} +``` + +The builder class must implement [ExternalTransformBuilder](https://beam.apache.org/releases/javadoc/current/org/apache/beam/sdk/transforms/ExternalTransformBuilder.html) and override `buildExternal`, which uses the config object. + +```java +public class JavaPrefixBuilder implements + ExternalTransformBuilder<JavaPrefixConfiguration, PCollection<String>, PCollection<String>> { + + @Override + public PTransform<PCollection<String>, PCollection<String>> buildExternal( + JavaPrefixConfiguration configuration) { + return new JavaPrefix(configuration.prefix); + } +} +``` + +You also need to add a registrar class to register your transform with the expansion service. + +```java +@AutoService(ExternalTransformRegistrar.class) +public class JavaPrefixRegistrar implements ExternalTransformRegistrar { + + final String URN = "my.beam.transform.javaprefix"; Review comment: Done -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
