Extract scalar values in Flux

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

To extract scalar values from output:

  1. Extract a column from the input stream or extract a row from the input stream.
  2. Use the returned array or record to reference scalar values.

The samples on this page use the sample data provided below.

Output scalar values

The InfluxDB /api/v2/query HTTP API endpoint and all clients that use it (InfluxDB UI, influx CLI, etc.) only support queries that return a stream of tables. This endpoint does not support raw scalar output.

To output a scalar value as part of a stream of tables:

  1. Import the array package.
  2. Use array.from() and display() to wrap the literal representation of a scalar value in a stream of tables and return it as output.

View example

  1. import "array"
  2. SFOTemps =
  3. sampleData
  4. |> findColumn(
  5. fn: (key) => key._field == "temp" and key.location == "sfo",
  6. column: "_value",
  7. )
  8. array.from(rows: [{ output: display(v: SFOTemps) }])
Returns
output
[ 65.1, 66.2, 66.3, 66.8 ]

Table extraction

Flux formats query results as a stream of tables. Both findColumn() and findRecord() extract the first table in a stream of tables whose group key values match the fn predicate function.

Extract the correct table

Flux functions do not guarantee table order. findColumn() and findRecord() extract only the first table that matches the fn predicate. To extract the correct table, use the fn predicate function to specifically identify the table to extract or filter and transform your data to minimize the number of tables piped-forward into the functions.

Extract a column

Use the findColumn() function to output an array of values from a specific column in the extracted table.

See Sample data below.

  1. sampleData
  2. |> findColumn(
  3. fn: (key) => key._field == "temp" and key.location == "sfo",
  4. column: "_value",
  5. )
  6. // Returns [65.1, 66.2, 66.3, 66.8]

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.

Use extracted column values

Use a variable to store the array of values. In the example below, SFOTemps represents the array of values. Reference a specific index (integer starting from 0) in the array to return the value at that index.

See Sample data below.

  1. SFOTemps = sampleData
  2. |> findColumn(
  3. fn: (key) => key._field == "temp" and key.location == "sfo",
  4. column: "_value",
  5. )
  6. SFOTemps
  7. // Returns [65.1, 66.2, 66.3, 66.8]
  8. SFOTemps[0]
  9. // Returns 65.1
  10. SFOTemps[2]
  11. // Returns 66.3

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.

Extract a row

Use the findRecord() function to output data from a single row in the extracted table. Specify the index of the row to output using the idx parameter. The function outputs a record with key-value pairs for each column.

  1. sampleData
  2. |> findRecord(
  3. fn: (key) => key._field == "temp" and key.location == "sfo",
  4. idx: 0,
  5. )
  6. // Returns {
  7. // _time:2019-11-11T12:00:00Z,
  8. // _field:"temp",
  9. // location:"sfo",
  10. // _value: 65.1
  11. // }

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.

Use an extracted row record

Use a variable to store the extracted row record. In the example below, tempInfo represents the extracted row. Use dot or bracket notation to reference keys in the record.

  1. tempInfo = sampleData
  2. |> findRecord(
  3. fn: (key) => key._field == "temp" and key.location == "sfo",
  4. idx: 0,
  5. )
  6. tempInfo
  7. // Returns {
  8. // _time:2019-11-11T12:00:00Z,
  9. // _field:"temp",
  10. // location:"sfo",
  11. // _value: 65.1
  12. // }
  13. tempInfo._time
  14. // Returns 2019-11-11T12:00:00Z
  15. tempInfo.location
  16. // Returns sfo

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.

Example helper functions

Create custom helper functions to extract scalar values from query output.

Extract a scalar field value
  1. // Define a helper function to extract field values
  2. getFieldValue = (tables=<-, field) => {
  3. extract = tables
  4. |> findColumn(fn: (key) => key._field == field, column: "_value")
  5. return extract[0]
  6. }
  7. // Use the helper function to define a variable
  8. lastJFKTemp = sampleData
  9. |> filter(fn: (r) => r.location == "kjfk")
  10. |> last()
  11. |> getFieldValue(field: "temp")
  12. lastJFKTemp
  13. // Returns 71.2

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.

Extract scalar row data
  1. // Define a helper function to extract a row as a record
  2. getRow = (tables=<-, field, idx=0) => {
  3. extract = tables
  4. |> findRecord(fn: (key) => true, idx: idx)
  5. return extract
  6. }
  7. // Use the helper function to define a variable
  8. lastReported = sampleData
  9. |> last()
  10. |> getRow(field: "temp")
  11. "The last location to report was ${lastReported.location}.
  12. The temperature was ${string(v: lastReported._value)}°F."
  13. // Returns:
  14. // The last location to report was kord.
  15. // The temperature was 38.9°F.

To return this value from the InfluxDB query API, InfluxDB UI, or influx CLI see Output scalar values.


Sample data

The following sample data set represents fictional temperature metrics collected from three locations. It’s formatted as an array of Flux records and structured as a stream of stables using array.from() function.

Place the following at the beginning of your query to use the sample data:

  1. import "array"
  2. sampleData =
  3. array.from(
  4. rows: [
  5. {_time: 2019-11-01T12:00:00Z, location: "sfo", _field: "temp", _value: 65.1},
  6. {_time: 2019-11-01T13:00:00Z, location: "sfo", _field: "temp", _value: 66.2},
  7. {_time: 2019-11-01T14:00:00Z, location: "sfo", _field: "temp", _value: 66.3},
  8. {_time: 2019-11-01T15:00:00Z, location: "sfo", _field: "temp", _value: 66.8},
  9. {_time: 2019-11-01T12:00:00Z, location: "kjfk", _field: "temp", _value: 69.4},
  10. {_time: 2019-11-01T13:00:00Z, location: "kjfk", _field: "temp", _value: 69.9},
  11. {_time: 2019-11-01T14:00:00Z, location: "kjfk", _field: "temp", _value: 71.0},
  12. {_time: 2019-11-01T15:00:00Z, location: "kjfk", _field: "temp", _value: 71.2},
  13. {_time: 2019-11-01T12:00:00Z, location: "kord", _field: "temp", _value: 46.4},
  14. {_time: 2019-11-01T13:00:00Z, location: "kord", _field: "temp", _value: 46.3},
  15. {_time: 2019-11-01T14:00:00Z, location: "kord", _field: "temp", _value: 42.7},
  16. {_time: 2019-11-01T15:00:00Z, location: "kord", _field: "temp", _value: 38.9},
  17. ],
  18. )
  19. |> group(columns: ["location", "_field"])

scalar