Merge pull request #235 from influxdata/flux/stream-table-functions

Stream and table functions
pull/242/head
Scott Anderson 2019-05-15 11:02:22 -06:00 committed by GitHub
commit 0d5ccf9212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,35 @@
---
title: Flux stream and table functions
list_title: Stream and table functions
seotitle: Flux built-in stream and table functions
description: >
Use stream and table functions to extract a table from a stream of tables and access its
columns and records.
weight: 401
menu:
v2_0_ref:
name: Stream & table
parent: built-in-transformations
v2.0/tags: [transformations, built-in, functions, stream, table]
---
Use stream and table functions to extract a table from a stream of tables and access its
columns and records.
##### Example stream and table functions
```js
data = from(bucket:"example-bucket")
|> range(start: -5m)
|> filter(fn:(r) => r._measurement == "cpu")
// Extract the first available table for which "_field" is equal to "usage_idle"
t = data |> tableFind(fn: (key) => key._field == "usage_idle")
// Extract the "_value" column from the table
values = t |> getColumn(column: "_value")
// Extract the first record from the table
r0 = t |> getRecord(idx: 0)
```
{{< children type="functions" >}}

View File

@ -0,0 +1,39 @@
---
title: getColumn() function
description: >
The `getColumn()` function extracts a column from a table given its label.
If the label is not present in the set of columns, the function errors.
menu:
v2_0_ref:
name: getColumn
parent: Stream & table
weight: 501
---
The `getColumn()` function extracts a column from a table given its label.
If the label is not present in the set of columns, the function errors.
_**Function type:** Stream and table_
```js
getColumn(column: "_value")
```
## Parameters
### column
The name of the column to extract.
_**Data type:** String_
## Example
```js
vs = from(bucket:"example-bucket")
|> range(start: -5m)
|> filter(fn:(r) => r._measurement == "cpu")
|> tableFind(fn: (key) => key._field == "usage_idle")
|> getColumn(column: "_value")
// Use column values
x = vs[0] + vs[1]
```

View File

@ -0,0 +1,39 @@
---
title: getRecord() function
description: >
The `getRecord()` function extracts a record from a table given its index.
If the index is out of bounds, the function errors.
menu:
v2_0_ref:
name: getRecord
parent: Stream & table
weight: 501
---
The `getRecord()` function extracts a record from a table given the record's index.
If the index is out of bounds, the function errors.
_**Function type:** Stream and table_
```js
getRecord(idx: 0)
```
## Parameters
### idx
The index of the record to extract.
_**Data type:** Integer_
## Example
```js
r0 = from(bucket:"example-bucket")
|> range(start: -5m)
|> filter(fn:(r) => r._measurement == "cpu")
|> tableFind(fn: (key) => key._field == "usage_idle")
|> getRecord(idx: 0)
// Use record values
x = r0._field + "--" + r0._measurement
```

View File

@ -0,0 +1,50 @@
---
title: tableFind() function
description: >
The `tableFind()` function extracts the first table in a stream of tables whose
group key values match a predicate. If no table is found, the function errors.
menu:
v2_0_ref:
name: tableFind
parent: Stream & table
weight: 501
---
The `tableFind()` function extracts the first table in a stream of tables whose
group key values match a predicate. If no table is found, the function errors.
_**Function type:** Stream and table_
```js
tableFind(column: "_value")
```
## Parameters
### fn
A predicate function for matching keys in a table's group key.
`tableFind` returns the first table that resolves as `true`.
It expects a `key` argument which represents a group key in the input stream.
_**Data type:** Function_
##### Example fn function
```js
(key) => key._field == "fieldName"
```
## Example
```js
t = from(bucket:"example-bucket")
|> range(start: -5m)
|> filter(fn:(r) => r._measurement == "cpu")
|> tableFind(fn: (key) => key._field == "usage_idle")
// t represents the first table in a stream whose group key
// contains "_field" with a value of "usage_idle".
```
{{% note %}}
You can use `t` from the example above as input for [`getColumn()`](/v2.0/reference/flux/functions/built-in/transformations/stream-table/getcolumn/)
and [`getRecord()`](/v2.0/reference/flux/functions/built-in/transformations/stream-table/getrecord/).
{{% /note %}}