updated sql guide to address PR feedback
parent
b73acbeea5
commit
cd94b9102d
|
@ -16,12 +16,15 @@ The [Flux](/v2.0/reference/flux) `sql` package provides functions for working wi
|
||||||
like [PostgreSQL](https://www.postgresql.org/) and [MySQL](https://www.mysql.com/)
|
like [PostgreSQL](https://www.postgresql.org/) and [MySQL](https://www.mysql.com/)
|
||||||
and use the results with InfluxDB dashboards, tasks, and other operations.
|
and use the results with InfluxDB dashboards, tasks, and other operations.
|
||||||
|
|
||||||
To query a SQL data source, import the `sql` package in your Flux query and use
|
To query a SQL data source:
|
||||||
the `sql.from()` function:
|
|
||||||
|
1. import the `sql` package in your Flux query
|
||||||
|
2. Use the `sql.from()` function to specify the driver, data source name (DSN),
|
||||||
|
and query used to query data from your SQL data source:
|
||||||
|
|
||||||
{{< code-tabs-wrapper >}}
|
{{< code-tabs-wrapper >}}
|
||||||
{{% code-tabs %}}
|
{{% code-tabs %}}
|
||||||
[Postgres](#)
|
[PostgreSQL](#)
|
||||||
[MySQL](#)
|
[MySQL](#)
|
||||||
{{% /code-tabs %}}
|
{{% /code-tabs %}}
|
||||||
|
|
||||||
|
@ -53,34 +56,36 @@ sql.from(
|
||||||
_See the [`sql.from()` documentation](/v2.0/reference/flux/functions/sql/from/) for
|
_See the [`sql.from()` documentation](/v2.0/reference/flux/functions/sql/from/) for
|
||||||
information about required function parameters._
|
information about required function parameters._
|
||||||
|
|
||||||
## Use cases
|
## Join SQL data with data in InfluxDB
|
||||||
|
|
||||||
### Join SQL data with data in InfluxDB
|
|
||||||
One of the primary benefits of querying SQL data sources from InfluxDB
|
One of the primary benefits of querying SQL data sources from InfluxDB
|
||||||
is the ability to enrich query results with data stored outside of InfluxDB.
|
is the ability to enrich query results with data stored outside of InfluxDB.
|
||||||
|
|
||||||
Using the [air sensor sample data](#sample-data) below, the following query
|
Using the [air sensor sample data](#sample-sensor-data) below, the following query
|
||||||
joins air sensor metrics stored in InfluxDB with sensor information stored in PostgreSQL.
|
joins air sensor metrics stored in InfluxDB with sensor information stored in PostgreSQL.
|
||||||
The joined data lets you query and filter results based on sensor information
|
The joined data lets you query and filter results based on sensor information
|
||||||
that isn't stored in InfluxDB.
|
that isn't stored in InfluxDB.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
// Import the "sql" package
|
||||||
import "sql"
|
import "sql"
|
||||||
|
|
||||||
|
// Query data from PostgreSQL
|
||||||
sensorInfo = sql.from(
|
sensorInfo = sql.from(
|
||||||
driverName: "postgres",
|
driverName: "postgres",
|
||||||
dataSourceName: "postgresql://localhost?sslmode=disable",
|
dataSourceName: "postgresql://localhost?sslmode=disable",
|
||||||
query: "SELECT * FROM sensors"
|
query: "SELECT * FROM sensors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Query data from InfluxDB
|
||||||
sensorMetrics = from(bucket: "example-bucket")
|
sensorMetrics = from(bucket: "example-bucket")
|
||||||
|> range(start: -1h)
|
|> range(start: -1h)
|
||||||
|> filter(fn: (r) => r._measurement == "airSensors")
|
|> filter(fn: (r) => r._measurement == "airSensors")
|
||||||
|
|
||||||
|
// Join InfluxDB query results with PostgreSQL query results
|
||||||
join(tables: {metric: sensorMetrics, info: sensorInfo}, on: ["sensor_id"])
|
join(tables: {metric: sensorMetrics, info: sensorInfo}, on: ["sensor_id"])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Use SQL results to populate dashboard variables
|
## Use SQL results to populate dashboard variables
|
||||||
Use `sql.from()` to [create dashboard variables](/v2.0/visualize-data/variables/create-variable/)
|
Use `sql.from()` to [create dashboard variables](/v2.0/visualize-data/variables/create-variable/)
|
||||||
from SQL query results.
|
from SQL query results.
|
||||||
The following example uses the [air sensor sample data](#sample-data) below to
|
The following example uses the [air sensor sample data](#sample-data) below to
|
||||||
|
@ -104,39 +109,40 @@ Use the variable to manipulate queries in your dashboards.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Sample data
|
## Sample sensor data
|
||||||
The [sample data generator](#download-and-run-the-sample-data-generator) and
|
The [sample data generator](#download-and-run-the-sample-data-generator) and
|
||||||
[sample sensor information](#import-the-sample-sensor-information) simulate a
|
[sample sensor information](#import-the-sample-sensor-information) simulate a
|
||||||
group of sensors that measure temperature, humidity, and carbon monoxide
|
group of sensors that measure temperature, humidity, and carbon monoxide
|
||||||
in rooms throughout a building.
|
in rooms throughout a building.
|
||||||
Each collected data point is stored in InfluxDB with a `sensor_id` tag that identifies
|
Each collected data point is stored in InfluxDB with a `sensor_id` tag that identifies
|
||||||
the specific sensor it came from.
|
the specific sensor it came from.
|
||||||
|
Sample sensor information is stored in PostgreSQL.
|
||||||
|
|
||||||
Observed sensor data is stored in the `airSensors` measurement which contains the following fields:
|
**Sample data includes:**
|
||||||
|
|
||||||
- temperature
|
- Simulated data collected from each sensor and stored in the `airSensors` measurement in **InfluxDB**:
|
||||||
- humidity
|
- temperature
|
||||||
- co
|
- humidity
|
||||||
|
- co
|
||||||
|
|
||||||
Information about each sensor is stored in a `sensors` table in a Postgres database:
|
- Information about each sensor stored in the `sensors` table in **PostgreSQL**:
|
||||||
|
- sensor_id
|
||||||
- sensor_id
|
- location
|
||||||
- location
|
- model_number
|
||||||
- model_number
|
- last_inspected
|
||||||
- last_inspected
|
|
||||||
|
|
||||||
### Import and generate sample sensor data
|
### Import and generate sample sensor data
|
||||||
|
|
||||||
#### Download and run the sample data generator
|
#### Download and run the sample data generator
|
||||||
`air-sensor-data` is a CLI that generates air sensor data and stores the data in InfluxDB.
|
`air-sensor-data.rb` is a script that generates air sensor data and stores the data in InfluxDB.
|
||||||
To use `air-sensor-data`:
|
To use `air-sensor-data.rb`:
|
||||||
|
|
||||||
1. [Create a bucket](/v2.0/organizations/buckets/create-bucket/) to store the data.
|
1. [Create a bucket](/v2.0/organizations/buckets/create-bucket/) to store the data.
|
||||||
2. Download the sample data generator. _This tool requires **Ruby**._
|
2. Download the sample data generator. _This tool requires [Ruby](https://www.ruby-lang.org/en/)._
|
||||||
|
|
||||||
<a class="btn download" href="/downloads/air-sensor-data.rb" download>Download Air Sensor Generator</a>
|
<a class="btn download" href="/downloads/air-sensor-data.rb" download>Download Air Sensor Generator</a>
|
||||||
|
|
||||||
3. Give `air-sensor-data` executable permissions:
|
3. Give `air-sensor-data.rb` executable permissions:
|
||||||
|
|
||||||
```
|
```
|
||||||
chmod +x air-sensor-data.rb
|
chmod +x air-sensor-data.rb
|
||||||
|
@ -167,12 +173,12 @@ To use `air-sensor-data`:
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Import the sample sensor information
|
#### Import the sample sensor information
|
||||||
1. [Download and install Postgres](https://www.postgresql.org/download/).
|
1. [Download and install PostgreSQL](https://www.postgresql.org/download/).
|
||||||
2. Download the sample sensor information CSV.
|
2. Download the sample sensor information CSV.
|
||||||
|
|
||||||
<a class="btn download" href="/downloads/sample-sensor-info.csv" download>Download Sample Data</a>
|
<a class="btn download" href="/downloads/sample-sensor-info.csv" download>Download Sample Data</a>
|
||||||
|
|
||||||
3. Use a Postgres client (`psql` or a GUI) to create the `sensors` table:
|
3. Use a PostgreSQL client (`psql` or a GUI) to create the `sensors` table:
|
||||||
|
|
||||||
```
|
```
|
||||||
CREATE TABLE sensors (
|
CREATE TABLE sensors (
|
||||||
|
|
Loading…
Reference in New Issue