Show Menu
Cheatography

FluxLang 101 Cheat Sheet by

The basic functionalities of Flux Language

A Table of Measure

Measur­ement
Sensor
Temper­ature
Timestamp
iot-oven
S1
290
163395­360­000­0000000
iot-oven
S2
105
163395­361­500­0000000
iot-oven
S1
305
163395­366­000­0000000
iot-oven
S2
120
163395­367­500­0000000

Basic Filtering

from(bucket: "training")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "iot-oven")
  |> filter(fn: (r) => r._field == "temperature")
  |> filter(fn: (r) => r.sensor == "S2")
  |> filter(fn: (r) => r._value < 100)
The range clause allows to filter by time creating a time windodw
The filter clause reduces the ammount of records and can be applied on the measur­ements, fields, tags, and field/tag keys.

Functions

Flux transf­orm­ations take a stream of tables as input, transform the data in some way, and output a stream of tables.

Aggreg­ation

from(bucket: "training")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "iot-oven")
  |> group(columns: ["_field"])
  |> mean()
Flux aggregate functions are transf­orm­ations aggregate values from input tables in some way.

Selector

from(bucket: "training")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "iot-oven")
  |> filter(fn: (r) => r.sensor == "S1")
  |> group(columns: ["_field"])
  |> last()
Flux selector functions are transf­orm­ations that return one or more record per input table.

Condit­ional Expres­sions

... |> map(fn: (r) => ({
      r with _value:
                    if r._value == true then 1
                    else 0  }))
Condit­ional expres­sions evaluate a boolea­n-v­alued condition. If the result is true, the expression that follows the then keyword is evaluated and returned.
 

Line Protocol

Advanced Windowing

from(bucket: "training")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "iot-oven")
  |> filter(fn: (r) => r._field == "temperature")
  |> filter(fn: (r) => r.sensor == "S1")
  |> aggregateWindow(every: 2m, fn: mean)
aggreg­ate­Win­dow() downsa­mples data by grouping data into fixed windows of time and applying an aggregate or selector function to each window.

NOTE The flag
create­Empty: false
can be used to consider only the windows that contains data (its default value is
true
)

Map and Custom Functions

from(bucket: "training") ...  
        |> map(fn: (r) => ({ r with correctValue: r._value - 5.0 }))
Note the
r with
clause maintains alla the original columns and adds the new one.

Custom pipe forwar­dable function

adjValues = (tables=<-, x) =>
  tables
    |> map(fn: (r) => ({ r with correctValue: r._value + x}))

from(bucket: "training")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "iot-oven")
  |> adjValues(x:-5.0)
Most Flux functions manipulate data piped-­forward into the function. In order for a custom function to process piped-­forward data, one of the function parameters must capture the input tables using the
<-
pipe-r­eceive expres­sion.

Joins

from(bucket: "training")
...
join(tables: {s1: hs1, s2: hs2}, on: ["_time"], method: "inner")
The join() function merges two or more input streams, whose values are equal on a set of common columns, into a single output stream.
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          GoLang fmt Printing Cheat Sheet
          GoLang Cheat Sheet
          Golang Naming Conventions Cheat Sheet