Merge pull request #478 from influxdata/guide/exists-operator

Added guide for using the 'exists' operator
pull/479/head
Scott Anderson 2019-09-19 15:13:14 -06:00 committed by GitHub
commit 3cee453564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 1 deletions

View File

@ -0,0 +1,69 @@
---
title: Check if a value exists
seotitle: Use Flux to check if a value exists
description: >
Use the Flux `exists` operator to check if an object contains a key or if that
key's value is `null`.
v2.0/tags: [exists]
menu:
v2_0:
name: Check if a value exists
parent: How-to guides
weight: 209
---
Use the Flux `exists` operator to check if an object contains a key or if that
key's value is `null`.
```js
p = {firstName: "John", lastName: "Doe", age: 42}
exists p.firstName
// Returns true
exists p.height
// Returns false
```
Use `exists` with row functions (
[`filter()`](/v2.0/reference/flux/stdlib/built-in/transformations/filter/),
[`map()`](/v2.0/reference/flux/stdlib/built-in/transformations/map/),
[`reduce()`](/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce/))
to check if a row includes a column or if the value for that column is `null`.
#### Filter out null values
```js
from(bucket: "example-bucket")
|> range(start: -5m)
|> filter(fn: (r) => exists r._value)
```
#### Map values based on existence
```js
from(bucket: "default")
|> range(start: -30s)
|> map(fn: (r) => ({
r with
human_readable:
if exists r._value then "${r._field} is ${string(v:r._value)}."
else "${r._field} has no value."
}))
```
#### Ignore null values in a custom aggregate function
```js
customSumProduct = (tables=<-) =>
tables
|> reduce(
identity: {sum: 0.0, product: 1.0},
fn: (r, accumulator) => ({
r with
sum:
if exists r._value then r._value + accumulator.sum
else accumulator.sum,
product:
if exists r._value then r.value * accumulator.product
else accumulator.product
})
)
```

View File

@ -10,6 +10,7 @@ menu:
name: reduce
parent: built-in-aggregates
weight: 501
v2.0/tags: [exists]
---
The `reduce()` function aggregates records in each table according to the reducer,
@ -96,7 +97,7 @@ creates a new column if it doesn't exist, and includes all existing columns in
the output table.
```js
recduce(fn: (r) => ({ r with newColumn: r._value * 2 }))
reduce(fn: (r) => ({ r with newColumn: r._value * 2 }))
```

View File

@ -9,6 +9,7 @@ menu:
name: filter
parent: built-in-transformations
weight: 401
v2.0/tags: [exists]
---
The `filter()` function filters data based on conditions defined in a predicate function ([`fn`](#fn)).
@ -42,6 +43,7 @@ Objects evaluated in `fn` functions are represented by `r`, short for "record" o
## Examples
##### Filter based on measurement, field, and tag
```js
from(bucket:"example-bucket")
|> range(start:-1h)
@ -52,6 +54,20 @@ from(bucket:"example-bucket")
)
```
##### Filter out null values
```js
from(bucket:"example-bucket")
|> range(start:-1h)
|> filter(fn: (r) => exists r._value )
```
##### Filter values based on thresholds
```js
from(bucket:"example-bucket")
|> range(start:-1h)
|> filter(fn: (r) => r._value > 50.0 and r._value < 65.0 )
```
<hr style="margin-top:4rem"/>
##### Related InfluxQL functions and statements:

View File

@ -9,6 +9,7 @@ menu:
name: map
parent: built-in-transformations
weight: 401
v2.0/tags: [exists]
---
The `map()` function applies a function to each record in the input tables.