Project: Beam (App Definition and Development / Streaming & Messaging)
File: pardo.md

title: "ParDo" <!-- 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. --> {{< localstorage language language-py >}} {{< button-pydoc path="apache_beam.transforms.core" class="ParDo" >}} A transform for generic parallel processing. A `ParDo` transform considers each element in the input `PCollection`, performs some processing function (your user code) on that element, and emits zero or more elements to an output `PCollection`. See more information in the . In the following examples, we explore how to create custom `DoFn`s and access the timestamp and windowing information. The following example defines a simple `DoFn` class called `SplitWords` which stores the `delimiter` as an object field. The `process` method is called once per element, and it can yield zero or more output elements. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONParDoDoFn" show="pardodofn" >}} {{< /playground >}} In this example, we add new parameters to the `process` method to bind parameter values at runtime. binds the timestamp information as an object. binds the window information as the appropriate object. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONParDoDoFnParams" show="pardodofn_params" >}} {{< /playground >}} A can be customized with a number of methods that can help create more complex behaviors. You can customize what a worker does when it starts and shuts down with `setup` and `teardown`. You can also customize what to do when a starts and finishes with `startbundle` and `finishbundle`. : Called whenever the `DoFn` instance is deserialized on the worker. This means it can be called more than once per worker because multiple instances of a given `DoFn` subclass may be created

due to parallelization, or due to garbage collection after a period of disuse). This is a good place to connect to database instances, open network connections or other resources. : Called once per bundle of elements before calling `process` on the first element of the bundle. This is a good place to start keeping track of the bundle elements. : Called once per element, can yield zero or more elements. Additional `args` or `*kwargs` can be passed through . [required] : Called once per bundle of elements after calling `process` after the last element of the bundle, can yield zero or more elements. This is a good place to do batch calls on a bundle of elements, such as running a database query. For example, you can initialize a batch in `start_bundle`, add elements to the batch in `process` instead of yielding them, then running a batch query on those elements on `finish_bundle`, and yielding all the results. Note that yielded elements from `finish_bundle` must be of the type . You need to provide a timestamp as a unix timestamp, which you can get from the last processed element. You also need to provide a window, which you can get from the last processed element like in the example below. : Called once (as a best effort) per `DoFn` instance when the `DoFn` instance is shutting down. This is a good place to close database instances, close network connections or other resources. Note that `teardown` is called as a best effort and is not guaranteed. For example, if the worker crashes, `teardown` might not be called. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONParDoDoFnMethods" show="pardodofn_methods" >}} {{< /playground >}} Known issues: `DoFn.teardown()` metrics are lost. behaves the same, but produces exactly one output for each input. behaves the same as `Map`, but for each input it may produce zero or more outputs. is useful if the function is just deciding whether to output an element or not. {{< button-pydoc path="apache_beam.transforms.core" class="Pardo" >}}