Execute Flux queries

There are multiple ways to execute Flux queries with InfluxDB and Chronograf v1.8+. This guide covers the different options:

  1. Chronograf’s Data Explorer
  2. Influx CLI
  3. InfluxDB API

Before attempting these methods, make sure Flux is enabled by setting flux-enabled = true in the [http] section of your InfluxDB configuration file.

Chronograf’s Data Explorer

Chronograf v1.8+ supports Flux in its Data Explorer. Flux queries can be built, executed, and visualized from within the Chronograf user interface.

Influx CLI

InfluxDB v1.8+’s influx CLI includes a -type option which allows you specify what type of interactive session to start. -type=flux will start an interactive read-eval-print-loop (REPL) that supports Flux.

If authentication is enabled on your InfluxDB instance, use the -username flag to provide your InfluxDB username and the -password flag to provide your password.

Enter an interactive Flux REPL

No Auth Auth Enabled

  1. influx -type=flux
  1. influx -type=flux -username myuser -password PasSw0rd

Any Flux query can be executed within the REPL.

Submit a Flux query via parameter

Flux queries can also be passed to the Flux REPL as a parameter using the influx CLI’s -type=flux option and the -execute parameter. The accompanying string is executed as a Flux query and results are output in your terminal.

No Auth Auth Enabled

  1. influx -type=flux -execute '<flux query>'
  1. influx -type=flux -username myuser -password PasSw0rd -execute '<flux query>'

Submit a Flux query via via STDIN

Flux queries an be piped into the influx CLI via STDIN. Query results are otuput in your terminal.

No Auth Auth Enabled

  1. echo '<flux query>' | influx -type=flux
  1. echo '<flux query>' | influx -type=flux -username myuser -password PasSw0rd

InfluxDB API

Flux can be used to query InfluxDB through InfluxDB’s /api/v2/query endpoint. Queried data is returned in annotated CSV format.

In your request, set the following:

  • Accept header to application/csv
  • Content-type header to application/vnd.flux
  • If authentication is enabled on your InfluxDB instance, Authorization header to Token <username>:<password>

This allows you to POST the Flux query in plain text and receive the annotated CSV response.

Below is an example curl command that queries InfluxDB using Flux:

No Auth Auth Enabled

  1. curl -XPOST localhost:8086/api/v2/query -sS \
  2. -H 'Accept:application/csv' \
  3. -H 'Content-type:application/vnd.flux' \
  4. -d 'from(bucket:"telegraf")
  5. |> range(start:-5m)
  6. |> filter(fn:(r) => r._measurement == "cpu")'
  1. curl -XPOST localhost:8086/api/v2/query -sS \
  2. -H 'Accept:application/csv' \
  3. -H 'Content-type:application/vnd.flux' \
  4. -H 'Authorization: Token <username>:<password>' \
  5. -d 'from(bucket:"telegraf")
  6. |> range(start:-5m)
  7. |> filter(fn:(r) => r._measurement == "cpu")'