Documentation

Flux query basics

Most Flux queries follow the same basic structure. Familiarize yourself with the basic concepts and steps to use when querying data with Flux.

Basic query structure

The majority of basic Flux queries include the following steps:

from(bucket: "example-bucket")              // ── Source
    |> range(start: -1d)                    // ── Filter on time
    |> filter(fn: (r) => r._field == "foo") // ── Filter on column values
    |> group(columns: ["sensorID"])         // ── Shape
    |> mean()                               // ── Process

Source

Flux input functions retrieve data from a data source. All input functions return a stream of tables.

Flux supports multiple data sources including, time series databases (such as InfluxDB and Prometheus), relational databases (such as MySQL and PostgreSQL), CSV, and more.

Filter

Filter functions iterate over and evaluate each input row to see if it matches specified conditions. Rows that meet the conditions are included in the function output. Rows that do not meet the specified conditions are dropped.

Flux provides the following primary filter functions:

  • range(): filter data based on time.
  • filter(): filter data based on column values. filter() uses a predicate function defined in the fn parameter to evaluate input rows. Each row is passed into the predicate function as a record, r, containing key-value pairs for each column in the row.

Other filter functions are also available. For more information, see Function types and categories – Filters.

Shape data

Many queries need to modify the structure of data to prepare it for processing. Common data-shaping tasks include regrouping data by column values or by time or pivoting column values into rows.

Functions that reshape data include the following:

  • group(): modify group keys
  • window(): modify _start and _stop values of rows to group data by time
  • pivot(): pivot column values into rows
  • drop(): drop specific columns
  • keep(): keep specific columns and drop all others

Process

Processing data can take on many forms, and includes the following types of operations:

  • Aggregate data: aggregate all rows of an input table into a single row. For information, see Function types and categories - Aggregates.
  • Select specific data points: return specific rows from each input table. For example, return the first or last row, the row with the highest or lowest value, and more. For information, see Function types and categories - Selectors.
  • Rewrite rows: use map() to rewrite each input row. Transform values with mathematic operations, process strings, dynamically add new columns, and more.
  • Send notifications: evaluate data and use Flux notification endpoint functions to send notifications to external services. For information, see Function types and categories- Notification endpoints.

aggregateWindow helper function

aggregateWindow() is a helper function that both shapes and processes data. The function windows and groups data by time, and then applies an aggregate or selector function to the restructured tables.


Write a basic query

Use InfluxDB sample data to write a basic Flux query that queries data, filters the data by time and column values, and then applies an aggregate.

Use the InfluxDB Data Explorer or the Flux REPL to build and execute the following basic query.

  1. Import the influxdata/influxdb/sample package and use the sample.data() function to load the airSensor sample dataset.

    import "influxdata/influxdb/sample"
    
    sample.data(set: "airSensor")
    

    sample.data() returns data as if it was queried from InfluxDB. To actually query data from InfluxDB, replace sample.data() with the from() function.

  2. Pipe the returned data forward into range() to filter the data by time. Return data from the last hour.

    import "influxdata/influxdb/sample"
    
    sample.data(set: "airSensor")
        |> range(start: -1h)
    
  3. Use filter() to filter rows based on column values. In this example, return only rows that include values for the co field. The field name is stored in the _field column.

    import "influxdata/influxdb/sample"
    
    sample.data(set: "airSensor")
        |> range(start: -1h)
        |> filter(fn: (r) => r._field == "co")
    
  4. Use mean() to calculate the average value in each input table. Because InfluxDB groups data by series, mean() returns a table for each unique sensor_id containing a single row with the average value in the _value column.

    import "influxdata/influxdb/sample"
    
    sample.data(set: "airSensor")
        |> range(start: -1h)
        |> filter(fn: (r) => r._field == "co")
        |> mean()
    
  5. Use group() to restructure tables into a single table:

    import "influxdata/influxdb/sample"
    
    sample.data(set: "airSensor")
        |> range(start: -1h)
        |> filter(fn: (r) => r._field == "co")
        |> mean()
        |> group()
    

Results from this basic query should be similar to the following:

_start and _stop columns have been omitted.

_field_measurementsensor_id_value
coairSensorsTLM01000.42338714381053716
coairSensorsTLM01010.4223251339463061
coairSensorsTLM01020.8543452859060252
coairSensorsTLM01030.2782783780205422
coairSensorsTLM02004.612143110484339
coairSensorsTLM02010.297474366047375
coairSensorsTLM02020.3336370208486757
coairSensorsTLM02030.4948166816959906

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered