Package org.apache.beam.sdk.transforms
Class Mean
java.lang.Object
org.apache.beam.sdk.transforms.Mean
PTransforms for computing the arithmetic mean (a.k.a. average) of the elements in a
PCollection, or the mean of the values associated with each key in a PCollection
of KVs.
Example 1: get the mean of a PCollection of Longs.
PCollection<Long> input = ...;
PCollection<Double> mean = input.apply(Mean.<Long>globally());
Example 2: calculate the mean of the Integers associated with each unique key (which
is of type String).
PCollection<KV<String, Integer>> input = ...;
PCollection<KV<String, Double>> meanPerKey =
input.apply(Mean.<String, Integer>perKey());
-
Method Summary
Modifier and TypeMethodDescriptionstatic <NumT extends Number>
Combine.Globally<NumT, Double> globally()Returns aPTransformthat takes an inputPCollection<NumT>and returns aPCollection<Double>whose contents is the mean of the inputPCollection's elements, or0if there are no elements.static <NumT extends Number>
Combine.AccumulatingCombineFn<NumT, org.apache.beam.sdk.transforms.Mean.CountSum<NumT>, Double> of()ACombine.CombineFnthat computes the arithmetic mean (a.k.a.static <K,NumT extends Number>
Combine.PerKey<K, NumT, Double> perKey()Returns aPTransformthat takes an inputPCollection<KV<K, N>>and returns aPCollection<KV<K, Double>>that contains an output element mapping each distinct key in the inputPCollectionto the mean of the values associated with that key in the inputPCollection.
-
Method Details
-
globally
Returns aPTransformthat takes an inputPCollection<NumT>and returns aPCollection<Double>whose contents is the mean of the inputPCollection's elements, or0if there are no elements.- Type Parameters:
NumT- the type of theNumbers being combined
-
perKey
Returns aPTransformthat takes an inputPCollection<KV<K, N>>and returns aPCollection<KV<K, Double>>that contains an output element mapping each distinct key in the inputPCollectionto the mean of the values associated with that key in the inputPCollection.See
Combine.PerKeyfor how this affects timestamps and bucketing.- Type Parameters:
K- the type of the keysNumT- the type of theNumbers being combined
-
of
public static <NumT extends Number> Combine.AccumulatingCombineFn<NumT,org.apache.beam.sdk.transforms.Mean.CountSum<NumT>, of()Double> ACombine.CombineFnthat computes the arithmetic mean (a.k.a. average) of anIterableof numbers of typeN, useful as an argument toCombine.globally(org.apache.beam.sdk.transforms.SerializableFunction<java.lang.Iterable<V>, V>)orCombine.perKey(org.apache.beam.sdk.transforms.SerializableFunction<java.lang.Iterable<V>, V>).Returns
Double.NaNif combining zero elements.- Type Parameters:
NumT- the type of theNumbers being combined
-