Documentation

map() function

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.

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

Parameters

fn

(Required) 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

import "sampledata"

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

View example input and output

Create a new table with new columns

import "sampledata"

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

View example input and output

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.

import "sampledata"

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

View example input and output


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following: