Documentation

reduce() function

reduce() aggregates rows in each input table using a reducer function (fn).

The output for each table is the group key of the table with columns corresponding to each field in the reducer record. If the reducer record contains a column with the same name as a group key column, the group key column’s value is overwritten, and the outgoing group key is changed. However, if two reduced tables write to the same destination group key, the function returns an error.

Dropped columns

reduce() drops any columns that:

  • Are not part of the input table’s group key.
  • Are not explicitly mapped in the identity record or the reducer function (fn).
Function type signature
(<-tables: stream[B], fn: (accumulator: A, r: B) => A, identity: A) => stream[C] where A: Record, B: Record, C: Record
  • Copy
  • Fill window

For more information, see Function type signatures.

Parameters

fn

(Required) Reducer function to apply to each row record (r).

The reducer function accepts two parameters:

  • r: Record representing the current row.
  • accumulator: Record returned from the reducer function’s operation on the previous row.

identity

(Required) Record that defines the reducer record and provides initial values for the reducer operation on the first row.

May be used more than once in asynchronous processing use cases. The data type of values in the identity record determine the data type of output values.

tables

Input data. Default is piped-forward data (<-).

Examples

Compute the sum of the value column

import "sampledata"

sampledata.int()
    |> reduce(fn: (r, accumulator) => ({sum: r._value + accumulator.sum}), identity: {sum: 0})
  • Copy
  • Fill window

View example input and output

Compute the sum and count in a single reducer

import "sampledata"

sampledata.int()
    |> reduce(
        fn: (r, accumulator) => ({sum: r._value + accumulator.sum, count: accumulator.count + 1}),
        identity: {sum: 0, count: 0},
    )
  • Copy
  • Fill window

View example input and output

Compute the product of all values

import "sampledata"

sampledata.int()
    |> reduce(fn: (r, accumulator) => ({prod: r._value * accumulator.prod}), identity: {prod: 1})
  • Copy
  • Fill window

View example input and output

Calculate the average of all values

import "sampledata"

sampledata.int()
    |> reduce(
        fn: (r, accumulator) =>
            ({
                count: accumulator.count + 1,
                total: accumulator.total + r._value,
                avg: float(v: accumulator.total + r._value) / float(v: accumulator.count + 1),
            }),
        identity: {count: 0, total: 0, avg: 0.0},
    )
  • Copy
  • Fill window

View example input and output


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: