layout: section title: “Mean” permalink: /documentation/transforms/java/aggregation/mean/ section_menu: section-menu/documentation.html

Mean

  • Mean.globally() returns a transform that then returns a collection whose contents is the mean of the input collection's elements. If there are no elements in the input collection, it returns 0.
  • Mean.perKey() returns a transform that returns a collection that contains an output element mapping each distinct key in the input collection to the mean of the values associated with that key in the input collection.

Examples

Example 1: get the mean of a PCollection of Longs.

PCollection<Double> input = ...;
PCollection<Double> mean = input.apply(Mean.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, Integer>> meanPerKey =
     input.apply(Mean.perKey());

Related transforms

  • [Max]({{ site.baseurl }}/documentation/transforms/java/aggregation/max) for computing maximum values in a collection
  • [Min]({{ site.baseurl }}/documentation/transforms/java/aggregation/min) for computing maximum values in a collection
  • [Combine]({{ site.baseurl }}/documentation/transforms/java/aggregation/combine) for combining all values associated with a key to a single result