2.1 KiB
title | description | aliases | menu | weight | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
map() function | The map() function applies a function to each record in the input tables. |
|
|
401 |
The map()
function applies a function to each record in the input tables.
The modified records are assigned to new tables based on the group key of the input table.
The output tables are the result of applying the map function to each record of the input tables.
When the output record contains a different value for the group key, the record is regrouped into the appropriate table. When the output record drops a column that was part of the group key, that column is removed from the group key.
Function type: Transformation
Output data type: Object
map(fn: (r) => r._value * r._value), mergeKey: true)
Parameters
fn
A single argument function that to apply to each record. The return value must be an object.
Data type: Function
{{% note %}}
Objects evaluated in fn
functions are represented by r
, short for "record" or "row".
{{% /note %}}
mergeKey
Indicates if the record returned from fn
should be merged with the group key.
When merging, all columns on the group key will be added to the record giving precedence to any columns already present on the record.
When not merging, only columns defined on the returned record will be present on the output records.
Defaults to true
.
Data type: Boolean
Examples
Square the value of each record
from(bucket:"telegraf/autogen")
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system" and
r.cpu == "cpu-total"
)
|> range(start:-12h)
|> map(fn: (r) => r._value * r._value)
Create a new table with new format
from(bucket:"telegraf/autogen")
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system"
)
|> range(start:-12h)
// create a new table by copying each row into a new format
|> map(fn: (r) => ({
_time: r._time,
app_server: r.host
}))