docs-v2/content/v2.0/query-data/guides/sql.md

5.7 KiB

title seotitle description v2.0/tags menu weight
Query SQL data sources Query SQL data sources with InfluxDB placeholder
query
flux
sql
v2_0
parent
How-to guides
207

The Flux sql package provides functions for working with SQL data sources. sql.from() lets you query SQL databases like PostgreSQL and MySQL 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 the sql.from() function:

{{< code-tabs-wrapper >}} {{% code-tabs %}} Postgres MySQL {{% /code-tabs %}}

{{% code-tab-content %}}

import "sql"

sql.from(
  driverName: "postgres",
  dataSourceName: "postgresql://user:password@localhost",
  query: "SELECT * FROM exampleTable"
)

{{% /code-tab-content %}}

{{% code-tab-content %}}

import "sql"

sql.from(
  driverName: "mysql",
  dataSourceName: "user:password@tcp(localhost:3306)/db",
  query: "SELECT * FROM exampleTable"
)

{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}

See the sql.from() documentation for information about required function parameters.

Use cases

Join SQL results with time series data

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.

Using the air sensor sample data below, the following query 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 that isn't stored in InfluxDB.

import "sql"

sensorInfo = sql.from(
  driverName: "postgres",
  dataSourceName: "postgresql://localhost?sslmode=disable",
  query: "SELECT * FROM sensors"
)

sensorMetrics = from(bucket: "example-bucket")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "airSensors")

join(tables: {metric: sensorMetrics, info: sensorInfo}, on: ["sensor_id"])

Create dashboard variables using SQL results

With the sql.from() function you're able to create dashboard variables from SQL query results. The following example uses the air sensor sample data below to create a variable that lets you select the location of a sensor.

import "sql"

sql.from(
    driverName: "postgres",
    dataSourceName: "postgresql://localhost?sslmode=disable",
    query: "SELECT * FROM sensors"
  )
  |> rename(columns: {location: "_value"})
  |> keep(columns: ["_value"])

You can then use this variable to manipulate queries in your dashboards.

{{< img-hd src="/img/2-0-sql-dashboard-variable.png" alt="Dashboard variable from SQL query results" />}}


Sample data

The sample data generator and sample sensor information simulate a fleet of sensors installed throughout a facility measuring the temperature, humidity, and carbon monoxide in each room. Each collected data point is stored in InfluxDB with a sensor_id tag that identifies the specific sensor it came from.

Observed sensor data is stored in the airSensors measurement which contains the following fields:

  • temperature
  • humidity
  • co

Information about each sensor is stored in a sensors table in a Postgres database:

  • sensor_id
  • location
  • model_number
  • last_inspected

Sample data generator

air-sensor-data is a CLI that generates air sensor data and stores in InfluxDB. To use it:

  1. Create a bucket in which to store the generated data.

  2. Get your authorization token.

  3. Download the sample data generator. This tool requires Ruby.

    Download Air Sensor Generator

  4. Give air-sensor-data executable permissions:

    chmod +x air-sensor-data
    
  5. Start the generator by providing it with your organization, bucket, and authorization token:

    air-sensor-data -o your-org -b your-bucket -t YOURAUTHTOKEN
    

    The generator will begin writing data to InfluxDB.

    Note: Use the --help flag to view other configuration options.

Sample sensor information

  1. Download and install Postgres.

  2. Download the sample sensor information CSV.

    Download Sample Data

  3. Use a Postgres client (psql or a GUI) to import the CSV file using the following:

DO $$
DECLARE
  filepath VARCHAR(200) := '/path/to/sample-sensor-info.csv';
BEGIN
  -- Create the sensors table
  CREATE TABLE sensors (
    sensor_id character varying(50),
    location character varying(50),
    model_number character varying(50),
    last_inspected date
  );
  -- Import sample CSV from your filesystem
  COPY sensors(sensor_id,location,model_number,last_inspected)
  FROM filepath DELIMITER ',' CSV HEADER;
END $$

{{% note %}} Update the filepath variable to match the path of your to your downloaded sample data CSV. {{% /note %}}

Query the table to ensure the data was imported correctly:

SELECT * FROM sensors;

Sample dashboard

Download and import the Air Sensors dashboard to visualize the generated data:

Download Air Sensors dashboard

See Create a dashboard for information about importing a dashboard.