Extract scalar values in Flux
To extract scalar values from output:
- or extract a row from the input stream.
- 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.
SFOTemps = sampleData
|> findColumn(
fn: (key) => key._field == "temp" and key.location == "sfo",
column: "_value",
)
SFOTemps
SFOTemps[0]
// Returns 65.1
SFOTemps[2]
// 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.
tempInfo = sampleData
|> findRecord(
fn: (key) => key._field == "temp" and key.location == "sfo",
idx: 0,
)
tempInfo
// Returns {
// _time:2019-11-11T12:00:00Z,
// _field:"temp",
// location:"sfo",
// _value: 65.1
// }
tempInfo._time
// Returns 2019-11-11T12:00:00Z
tempInfo.location
Create custom helper functions to extract scalar values from query output.
Extract a scalar field value
Extract scalar row data
// Define a helper function to extract a row as a record
getRow = (tables=<-, field, idx=0) => {
extract = tables
|> findRecord(fn: (key) => true, idx: idx)
return extract
}
// Use the helper function to define a variable
lastReported = sampleData
|> last()
|> getRow(field: "temp")
"The last location to report was ${lastReported.location}.
The temperature was ${string(v: lastReported._value)}°F."
// Returns:
// The last location to report was kord.
// The temperature was 38.9°F.
Place the following at the beginning of your query to use the sample data: