filter() function

The filter() function filters data based on conditions defined in a predicate function (fn). The output tables have the same schema as the corresponding input tables.

*Function type: Transformation
**
Output data type:* Record

  1. filter(
  2. fn: (r) => r._measurement == "cpu",
  3. onEmpty: "drop"
  4. )

Parameters

Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names.

fn

A single argument predicate function that evaluates true or false. Records are passed to the function. Those that evaluate to true are included in the output tables. Records that evaluate to null or false are not included in the output tables.

*Data type: Function*

Records evaluated in fn functions are represented by r, short for “record” or “row”.

onEmpty

Defines the behavior for empty tables. Potential values are keep and drop. Defaults to drop.

*Data type: String*

drop

Tables without rows are dropped.

keep

Tables without rows are output to the next transformation.

Keeping empty tables with your first filter() function can have severe performance costs since it retains empty tables from your entire data set. For higher performance, use your first filter() function to do basic filtering, then keep empty tables on subsequent filter() calls with smaller data sets. See the example below.

Examples

Filter based on measurement, field, and tag
  1. from(bucket:"example-bucket")
  2. |> range(start:-1h)
  3. |> filter(fn: (r) =>
  4. r._measurement == "cpu" and
  5. r._field == "usage_system" and
  6. r.cpu == "cpu-total"
  7. )
Filter out null values
  1. from(bucket:"example-bucket")
  2. |> range(start:-1h)
  3. |> filter(fn: (r) => exists r._value )
Filter values based on thresholds
  1. from(bucket:"example-bucket")
  2. |> range(start:-1h)
  3. |> filter(fn: (r) => r._value > 50.0 and r._value < 65.0 )
Keep empty tables when filtering
  1. from(bucket: "example-bucket")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) => r._measurement == "events" and r._field == "open")
  4. |> filter(fn: (r) => r.doorId =~ /^2.*/, onEmpty: "keep")

Related articles

exists