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

11 KiB
Raw Permalink Blame History

title description aliases menu weight flux/v0.x/tags related introduced
keys() function The `keys()` function outputs the group key of input tables. For each input table, it outputs a table with the same group key columns, plus a _value column containing the labels of the input table's group key.
/influxdb/v2.0/reference/flux/functions/transformations/keys
/influxdb/v2.0/reference/flux/functions/built-in/transformations/keys/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/keys/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/keys/
flux_0_x_ref
name parent
keys universe
102
transformations
/{{< latest "influxdb" "v1" >}}/query_language/explore-schema/#show-measurements, InfluxQL SHOW MEASUREMENTS
/{{< latest "influxdb" "v1" >}}/query_language/explore-schema/#show-field-keys, InfluxQL SHOW FIELD KEYS
/{{< latest "influxdb" "v1" >}}/query_language/explore-schema/#show-tag-keys, InfluxQL SHOW TAG KEYS
/{{< latest "influxdb" "v1" >}}/query_language/explore-schema/#show-tag-keys, InfluxQL SHOW SERIES
0.13.0

The keys() function outputs the column keys in the group key of each input table. For each input table, the function outputs a table with the same group key columns, and a _value column containing the labels of the input table's group key. Each row in an output table contains the group key value and the label of one column in the group key of the input table. Each output table has the same number of rows as the size of the group key of the input table.

keys(column: "_value")

Parameters

column

Name of the output column in to store group key labels in. Default is "_value"`.

tables

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

Examples

The following examples use sample.data() to simulate data queried from InfluxDB and illustrate how keys() transforms data.

Return group key columns for each input table

import "influxdata/influxdb/sample"

data = sample.data(set: "airSensor")
    |> range(start: -30m)
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

data
    |> keys()

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

Example input data

{{< flux/group-key "[_start, _stop, _field, _measurement, sensor_id]" >}}

_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:21:57Z 0.31069912185103726
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:07Z 0.2958765656451926
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:17Z 0.3148598993377045
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:27Z 0.3138373097388317
_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:21:57Z 36.032121180773785
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:07Z 36.078174038253856
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:17Z 36.10019403559529
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:27Z 36.12069055726357
_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:21:57Z 70.84122391403946
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:07Z 70.86036165985708
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:17Z 70.89253177998165
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:27Z 70.85193833073798
Example output data
_start _stop _field _measurement sensor_id _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 _start
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 _stop
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 _field
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 _measurement
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 sensor_id
_start _stop _field _measurement sensor_id _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 _start
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 _stop
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 _field
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 _measurement
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 sensor_id
_start _stop _field _measurement sensor_id _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 _start
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 _stop
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 _field
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 _measurement
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 sensor_id
{{% /expand %}}
{{< /expand-wrapper >}}

Return all distinct group key columns in a single table

import "influxdata/influxdb/sample"

data = sample.data(set: "airSensor")
    |> range(start: -30m)
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

data
    |> keys()
    |> keep(columns: ["_value"])
    |> distinct()

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

Example input data

{{< flux/group-key "[_start, _stop, _field, _measurement, sensor_id]" >}}

_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:21:57Z 0.31069912185103726
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:07Z 0.2958765656451926
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:17Z 0.3148598993377045
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z co airSensors TLM0100 2021-09-08T14:22:27Z 0.3138373097388317
_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:21:57Z 36.032121180773785
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:07Z 36.078174038253856
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:17Z 36.10019403559529
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z humidity airSensors TLM0100 2021-09-08T14:22:27Z 36.12069055726357
_start _stop _field _measurement sensor_id _time _value
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:21:57Z 70.84122391403946
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:07Z 70.86036165985708
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:17Z 70.89253177998165
2021-09-08T14:21:53Z 2021-09-08T14:51:53Z temperature airSensors TLM0100 2021-09-08T14:22:27Z 70.85193833073798
Example output data
_value
_start
_stop
_field
_measurement
sensor_id

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

Return group key columns as an array

To return group key columns as an array:

  1. Use keys() to replace the _value column with the group key labels.
  2. Use findColumn() to return the _value column as an array.
import "influxdata/influxdb/sample"

data = sample.data(set: "airSensor")
    |> range(start: -30m)
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

data
    |> keys()
    |> findColumn(fn: (key) => true, column: "_value")

// Returns [_start, _stop, _field, _measurement, sensor_id]