Package org.apache.beam.sdk.transforms
Class Mean
java.lang.Object
org.apache.beam.sdk.transforms.Mean
PTransform
s 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 KV
s.
Example 1: get the mean of a PCollection
of Long
s.
PCollection<Long> input = ...;
PCollection<Double> mean = input.apply(Mean.<Long>globally());
Example 2: calculate the mean of the Integer
s 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 aPTransform
that takes an inputPCollection<NumT>
and returns aPCollection<Double>
whose contents is the mean of the inputPCollection
's elements, or0
if there are no elements.static <NumT extends Number>
Combine.AccumulatingCombineFn<NumT, org.apache.beam.sdk.transforms.Mean.CountSum<NumT>, Double> of()
ACombine.CombineFn
that computes the arithmetic mean (a.k.a.static <K,
NumT extends Number>
Combine.PerKey<K, NumT, Double> perKey()
Returns aPTransform
that takes an inputPCollection<KV<K, N>>
and returns aPCollection<KV<K, Double>>
that contains an output element mapping each distinct key in the inputPCollection
to the mean of the values associated with that key in the inputPCollection
.
-
Method Details
-
globally
Returns aPTransform
that takes an inputPCollection<NumT>
and returns aPCollection<Double>
whose contents is the mean of the inputPCollection
's elements, or0
if there are no elements.- Type Parameters:
NumT
- the type of theNumber
s being combined
-
perKey
Returns aPTransform
that takes an inputPCollection<KV<K, N>>
and returns aPCollection<KV<K, Double>>
that contains an output element mapping each distinct key in the inputPCollection
to the mean of the values associated with that key in the inputPCollection
.See
Combine.PerKey
for how this affects timestamps and bucketing.- Type Parameters:
K
- the type of the keysNumT
- the type of theNumber
s being combined
-
of
public static <NumT extends Number> Combine.AccumulatingCombineFn<NumT,org.apache.beam.sdk.transforms.Mean.CountSum<NumT>, of()Double> ACombine.CombineFn
that computes the arithmetic mean (a.k.a. average) of anIterable
of 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.NaN
if combining zero elements.- Type Parameters:
NumT
- the type of theNumber
s being combined
-