initial draft of reduce function doc
parent
15f62e9343
commit
ba8a2243f5
|
@ -0,0 +1,117 @@
|
|||
---
|
||||
title: reduce() function
|
||||
description: The `reduce()` function aggregates records in each table according to the reducer, `fn`.
|
||||
menu:
|
||||
v2_0_ref:
|
||||
name: reduce
|
||||
parent: built-in-transformations
|
||||
weight: 401
|
||||
---
|
||||
|
||||
The `reduce()` function aggregates records in each table according to the reducer, `fn`.
|
||||
The output for each table is the group key of the table with columns corresponding
|
||||
to each field in the reducer object.
|
||||
|
||||
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 resulting record is regrouped
|
||||
into the appropriate table.
|
||||
|
||||
_**Function type:** Transformation_
|
||||
|
||||
```js
|
||||
reduce(
|
||||
fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
|
||||
identity: {sum: 0.0}
|
||||
)
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
### fn
|
||||
Function to apply to each record with a reducer object ([`identity`](#identity)).
|
||||
|
||||
_**Data type:** Function_
|
||||
|
||||
###### fn syntax
|
||||
```js
|
||||
// Pattern
|
||||
fn: (r, accumulator) => ({ newColumnName: r.column + accumulator.identityKey })
|
||||
|
||||
// Example
|
||||
fn: (r, accumulator) => ({ valueSum: r._value + accumulator.sum })
|
||||
```
|
||||
|
||||
#### r
|
||||
Object representing each row or record.
|
||||
|
||||
#### accumulator
|
||||
Reducer object defined by [`identity`](#identity).
|
||||
|
||||
### identity
|
||||
Defines the reducer object and provides initial values to use when creating a reducer.
|
||||
May be used more than once in asynchronous processing use cases.
|
||||
_The data type of values in the `identity` object determine the data type of output values._
|
||||
|
||||
_**Data type:** Object_
|
||||
|
||||
###### identity object syntax
|
||||
```js
|
||||
// Pattern
|
||||
identity: {key1: value1, key2: value2}
|
||||
|
||||
// Example
|
||||
identity: {sum: 0.0, count: 0.0}
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
##### Compute the sum of the value column
|
||||
```js
|
||||
from(bucket:"example-bucket")
|
||||
|> filter(fn: (r) =>
|
||||
r._measurement == "cpu" and
|
||||
r._field == "usage_system" and
|
||||
r.service == "app-server"
|
||||
)
|
||||
|> range(start:-12h)
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
sum: r._value + accumulator.sum
|
||||
}),
|
||||
identity: {sum: 0.0}
|
||||
)
|
||||
```
|
||||
|
||||
##### Compute the sum and count in a single reducer
|
||||
```js
|
||||
from(bucket:"example-bucket")
|
||||
|> filter(fn: (r) =>
|
||||
r._measurement == "cpu" and
|
||||
r._field == "usage_system" and
|
||||
r.service == "app-server"
|
||||
)
|
||||
|> range(start:-12h)
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
valueSum: r._value + accumulator.sum,
|
||||
valueCount: accumulator.count + 1.0
|
||||
}),
|
||||
identity: {sum: 0.0, count: 0.0}
|
||||
)
|
||||
```
|
||||
|
||||
##### Compute the product of all values
|
||||
```js
|
||||
from(bucket:"example-bucket")
|
||||
|> filter(fn: (r) =>
|
||||
r._measurement == "cpu" and
|
||||
r._field == "usage_system" and
|
||||
r.service == "app-server")
|
||||
|> range(start:-12h)
|
||||
|> reduce(
|
||||
fn: (r, accumulator) => ({
|
||||
valueProduct: r._value * accumulator.prod
|
||||
}),
|
||||
identity: {prod: 1.0}
|
||||
)
|
||||
```
|
Loading…
Reference in New Issue