Extract scalar values in Flux

To extract scalar values from output:

  1. 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 .

Current limitations

  • The InfluxDB user interface (UI) does not currently support raw scalar output. Use to add scalar values to output data.

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

Extract the correct table

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

See Sample data below.

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.

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

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

Use an extracted row record

Use a variable to store the extracted row record. In the example below, tempInfo represents the extracted row. Use 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

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

Extract a scalar field value
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.

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