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

9.3 KiB
Raw Permalink Blame History

title description aliases menu weight flux/v0.x/tags related introduced
keyValues() function The `keyValues()` function returns a table with the input table's group key plus two columns, _key and _value, that correspond to unique column + value pairs from the input table.
/influxdb/v2.0/reference/flux/functions/transformations/keyvalues
/influxdb/v2.0/reference/flux/functions/built-in/transformations/keyvalues/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/keyvalues/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/keyvalues/
flux_0_x_ref
name parent
keyValues 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-values, InfluxQL  SHOW TAG VALUES
/{{< latest "influxdb" "v1" >}}/query_language/explore-schema/#show-serie, InfluxQL  SHOW SERIES
0.13.0

The keyValues() function returns a table with the input table's group key plus two columns, _key and _value, that correspond to unique column and value pairs for specific columns in each input table.

keyValues(keyColumns: ["usage_idle", "usage_user"])

Parameters

keyColumns

A list of columns from which values are extracted. All columns indicated must be of the same type. Each input table must have all of the columns listed by the keyColumns parameter.

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.

Get key values from explicitly defined columns

import "influxdata/influxdb/sample"

data = sample.data(set: "airSensor")
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

data
    |> keyValues(keyColumns: ["sensor_id", "_field"])

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

Example input data

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

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

Get key values from all group key columns

  1. Use keys() and findColumn() to extract an array of group key columns and store it as a variable.
  2. Use keyValues() and provide the group key column array variable to the keyColumns parameter.
import "influxdata/influxdb/sample"

data = sample.data(set: "airSensor")
    |> filter(fn: (r) => r.sensor_id == "TLM0100")

keyColumns = data
    |> keys()
    |> findColumn(fn: (key) => true, column: "_value")
// Returns [_field, _measurement, sensor_id]

data
    |> keyValues(keyColumns: keyColumns)

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

Example input data

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

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