There are multiple ways to execute Flux queries with InfluxDB and Chronograf v1.7+. This guide covers the different options:
- Chronograf’s Data Explorer
- Influx CLI in “Flux mode”
- Influx CLI via parameter
- Influx CLI via STDIN
- InfluxDB API
Before attempting these methods, make sure Flux is enabled by setting
flux-enabled = truein the
[http]section of your InfluxDB configuration file.
Chronograf’s Data Explorer
Chronograf v1.7+ supports Flux in its Data Explorer. Flux queries can be built, executed, and visualized from within the Chronograf user interface.
Influx CLI in “Flux mode”
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.
Any Flux query can be executed within the REPL.
Influx CLI via parameter
Flux queries can also be passed to the Flux REPL as a parameter using the
-type=flux option and the
The accompanying string is executed as a Flux query and results are output in your terminal.
influx -type=flux -execute '<flux query>'
Influx CLI via STDIN
Flux queries an be piped into the
influx CLI via STDIN.
Query results are output in your terminal.
echo '<flux query>' | influx -type=flux
Flux can be used to query InfluxDB through InfluxDB’s
Queried data is returned in annotated CSV format.
In your request, set the following:
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:
curl localhost:8086/api/v2/query -XPOST -sS \ -H 'accept:application/csv' \ -H 'content-type:application/vnd.flux' \ -d 'from(bucket:"telegraf") |> range(start:-5m) |> filter(fn:(r) => r._measurement == "cpu")'
curl localhost:8086/api/v2/query -XPOST -sS -H 'accept:application/csv' -H 'content-type:application/vnd.flux' -d 'from(bucket:"telegraf") |> range(start:-5m) |> filter(fn:(r) => r._measurement == "cpu")'