Hands-on session: Time Series Database (TSBD) ################################################################################ # InfluxDB ################################################################################ 1. Let's retrieve the Docker image of InfluxDB docker pull influxdb:2.0 2. We can now create an instance of InfluxDB; since we are interesting to use it from our local machine, we need to forward the Chronograf port (8086); docker run -p 8086:8086 \ -v $PWD:/var/lib/influxdb2 \ influxdb:2.0 3. We are ready to use InfluxDB. Let's open the Web UI of Chronograf, and complete the configuration wizard https://localhost:8086 For example, we use the following parameters: user: root password: adminadmin organization: org bucket: bucket 4. Using the Flux console, we can load some sample data to play around: import "influxdata/influxdb/sample" sample.data(set: "airSensor") |> to( org: "org", bucket: "bucket" ) 5. A query example - Get raw temperature data for the 'airSensors' measurement (table) from(bucket: "bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "airSensors") |> filter(fn: (r) => r["_field"] == "temperature") |> yield(name: "raw values") 6. Get data of a specific sensor ("TLM0100") from(bucket: "bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "airSensors") |> filter(fn: (r) => r["_field"] == "temperature") |> filter(fn: (r) => r["sensor_id"] == "TLM0100") |> yield(name: "raw values") 6. Get the average temperature registered by a specific sensor ("TLM0100") from(bucket: "bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "airSensors") |> filter(fn: (r) => r["_field"] == "temperature") |> filter(fn: (r) => r["sensor_id"] == "TLM0100") |> group(columns: ["sensorID"]) // this is not really needed here |> mean() Note: Use mean() to calculate the average value in each input table. Since InfluxDB groups data by series, mean() returns a table for each unique sensor_id containing a single row with the average value in the _value column. 7. Get the average temperature registered by all sensors from(bucket: "bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "airSensors") |> filter(fn: (r) => r["_field"] == "temperature") |> group(columns: ["sensorID"]) |> mean() Note the presence of "group()", which creates a single table for all data from all sensors