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 table.
  2. Extract a column from the table or extract a row from the table.

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.
  • The Flux REPL does not currently support Flux stream and table functions (also known as “dynamic queries”). See #15321.

Extract a table

Flux formats query results as a stream of tables. To extract a scalar value from a stream of tables, you must first extract a single table.

to extract a single table from the stream of tables.

If query results include only one table, it is still formatted as a stream of tables. You still must extract that table from the stream.

Use tableFind() to extract the first table whose group key values match the fn predicate function. The predicate function requires a key record, which represents the group key of each table.

  1. sampleData
  2. |> tableFind(fn: (key) =>
  3. key._field == "temp" and
  4. key.location == "sfo"
  5. )

The example above returns a single table:

_timelocation_field_value
2019-11-01T12:00:00Zsfotemp65.1
2019-11-01T13:00:00Zsfotemp66.2
2019-11-01T14:00:00Zsfotemp66.3
2019-11-01T15:00:00Zsfotemp66.8

Extract the correct table

Flux functions do not guarantee table order and tableFind() returns only the first table that matches the fn predicate. To extract the table that includes the data you actually want, be very specific in your predicate function or filter and transform your data to minimize the number of tables piped-forward into tableFind().

Extract a column from the table

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

  1. sampleData
  2. |> tableFind(fn: (key) =>
  3. key._field == "temp" and
  4. key.location == "sfo"
  5. )
  6. |> getColumn(column: "_value")
  7. // 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.

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

Extract a row from the table

Use the getRecord() 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. |> tableFind(fn: (key) =>
  3. key._field == "temp" and
  4. key.location == "sfo"
  5. )
  6. |> getRecord(idx: 0)
  7. // Returns {
  8. // _time:2019-11-11T12:00:00Z,
  9. // _field:"temp",
  10. // location:"sfo",
  11. // _value: 65.1
  12. // }

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