Merge pull request #1657 from influxdata/add-flux-examples

Add flux examples
pull/1713/head
noramullen1 2020-10-21 14:03:43 -07:00 committed by GitHub
commit 1062486607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 322 additions and 5 deletions

View File

@ -15,8 +15,3 @@ weight: 104
The following articles walk through common task use cases.
{{< children >}}
{{% note %}}
This list will continue to grow.
If you have suggestions, please [submit them to the InfluxData Community](https://community.influxdata.com/c/influxdb2).
{{% /note %}}

View File

@ -0,0 +1,49 @@
---
title: Calculate a weekly mean
description: >
Calculate a weekly mean and add it to a new bucket.
menu:
influxdb_2_0:
name: Calculate a weekly mean
parent: Common tasks
weight: 202
influxdb/v2.0/tags: [tasks]
---
{{% note %}}
This example uses [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
This example calculates a temperature weekly mean and stores it in a separate bucket.
The following query:
- Uses [`filter()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/filter/) to filter the `average_temperature` measurement.
- Uses [`range()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/range/) to define a time range.
- Uses [`aggregateWindow()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/aggregatewindow/) to group average temperature by week and compute the mean.
- Sends the weekly mean to a new bucket (`weekly_means`)
```js
option task = {
name: "weekly-means",
every: 1w,
}
from(bucket: "noaa")
|> filter(fn: (r) => r._measurement == "average_temperature")
|> range(start: 2019-09-01T11:24:00Z)
|> aggregateWindow(every: 1w, fn: mean)
|> to(bucket: "weekly_means")
```
### Example results
| _start | _stop | _field | _measurement | location | _value | _time |
|:------ |:----- |:------ |:------------ |:-------- | ------: |:----- |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | coyote_creek | 80.31005917159763 | 2019-09-05T00:00:00Z |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | coyote_creek | 79.8422619047619 | 2019-09-12T00:00:00Z |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | coyote_creek | 79.82710622710623 | 2019-09-19T00:00:00Z |
| _start | _stop | _field | _measurement | location | _value | _time |
|:------ |:----- |:------ |:------------ |:-------- | ------: |:----- |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | santa_monica | 80.19952494061758 | 2019-09-05T00:00:00Z |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | santa_monica | 80.01964285714286 | 2019-09-12T00:00:00Z |
| 2019-09-01T11:24:00Z | 2020-10-19T20:39:49Z | degrees | average_temperature | santa_monica | 80.20451

View File

@ -0,0 +1,43 @@
---
title: Convert results to JSON
seotitle: Convert results to JSON and send them to a URL
description: >
Use `json.encode()` to convert query results to JSON and `http.post()` to send them
to a URL endpoint.
menu:
influxdb_2_0:
name: Convert results to JSON
parent: Common tasks
weight: 203
influxdb/v2.0/tags: [tasks]
---
{{% note %}}
This example uses [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
Send each record to a URL endpoint using the HTTP POST method. This example uses [`json.encode()`](/influxdb/v2.0/reference/flux/stdlib/json/encode/) to convert a value into JSON bytes, then uses [`http.post()`](/influxdb/v2.0/reference/flux/stdlib/http/post/) to send them to a URL endpoint.
The following query:
- Uses [`filter()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/filter/) to filter the `average_temperature` measurement.
- Uses [`mean()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/mean/) to calculate the average value from results.
- Uses [`map()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/) to create a new column, `jsonStr`, and build a JSON object using column values from the query. It then byte-encodes the JSON object and stores it as a string in the `jsonStr` column.
- Uses [`http.post()`](/influxdb/v2.0/reference/flux/stdlib/http/post/) to send the `jsonStr` value from each record to an HTTP endpoint.
```js
import "http"
import "json"
from(bucket: "noaa")
|> filter(fn: (r) => r._measurement == "average_temperature")
|> mean()
|> map(fn: (r) => ({ r with
jsonStr: string(v: json.encode(v: {"location":r.location,"mean":r._value}))}))
|> map(fn: (r) => ({r with
status_code: http.post(
url: "http://somehost.com/",
headers: {x:"a", y:"b"},
data: bytes(v: r.jsonStr)
)
}))
```

View File

@ -0,0 +1,21 @@
---
title: Common queries
seotitle: Common queries with Flux
description: >
This collection of articles walks through common use cases for Flux queries.
influxdb/v2.0/tags: [queries]
menu:
influxdb_2_0:
name: Common queries
parent: Query data
weight: 104
---
The following articles walk through common queries using the [NOAA water database data](https://influx-testdata.s3.amazonaws.com/noaa.csv).
{{< children >}}
{{% note %}}
This list will continue to grow.
If you have suggestions, please [submit them to the InfluxData Community](https://community.influxdata.com/c/influxdb2).
{{% /note %}}

View File

@ -0,0 +1,46 @@
---
title: Use values to calculate a new column
description: >
Use the `map()` function to create a new column calculated from existing values in each row.
influxdb/v2.0/tags: [queries]
menu:
influxdb_2_0:
name: Calculate a new column
parent: Common queries
weight: 104
---
{{% note %}}
This example uses [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
This example converts temperature from Fahrenheit to Celsius and maps the Celsius value to a new `celsius` column.
The following query:
- Uses [`filter()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/filter/) to filter the `average_temperature` measurement.
- Uses [`map()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/) to create a new column calculated from existing values in each row.
```js
from(bucket: "noaa")
|> filter(fn: (r) => r._measurement == "average_temperature")
|> map(fn: (r) => ({r with
celsius: ((r._value - 32.0) * 5.0 / 9.0)
})
)
```
### Example results
| _start | _stop | _field | _measurement | location | _time | _value | celsius |
|:------ |:----- |:------: |:------------: |:--------: |:----- | ------:| -------:|
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:00:00Z | 82 | 27.78 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:06:00Z | 73 | 22.78 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:12:00Z | 86 | 30.00 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:18:00Z | 89 | 31.67 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:24:00Z | 77 | 25.00 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:30:00Z | 70 | 21.11 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:36:00Z | 84 | 28.89 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:42:00Z | 76 | 24.44 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:48:00Z | 85 | 29.44 |
| 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | degrees | average_temperature | coyote_creek | 2019-08-17T00:54:00Z | 80 | 26.67 |
| ••• | ••• | ••• | ••• | ••• | ••• | ••• | ••• |

View File

@ -0,0 +1,47 @@
---
title: Compare the last measurement to another bucket's mean
seotitle: Compare the last measurement to a mean stored in another bucket
description: >
Compare the value from the latest point to an average value stored in another bucket. This is useful when using the average value to calculate a threshold check.
influxdb/v2.0/tags: [queries]
menu:
influxdb_2_0:
name: Compare values from different buckets
parent: Common queries
weight: 104
---
{{% note %}}
This example uses [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
This example compares the value from the latest point to an average value stored in another bucket. This is useful when using the average value to calculate a [threshold check](/influxdb/v2.0/monitor-alert/checks/create/#threshold-check).
The following query:
- Uses [`range()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/range/) to define a time range.
- Gets the last value in the `means` bucket and compares it to the last value in the `noaa` bucket using [`last()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/selectors/last/).
- Uses [`join()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/join/) to combine the results
- Uses [`map()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/) to calculate the differences
```js
means = from(bucket: "weekly_means")
|> range(start: 2019-09-01T00:00:00Z)
|> last()
|> keep(columns: ["_value", "location"])
latest = from(bucket: "noaa")
|> range(start: 2019-09-01T00:00:00Z)
|> filter(fn: (r) => r._measurement == "average_temperature")
|> last()
|> keep(columns: ["_value", "location"])
join(tables: {mean: means, reading: latest}, on: ["location"])
|> map(fn: (r) => ({r with deviation: r._value_reading - r._value_mean}))
```
### Example results
| location | _value_mean | _value_reading | deviation |
|:-------- | -----------: | --------------:| ---------: |
| coyote_creek | 79.82710622710623 | 89 | 9.172893772893772 |
| santa_monica | 80.20451339915374 | 85 | 4.79548660084626 |

View File

@ -0,0 +1,57 @@
---
title: Find and count unique values
description: >
Count the number of unique values in a specified column.
influxdb/v2.0/tags: [queries]
menu:
influxdb_2_0:
name: Count unique values
parent: Common queries
weight: 104
---
{{% note %}}
These examples use [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
The following examples identify and count unique locations that data was collected from.
## Find unique values
This query:
- Uses [`group()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/group/) to ungroup data and return results in a single table.
- Uses [`keep()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/keep/) and [`unique()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/selectors/unique/) to return unique values in the specified column.
```js
import "experimental/csv"
csv.from(bucket: "noaa")
|> group()
|> keep(columns: ["location"])
|> unique(column: "location")
```
### Example results
| location |
|:-------- |
| coyote_creek |
| santa_monica |
## Count unique values
This query:
- Uses [`group()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/group/) to ungroup data and return results in a single table.
- Uses [`keep()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/keep/), [`unique()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/selectors/unique/), and then [`count()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/count/) to count the number of unique values.
```js
from(bucket: "noaa")
|> group()
|> unique(column: "location")
|> count(column: "location")
```
### Example results
| location |
| ---------:|
| 2 |

View File

@ -0,0 +1,42 @@
---
title: Recalculate the _value column
description: Recalculate the `_value` column without creating a new one.
influxdb/v2.0/tags: [queries]
menu:
influxdb_2_0:
name: Recalculate the _value column
parent: Common queries
weight: 104
---
{{% note %}}
This example uses [NOAA water sample data](/influxdb/v2.0/reference/sample-data/#noaa-water-sample-data).
{{% /note %}}
Recalculate the `_value` column without creating a new one. Use the `with` operator in [`map()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/) to overwrite the existing `_value` column.
The following query:
- Uses [`filter()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/filter/) to filter the `average_temperature` measurement.
- Uses [`map()`](/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/) to convert Fahrenheit temperature values into Celsius.
```js
from(bucket: "noaa")
|> filter(fn: (r) => r._measurement == "average_temperature")
|> map(fn: (r) => ({r with _value: (float(v: r._value) - 32.0) * 5.0 / 9.0} ))
```
| _field | _measurement | _start | _stop | _time | location | _value |
|:------ |:------------ |:------ |:----- |:----- |:-------- | ------: |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:00:00Z | coyote_creek | 27.77777777777778 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:06:00Z | coyote_creek | 22.77777777777778 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:12:00Z | coyote_creek | 30 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:18:00Z | coyote_creek | 31.666666666666668 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:24:00Z | coyote_creek | 25 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:30:00Z | coyote_creek | 21.11111111111111 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:36:00Z | coyote_creek | 28.88888888888889 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:42:00Z | coyote_creek | 24.444444444444443 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:48:00Z | coyote_creek | 29.444444444444443 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T00:54:00Z | coyote_creek | 26.666666666666668 |
| degrees | average_temperature | 1920-03-05T22:10:01Z | 2020-03-05T22:10:01Z | 2019-08-17T01:00:00Z | coyote_creek | 21.11111111111111 |
| ••• | ••• | ••• | ••• | ••• | ••• | ••• |

View File

@ -53,3 +53,20 @@ to query and analyze the geo-temporal data in this sample data set.
</a>
_Used in [Work with geo-temporal data](/influxdb/v2.0/query-data/flux/geo/)._
### NOAA water sample data
This data set is publicly available data from the [National Oceanic and Atmospheric Administrations (NOAA) Center for Operational Oceanographic Products and Services](http://tidesandcurrents.noaa.gov/stations.html).
[The CSV data](https://influx-testdata.s3.amazonaws.com/noaa.csv) includes 15,258 observations of water levels (ft) collected every six minutes at two stations (Santa Monica, CA (ID 9410840) and Coyote Creek, CA (ID 9414575)) over the period from August 18, 2015 through September 18, 2015.
To avoid having to re-download this 10MB dataset every time you run a query, we recommend that you [create a new bucket](/influxdb/v2.0/organizations/buckets/create-bucket/) (`noaa`) and write the NOAA data to it. To do so, run the following:
```js
import "experimental/csv"
csv.from(url: "https://influx-testdata.s3.amazonaws.com/noaa.csv")
|> to(bucket: "noaa", org: "your-org")
```
_Used in [Common queries](/influxdb/v2.0/query-data/common-queries/) and [Common tasks](/influxdb/v2.0/process-data/common-tasks/)._