Back to Influxdb

map() function

content/flux/v0/stdlib/universe/map.md

latest7.9 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L1749-L1749 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

map() iterates over and applies a function to input rows.

Each input row is passed to the fn as a record, r. Each r property represents a column key-value pair. Output values must be of the following supported column types:

  • float
  • integer
  • unsigned integer
  • string
  • boolean
  • time

Output data

Output tables are the result of applying the map function (fn) to each record of the input tables. Output records are assigned to new tables based on the group key of the input stream. If the output record contains a different value for a group key column, the record is regrouped into the appropriate table. If the output record drops a group key column, that column is removed from the group key.

Preserve columns

map() drops any columns that are not mapped explicitly by column label or implicitly using the with operator in the fn function. The with operator updates a record property if it already exists, creates a new record property if it doesn’t exist, and includes all existing properties in the output record.

no_run
data
    |> map(fn: (r) => ({ r with newColumn: r._value * 2 }))
Function type signature
js
(<-tables: stream[A], fn: (r: A) => B, ?mergeKey: bool) => stream[B]

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

fn

({{< req >}}) Single argument function to apply to each record. The return value must be a record.

mergeKey

(Deprecated) Merge group keys of mapped records. Default is false.

tables

Input data. Default is piped-forward data (<-).

Examples

Square the value in each row

js
import "sampledata"

sampledata.int()
    |> map(fn: (r) => ({r with _value: r._value * r._value}))

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

_time_value*tag
2021-01-01T00:00:00Z4t1
2021-01-01T00:00:10Z100t1
2021-01-01T00:00:20Z49t1
2021-01-01T00:00:30Z289t1
2021-01-01T00:00:40Z225t1
2021-01-01T00:00:50Z16t1
_time_value*tag
2021-01-01T00:00:00Z361t2
2021-01-01T00:00:10Z16t2
2021-01-01T00:00:20Z9t2
2021-01-01T00:00:30Z361t2
2021-01-01T00:00:40Z169t2
2021-01-01T00:00:50Z1t2

{{% /expand %}} {{< /expand-wrapper >}}

Create a new table with new columns

js
import "sampledata"

sampledata.int()
    |> map(
        fn: (r) => ({time: r._time, source: r.tag, alert: if r._value > 10 then true else false}),
    )

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

timesourcealert
2021-01-01T00:00:00Zt1false
2021-01-01T00:00:10Zt1false
2021-01-01T00:00:20Zt1false
2021-01-01T00:00:30Zt1true
2021-01-01T00:00:40Zt1true
2021-01-01T00:00:50Zt1false
2021-01-01T00:00:00Zt2true
2021-01-01T00:00:10Zt2false
2021-01-01T00:00:20Zt2false
2021-01-01T00:00:30Zt2true
2021-01-01T00:00:40Zt2true
2021-01-01T00:00:50Zt2false

{{% /expand %}} {{< /expand-wrapper >}}

Add new columns and preserve existing columns

Use the with operator on the r record to preserve columns not directly operated on by the map operation.

js
import "sampledata"

sampledata.int()
    |> map(fn: (r) => ({r with server: "server-${r.tag}", valueFloat: float(v: r._value)}))

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

_time_valueserver*tagvalueFloat
2021-01-01T00:00:00Z-2server-t1t1-2
2021-01-01T00:00:10Z10server-t1t110
2021-01-01T00:00:20Z7server-t1t17
2021-01-01T00:00:30Z17server-t1t117
2021-01-01T00:00:40Z15server-t1t115
2021-01-01T00:00:50Z4server-t1t14
_time_valueserver*tagvalueFloat
2021-01-01T00:00:00Z19server-t2t219
2021-01-01T00:00:10Z4server-t2t24
2021-01-01T00:00:20Z-3server-t2t2-3
2021-01-01T00:00:30Z19server-t2t219
2021-01-01T00:00:40Z13server-t2t213
2021-01-01T00:00:50Z1server-t2t21

{{% /expand %}} {{< /expand-wrapper >}}