Query InfluxDB with Flux
This guide walks through the basics of using Flux to query data from InfluxDB. Every Flux query needs the following:
1. Define your data source
2. Specify a time range
Flux requires a time range when querying time series data. “Unbounded” queries are very resource-intensive and as a protective measure, Flux will not query the database without a specified range.
Use the pipe-forward operator (
|>) to pipe data from your data source into the
function, which specifies a time range for your query.
It accepts two parameters:
Ranges can be relative using negative durations
or absolute using timestamps.
Example relative time ranges
// Relative time range with start only. Stop defaults to now. from(bucket:"example-bucket") |> range(start: -1h) // Relative time range with start and stop from(bucket:"example-bucket") |> range(start: -1h, stop: -10m)
Relative ranges are relative to “now.”
Example absolute time range
from(bucket:"example-bucket") |> range(start: 2018-11-05T23:30:00Z, stop: 2018-11-06T00:00:00Z)
Use the following:
For this guide, use the relative time range,
-15m, to limit query results to data from the last 15 minutes:
from(bucket:"example-bucket") |> range(start: -15m)
3. Filter your data
Pass your ranged data into the
filter() function to narrow results based on data attributes or columns.
filter() function has one parameter,
fn, which expects an anonymous function
with logic that filters data based on columns or attributes.
Records or rows are passed into the
filter() function as an object (
The anonymous function takes the object and evaluates it to see if it matches the defined filters.
and relational operator to chain multiple filters.
// Pattern (r) => (r.objectProperty comparisonOperator comparisonExpression) // Example with single filter (r) => (r._measurement == "cpu") // Example with multiple filters (r) => (r._measurement == "cpu") and (r._field != "usage_system" )
Use the following:
For this example, filter by the
cpu measurement, the
usage_system field, and the
cpu-total tag value:
from(bucket:"example-bucket") |> range(start: -15m) |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total" )
4. Yield your queried data
yield() function outputs the filtered tables as the result of the query.
from(bucket:"example-bucket") |> range(start: -15m) |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total" ) |> yield()
Flux automatically assumes a
yield() function at
the end of each script in order to output and visualize the data.
yield() is only necessary when including multiple queries in the same Flux query.
Each set of returned data needs to be named using the
You have now queried data from InfluxDB using Flux.
The query shown here is a barebones example. Flux queries can be extended in many ways to form powerful scripts.
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support or submit feedback, the following resources are available:
- Post in the InfluxData Community.
- Submit documentation issues to the InfluxDB 2.0 documentation repository.
- Submit InfluxDB issues to the InfluxDB repository.
For our InfluxDB Cloud customers, InfluxData Support is also available.