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. A data source
  2. A time range
  3. Data filters

1. Define your data source

Flux’s from() function defines an InfluxDB data source. It requires a bucket parameter. The following examples use example-bucket as the bucket name.

  1. from(bucket:"example-bucket")

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 range() function, which specifies a time range for your query. It accepts two parameters: start and stop. Ranges can be relative using negative durations or absolute using timestamps.

Example relative time ranges
  1. // Relative time range with start only. Stop defaults to now.
  2. from(bucket:"example-bucket")
  3. |> range(start: -1h)
  4. // Relative time range with start and stop
  5. from(bucket:"example-bucket")
  6. |> range(start: -1h, stop: -10m)

Relative ranges are relative to “now.”

Example absolute time range
  1. from(bucket:"example-bucket")
  2. |> 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:

  1. from(bucket:"example-bucket")
  2. |> range(start: -15m)

3. Filter your data

Pass your ranged data into the filter() function to narrow results based on data attributes or columns. The filter() function has one parameter, fn, which expects an anonymous function with logic that filters data based on columns or attributes.

Flux’s anonymous function syntax is similar to Javascript’s. Records or rows are passed into the filter() function as a record (r). The anonymous function takes the record and evaluates it to see if it matches the defined filters. Use the and relational operator to chain multiple filters.

  1. // Pattern
  2. (r) => (r.recordProperty comparisonOperator comparisonExpression)
  3. // Example with single filter
  4. (r) => (r._measurement == "cpu")
  5. // Example with multiple filters
  6. (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:

  1. from(bucket:"example-bucket")
  2. |> range(start: -15m)
  3. |> filter(fn: (r) =>
  4. r._measurement == "cpu" and
  5. r._field == "usage_system" and
  6. r.cpu == "cpu-total"
  7. )

4. Yield your queried data

Flux’s yield() function outputs the filtered tables as the result of the query.

  1. from(bucket:"example-bucket")
  2. |> range(start: -15m)
  3. |> filter(fn: (r) =>
  4. r._measurement == "cpu" and
  5. r._field == "usage_system" and
  6. r.cpu == "cpu-total"
  7. )
  8. |> yield()

Flux automatically assumes a yield() function at the end of each script in order to output and visualize the data. Explicitly calling yield() is only necessary when including multiple queries in the same Flux query. Each set of returned data needs to be named using the yield() function.

Congratulations!

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.

Get started with Flux Transform your data

Related articles

query flux