Extract scalar values in Flux

Use Flux stream and table 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.

Current limitations

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

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, be very specific in your predicate function 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]

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

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. // }

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

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(
  5. fn: (key) => key._field == field,
  6. column: "_value"
  7. )
  8. return extract[0]
  9. }
  10. // Use the helper function to define a variable
  11. lastJFKTemp = sampleData
  12. |> filter(fn: (r) => r.location == "kjfk")
  13. |> last()
  14. |> getFieldValue(field: "temp")
  15. lastJFKTemp
  16. // Returns 71.2
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(
  5. fn: (key) => true,
  6. idx: idx
  7. )
  8. return extract
  9. }
  10. // Use the helper function to define a variable
  11. lastReported = sampleData
  12. |> last()
  13. |> getRow(idx: 0)
  14. "The last location to report was ${lastReported.location}.
  15. The temperature was ${string(v: lastReported._value)}°F."
  16. // Returns:
  17. // The last location to report was kord.
  18. // The temperature was 38.9°F.

Sample data

The following sample data set represents fictional temperature metrics collected from three locations. It’s formatted in annotated CSV and imported into the Flux query using the csv.from() function.

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

  1. import "csv"
  2. sampleData = csv.from(csv: "
  3. #datatype,string,long,dateTime:RFC3339,string,string,double
  4. #group,false,true,false,true,true,false
  5. #default,,,,,,
  6. ,result,table,_time,location,_field,_value
  7. ,,0,2019-11-01T12:00:00Z,sfo,temp,65.1
  8. ,,0,2019-11-01T13:00:00Z,sfo,temp,66.2
  9. ,,0,2019-11-01T14:00:00Z,sfo,temp,66.3
  10. ,,0,2019-11-01T15:00:00Z,sfo,temp,66.8
  11. ,,1,2019-11-01T12:00:00Z,kjfk,temp,69.4
  12. ,,1,2019-11-01T13:00:00Z,kjfk,temp,69.9
  13. ,,1,2019-11-01T14:00:00Z,kjfk,temp,71.0
  14. ,,1,2019-11-01T15:00:00Z,kjfk,temp,71.2
  15. ,,2,2019-11-01T12:00:00Z,kord,temp,46.4
  16. ,,2,2019-11-01T13:00:00Z,kord,temp,46.3
  17. ,,2,2019-11-01T14:00:00Z,kord,temp,42.7
  18. ,,2,2019-11-01T15:00:00Z,kord,temp,38.9
  19. ")

Related articles

scalar