Back to Influxdb

keep() function

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

latest4.6 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#L1251-L1254 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

keep() returns a stream of tables containing only the specified columns.

Columns in the group key that are not specified in the columns parameter or identified by the fn parameter are removed from the group key and dropped from output tables. keep() is the inverse of drop().

Function type signature
js
(<-tables: stream[A], ?columns: [string], ?fn: (column: string) => bool) => stream[B] where A: Record, B: Record

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

Parameters

columns

Columns to keep in output tables. Cannot be used with fn.

fn

Predicate function that takes a column name as a parameter (column) and returns a boolean indicating whether or not the column should be kept in output tables. Cannot be used with columns.

tables

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

Examples

Keep a list of columns

js
import "sampledata"

sampledata.int()
    |> keep(columns: ["_time", "_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
2021-01-01T00:00:00Z-2
2021-01-01T00:00:10Z10
2021-01-01T00:00:20Z7
2021-01-01T00:00:30Z17
2021-01-01T00:00:40Z15
2021-01-01T00:00:50Z4
2021-01-01T00:00:00Z19
2021-01-01T00:00:10Z4
2021-01-01T00:00:20Z-3
2021-01-01T00:00:30Z19
2021-01-01T00:00:40Z13
2021-01-01T00:00:50Z1

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

Keep columns matching a predicate

js
import "sampledata"

sampledata.int()
    |> keep(fn: (column) => column =~ /^_?t/)

{{< 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*tag
2021-01-01T00:00:00Zt1
2021-01-01T00:00:10Zt1
2021-01-01T00:00:20Zt1
2021-01-01T00:00:30Zt1
2021-01-01T00:00:40Zt1
2021-01-01T00:00:50Zt1
_time*tag
2021-01-01T00:00:00Zt2
2021-01-01T00:00:10Zt2
2021-01-01T00:00:20Zt2
2021-01-01T00:00:30Zt2
2021-01-01T00:00:40Zt2
2021-01-01T00:00:50Zt2

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