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.
filter(
fn: (r) => r._measurement == "cpu",
onEmpty: "drop",
)
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.
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
.
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.
tables
Input data.
Default is piped-forward data (<-
).
Examples
- Filter based on InfluxDB measurement, field, and tag
- Keep empty tables when filtering
- Filter out null values
- Filter values based on thresholds
Filter based on InfluxDB measurement, field, and tag
from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total")
Keep empty tables when filtering
The following example uses data provided by the sampledata
package.
import "sampledata"
import "experimental/table"
sampledata.int()
|> filter(fn: (r) => r._value > 18, onEmpty: "keep")
Use table.fill()
to fill empty tables.
Filter out null values
The following example uses data provided by the sampledata
package.
import "sampledata"
sampledata.int(includeNull: true)
|> filter(fn: (r) => exists r._value )
Filter values based on thresholds
The following example uses data provided by the sampledata
package.
import "sampledata"
sampledata.int()
|> filter(fn: (r) => r._value > 0 and r._value < 10 )
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, the following resources are available:
InfluxDB Cloud customers can contact InfluxData Support.