Query data with Flux

The following guides walk through both common and complex queries and use cases for Flux.

Example data variable

Many of the examples provided in the following guides use a data variable, which represents a basic query that filters data by measurement and field. data is defined as:

1. data = from(bucket: "example-bucket")
2. |> range(start: -1h)
3. |> filter(fn: (r) => r._measurement == "example-measurement" and r._field == "example-field")

Flux query guides


Query fields and tags

Use filter() to query data based on fields, tags, or any other column value. filter() performs operations similar to the SELECT statement and the WHERE clause in InfluxQL and other SQL-like query languages.

1. from(bucket: "example-bucket")
2. |> range(start: -1h)
3. |> filter(fn: (r) => r._measurement == "example-measurement" and r.tag == "example-tag")
4. |> filter(fn: (r) => r._field == "example-field")

Read more


Group

Use group() to group data with common values in specific columns.

1. data
2. |> group(columns: ["host"], mode: "by")
Input:
_time host _value
2020-01-01T00:01:00Z host1 1.0
2020-01-01T00:01:00Z host2 2.0
2020-01-01T00:02:00Z host1 1.0
2020-01-01T00:02:00Z host2 3.0
Output:
_time host _value
2020-01-01T00:01:00Z host1 1.0
2020-01-01T00:02:00Z host1 1.0
_time host _value
2020-01-01T00:01:00Z host2 2.0
2020-01-01T00:02:00Z host2 3.0

Read more


Sort and limit

Use sort() to order records within each table by specific columns and limit() to limit the number of records in output tables to a fixed number, n.

1. data
2. |> sort(columns: ["host", "_value"])
3. |> limit(n: 4)
Input:
_time host _value
2020-01-01T00:01:00Z A 1.0
2020-01-01T00:02:00Z B 1.2
2020-01-01T00:03:00Z A 1.8
2020-01-01T00:04:00Z B 0.9
2020-01-01T00:05:00Z B 1.4
2020-01-01T00:06:00Z B 2.0
Output:
_time host _value
2020-01-01T00:03:00Z A 1.8
2020-01-01T00:01:00Z A 1.0
2020-01-01T00:06:00Z B 2.0
2020-01-01T00:05:00Z B 1.4

Read more


Window & aggregate

This guide walks through windowing and aggregating data with Flux and outlines how it shapes your data in the process.

1. data
2. |> aggregateWindow(every: 20m, fn: mean)
Input:
_time _value
2020-01-01T00:00:00Z 250
2020-01-01T00:04:00Z 160
2020-01-01T00:12:00Z 150
2020-01-01T00:19:00Z 220
2020-01-01T00:32:00Z 200
2020-01-01T00:51:00Z 290
2020-01-01T01:00:00Z 340
Output:
_time _value
2020-01-01T00:20:00Z 195
2020-01-01T00:40:00Z 200
2020-01-01T01:00:00Z 290
2020-01-01T01:20:00Z 340

Read more


Explore your schema

Flux provides functions that let you explore the structure and schema of your data stored in InfluxDB.

1. import "influxdata/influxdb/schema"

3. // List buckets
4. buckets()

6. // List measurements
7. schema.measurements(bucket: "example-bucket")

9. // List field keys
10. schema.fieldKeys(bucket: "example-bucket")

12. // List tag keys
13. schema.tagKeys(bucket: "example-bucket")

15. // List tag values
16. schema.tagValues(bucket: "example-bucket", tag: "example-tag")

Read more


Transform data with math

Use map() to remap column values and apply mathematic operations.

1. data
2. |> map(fn: (r) => ({ r with _value: r._value * r._value }))
Input:
_time _value
2020-01-01T00:01:00Z 2
2020-01-01T00:02:00Z 4
2020-01-01T00:03:00Z 3
2020-01-01T00:04:00Z 5
Output:
_time _value
2020-01-01T00:01:00Z 4
2020-01-01T00:02:00Z 16
2020-01-01T00:03:00Z 9
2020-01-01T00:04:00Z 25

Read more


Calculate percentages

Use pivot() or join() and the map() function to align operand values into rows and calculate a percentage.

1. data
2. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
3. |> map(
4. fn: (r) => ({
5. _time: r._time,
6. _field: "used_percent",
7. _value: float(v: r.used) / float(v: r.total) * 100.0,
8. }),
9. )
Input:
_time _field _value
2020-01-01T00:00:00Z used 2.5
2020-01-01T00:00:10Z used 3.1
2020-01-01T00:00:20Z used 4.2
_time _field _value
2020-01-01T00:00:00Z total 8.0
2020-01-01T00:00:10Z total 8.0
2020-01-01T00:00:20Z total 8.0
Output:
_time _field _value
2020-01-01T00:00:00Z used_percent 31.25
2020-01-01T00:00:10Z used_percent 38.75
2020-01-01T00:00:20Z used_percent 52.50

Read more


Increase

Use increase() to track increases across multiple columns in a table. This function is especially useful when tracking changes in counter values that wrap over time or periodically reset.

1. data
2. |> increase()
Input:
_time _value
2020-01-01T00:01:00Z 1
2020-01-01T00:02:00Z 2
2020-01-01T00:03:00Z 8
2020-01-01T00:04:00Z 10
2020-01-01T00:05:00Z 0
2020-01-01T00:06:00Z 4
Output:
_time _value
2020-01-01T00:02:00Z 1
2020-01-01T00:03:00Z 7
2020-01-01T00:04:00Z 9
2020-01-01T00:05:00Z 9
2020-01-01T00:06:00Z 13

Read more


Moving Average

Use movingAverage() or timedMovingAverage() to return the moving average of data.

1. data
2. |> movingAverage(n: 3)
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.2
2020-01-01T00:03:00Z 1.8
2020-01-01T00:04:00Z 0.9
2020-01-01T00:05:00Z 1.4
2020-01-01T00:06:00Z 2.0
Output:
_time _value
2020-01-01T00:03:00Z 1.33
2020-01-01T00:04:00Z 1.30
2020-01-01T00:05:00Z 1.36
2020-01-01T00:06:00Z 1.43
1. data
2. |> timedMovingAverage(every: 2m, period: 4m)
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.2
2020-01-01T00:03:00Z 1.8
2020-01-01T00:04:00Z 0.9
2020-01-01T00:05:00Z 1.4
2020-01-01T00:06:00Z 2.0
Output:
_time _value
2020-01-01T00:02:00Z 1.000
2020-01-01T00:04:00Z 1.333
2020-01-01T00:06:00Z 1.325
2020-01-01T00:06:00Z 1.150

Read more


Rate

Use derivative() to calculate the rate of change between subsequent values or aggregate.rate() to calculate the average rate of change per window of time. If time between points varies, these functions normalize points to a common time interval making values easily comparable.

1. data
2. |> derivative(unit: 1m, nonNegative: true)
Input:
_time _value
2020-01-01T00:00:00Z 250
2020-01-01T00:04:00Z 160
2020-01-01T00:12:00Z 150
2020-01-01T00:19:00Z 220
2020-01-01T00:32:00Z 200
2020-01-01T00:51:00Z 290
2020-01-01T01:00:00Z 340
Output:
_time _value
2020-01-01T00:04:00Z
2020-01-01T00:12:00Z
2020-01-01T00:19:00Z 10.0
2020-01-01T00:32:00Z
2020-01-01T00:51:00Z 4.74
2020-01-01T01:00:00Z 5.56
1. import "experimental/aggregate"

3. data
4. |> aggregate.rate(every: 20m, unit: 1m)
Input:
_time _value
2020-01-01T00:00:00Z 250
2020-01-01T00:04:00Z 160
2020-01-01T00:12:00Z 150
2020-01-01T00:19:00Z 220
2020-01-01T00:32:00Z 200
2020-01-01T00:51:00Z 290
2020-01-01T01:00:00Z 340
Output:
_time _value
2020-01-01T00:20:00Z
2020-01-01T00:40:00Z 10.0
2020-01-01T01:00:00Z 4.74
2020-01-01T01:20:00Z 5.56

Read more


Histograms

Use histogram() to create cumulative histograms with Flux.

1. data
2. |> histogram(
3. column: "_value",
4. upperBoundColumn: "le",
5. countColumn: "_value",
6. bins: [100.0, 200.0, 300.0, 400.0],
7. )
Input:
_time _value
2020-01-01T00:00:00Z 250.0
2020-01-01T00:01:00Z 160.0
2020-01-01T00:02:00Z 150.0
2020-01-01T00:03:00Z 220.0
2020-01-01T00:04:00Z 200.0
2020-01-01T00:05:00Z 290.0
2020-01-01T01:00:00Z 340.0
Output:
le _value
100.0 0.0
200.0 3.0
300.0 6.0
400.0 7.0

Read more


Fill

Use fill() function to replace null values.

1. data
2. |> fill(usePrevious: true)
Input:
_time _value
2020-01-01T00:01:00Z null
2020-01-01T00:02:00Z 0.8
2020-01-01T00:03:00Z null
2020-01-01T00:04:00Z null
2020-01-01T00:05:00Z 1.4
Output:
_time _value
2020-01-01T00:01:00Z null
2020-01-01T00:02:00Z 0.8
2020-01-01T00:03:00Z 0.8
2020-01-01T00:04:00Z 0.8
2020-01-01T00:05:00Z 1.4
1. data
2. |> fill(value: 0.0)
Input:
_time _value
2020-01-01T00:01:00Z null
2020-01-01T00:02:00Z 0.8
2020-01-01T00:03:00Z null
2020-01-01T00:04:00Z null
2020-01-01T00:05:00Z 1.4
Output:
_time _value
2020-01-01T00:01:00Z 0.0
2020-01-01T00:02:00Z 0.8
2020-01-01T00:03:00Z 0.0
2020-01-01T00:04:00Z 0.0
2020-01-01T00:05:00Z 1.4

Read more


Median

Use median() to return a value representing the 0.5 quantile (50th percentile) or median of input data.

1. data
2. |> median()
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.0
2020-01-01T00:03:00Z 2.0
2020-01-01T00:04:00Z 3.0
Output:
_value
1.5

Read more


Percentile & quantile

Use the quantile() function to return all values within the q quantile or percentile of input data.

1. data
2. |> quantile(q: 0.99, method: "estimate_tdigest")
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.0
2020-01-01T00:03:00Z 2.0
2020-01-01T00:04:00Z 3.0
Output:
_value
3.0

Read more


Join

This guide walks through joining data with Flux and outlines how it shapes your data in the process.

1. import "join"
2. import "sql"

4. left =
5. from(bucket: "example-bucket-1")
6. |> range(start: "-1h")
7. |> filter(fn: (r) => r._measurement == "example-m")
8. |> filter(fn: (r) => r._field == "example-f")
9. |> drop(columns: ["_measurement", "_field"])

11. right =
12. sql.from(
13. driverName: "postgres",
14. dataSourceName: "postgresql://username:password@localhost:5432",
15. query: "SELECT * FROM example_table",
16. )

18. join.inner(
19. left: left |> group(),
20. right: right,
21. on: (l, r) => l.sensorID == r.ID,
22. as: (l, r) => ({l with expired: r.expired}),
23. )
24. |> group(columns: ["_time", "_value"], mode: "except")
Input:
left
_time sensorID _value
2020-01-01T00:01:00Z 1234 1
2020-01-01T00:02:00Z 1234 2
2020-01-01T00:03:00Z 1234 1
2020-01-01T00:04:00Z 1234 3
_time sensorID _value
2020-01-01T00:01:00Z 5678 2
2020-01-01T00:02:00Z 5678 5
2020-01-01T00:03:00Z 5678 1
2020-01-01T00:04:00Z 5678 8
ID expired serviced
1234 false 2022-01-01
5678 true 2022-01-01
Output:
_time sensorID _value expired
2020-01-01T00:01:00Z 1234 1 false
2020-01-01T00:02:00Z 1234 2 false
2020-01-01T00:03:00Z 1234 1 false
2020-01-01T00:04:00Z 1234 3 false
_time sensorID _value expired
2020-01-01T00:01:00Z 5678 2 true
2020-01-01T00:02:00Z 5678 5 true
2020-01-01T00:03:00Z 5678 1 true
2020-01-01T00:04:00Z 5678 8 true

Read more


Cumulative sum

Use the cumulativeSum() function to calculate a running total of values.

1. data
2. |> cumulativeSum()
Input:
_time _value
2020-01-01T00:01:00Z 1
2020-01-01T00:02:00Z 2
2020-01-01T00:03:00Z 1
2020-01-01T00:04:00Z 3
Output:
_time _value
2020-01-01T00:01:00Z 1
2020-01-01T00:02:00Z 3
2020-01-01T00:03:00Z 4
2020-01-01T00:04:00Z 7

Read more


First and last

Use first() or last() to return the first or last point in an input table.

1. data
2. |> first()
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.0
2020-01-01T00:03:00Z 2.0
2020-01-01T00:04:00Z 3.0
Output:
_time _value
2020-01-01T00:01:00Z 1.0
1. data
2. |> last()
Input:
_time _value
2020-01-01T00:01:00Z 1.0
2020-01-01T00:02:00Z 1.0
2020-01-01T00:03:00Z 2.0
2020-01-01T00:04:00Z 3.0
Output:
_time _value
2020-01-01T00:04:00Z 3.0

Read more


Exists

Use the Flux exists operator to check if a row record contains a column or if that column’s value is null.

Filter null values
1. data
2. |> filter(fn: (r) => exists r._value)

Read more


Custom functions

Create your own custom Flux functions to transform and operate on data.

1. multByX = (tables=<-, x) => tables
2. |> map(fn: (r) => ({r with _value: r._value * x}))

4. data
5. |> multByX(x: 2.0)

Read more


Extract scalar values

Use Flux dynamic query functions to extract scalar values from Flux query output. This lets you, for example, dynamically set variables using query results.

1. scalarValue = (tables=<-) => {
2. _record = tables
3. |> findRecord(fn: (key) => true, idx: 0)

5. return _record._value
6. }

Read more


Monitor states

Flux provides several functions to help monitor states and state changes in your data.


Operate on timestamps

Use Flux to process and operate on timestamps.


Query SQL data

The Flux sql package provides functions for working with SQL data sources. Use sql.from() to query SQL databases like PostgreSQL, MySQL, Snowflake, SQLite, Microsoft SQL Server, Amazon Athena, and Google BigQuery.

1. import "sql"

3. sql.from(
4. driverName: "postgres",
5. dataSourceName: "postgresql://user:password@localhost",
6. query: "SELECT * FROM example_table",
7. )

Read more


Conditional logic

This guide describes how to use Flux conditional expressions, such as if, else, and then, to query and transform data. Flux evaluates statements from left to right and stops evaluating once a condition matches.

1. if color == "green" then "008000" else "ffffff"

Read more


Regular expressions

This guide walks through using regular expressions in evaluation logic in Flux functions.

1. data
2. |> filter(fn: (r) => r.tag =~ /^foo[1-3]/)
Input:
_time tag _value
2020-01-01T00:01:00Z foo1 1.0
2020-01-01T00:02:00Z foo5 1.2
2020-01-01T00:03:00Z bar3 1.8
2020-01-01T00:04:00Z foo3 0.9
2020-01-01T00:05:00Z foo2 1.4
2020-01-01T00:06:00Z bar1 2.0
Output:
_time tag _value
2020-01-01T00:01:00Z foo1 1.0
2020-01-01T00:04:00Z foo3 0.9
2020-01-01T00:05:00Z foo2 1.4

Read more


Geo-temporal data

Use the Flux Geo package to filter geo-temporal data and group by geographic location or track.

1. import "experimental/geo"

3. sampleGeoData
4. |> geo.filterRows(region: {lat: 30.04, lon: 31.23, radius: 200.0})
5. |> geo.groupByArea(newColumn: "geoArea", level: 5)

Read more


Query the Flux version

Use runtime.version() to return the version of Flux installed in InfluxDB.

1. import "array"
2. import "runtime"

4. array.from(rows: [{version: runtime.version()}])

Read more


flux query