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

title: "CombineValues" <!-- 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="CombineValues" >}} Combines an iterable of values in a keyed collection of elements. See more information in the . In the following examples, we create a pipeline with a `PCollection` of produce. Then, we apply `CombineValues` in multiple ways to combine the keyed values in the `PCollection`. `CombineValues` accepts a function that takes an `iterable` of elements as an input, and combines them to return a single element. `CombineValues` expects a keyed `PCollection` of elements, where the value is an iterable of elements to be combined. We use the function which takes an `iterable` of numbers and adds them together. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONCombineValuesSimple" show="combinevaluessimple" >}} {{< /playground >}} We want the sum to be bounded up to a maximum value, so we use . We define a function `saturated_sum` which takes an `iterable` of numbers and adds them together, up to a predefined maximum number. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONCombineValuesFunction" show="combinevaluesfunction" >}} {{< /playground >}} We can also use lambda functions to simplify Example 2. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONCombineValuesLambda" show="combinevalueslambda" >}} {{< /playground >}} You can pass functions with multiple arguments to `CombineValues`. They are passed as additional positional arguments or keyword arguments to the function. In this example, the lambda function takes `values` and `max_value` as arguments. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONCombineValuesMultipleArguments" show="combinevaluesmultiple_arguments" >}} {{< /playground >}} The more general way to combine elements, and the most flexible, is with a class that inherits from `CombineFn`. : This creates an empty accumulator. For example, an empty accumulator for a sum would be `0`, while an empty accumulator for a product (multiplication) would be `1`. : Called once per element. Takes an accumulator and an input element, combines them and returns the updated accumulator. : Multiple accumulators could be processed in parallel, so this function helps merging them into a single accumulator. : It allows to do additional calculations before extracting a result. {{< playground height="700px" >}} {{< playgroundsnippet language="py" path="SDKPYTHONCombineValuesCombineFn" show="combinevaluescombinefn" >}} {{< /playground >}} You can use the following combiner transforms: {{< button-pydoc path="apache_beam.transforms.core" class="CombineValues" >}}