docs-v2/content/flux/v0.x/stdlib/universe/keep.md

3.5 KiB

title description aliases menu weight flux/v0.x/tags introduced
keep() function The `keep()` function returns a table containing only the specified columns.
/influxdb/v2.0/reference/flux/functions/transformations/keep
/influxdb/v2.0/reference/flux/functions/built-in/transformations/keep/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/keep/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/keep/
flux_0_x_ref
name parent
keep universe
102
transformations
0.7.0

The keep() function returns a table containing only the specified columns, ignoring all others. Only columns in the group key that are also specified in the keep() function will be kept in the resulting group key. It is the inverse of drop.

keep(columns: ["col1", "col2"])

// OR

keep(fn: (column) => column =~ /inodes*/)

Parameters

{{% note %}} Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names. {{% /note %}}

columns

Columns that should be included in the resulting table. Cannot be used with fn.

fn

A predicate function which takes a column name as a parameter (column) and returns a boolean indicating whether or not the column should be included in the resulting table. Cannot be used with columns.

tables

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

Examples

{{% flux/sample-example-intro plural=true %}}

Keep a list of columns

import "sampledata"

sampledata.int()
    |> keep(columns: ["_time", "_value"])

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

Input data

{{% flux/sample "int" %}}

{{% /flex-content %}} {{% flex-content %}}

Output data
_time _value
2021-01-01T00:00:00Z -2
2021-01-01T00:00:10Z 10
2021-01-01T00:00:20Z 7
2021-01-01T00:00:30Z 17
2021-01-01T00:00:40Z 15
2021-01-01T00:00:50Z 4
2021-01-01T00:00:00Z 19
2021-01-01T00:00:10Z 4
2021-01-01T00:00:20Z -3
2021-01-01T00:00:30Z 19
2021-01-01T00:00:40Z 13
2021-01-01T00:00:50Z 1

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

Keep columns matching a predicate

import "sampledata"

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

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

Input data

{{% flux/sample "int" %}}

{{% /flex-content %}} {{% flex-content %}}

Output data
_time tag
2021-01-01T00:00:00Z t1
2021-01-01T00:00:10Z t1
2021-01-01T00:00:20Z t1
2021-01-01T00:00:30Z t1
2021-01-01T00:00:40Z t1
2021-01-01T00:00:50Z t1
_time tag
2021-01-01T00:00:00Z t2
2021-01-01T00:00:10Z t2
2021-01-01T00:00:20Z t2
2021-01-01T00:00:30Z t2
2021-01-01T00:00:40Z t2
2021-01-01T00:00:50Z t2

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