DEV Community

Fernando Paladini
Fernando Paladini

Posted on

Export ScadaBR Sensor Histories from MySQL to KairosDB with Python

Industrial and lab automation stacks built on ScadaBR keep years of sensor readings inside MySQL or MariaDB tables. Relational storage handles live alarms and point configuration well, yet long-horizon aggregations over millions of historical samples are easier to serve from a time-series database such as KairosDB. Moving those histories manually means mapping ScadaBR's two-table point schema into KairosDB's tagged JSON format, preserving millisecond timestamps, and compressing batches over HTTP.

ScadaBR_to_KairosDB is a compact MIT-licensed Python 3 exporter that reads ScadaBR's dataPoints and pointValues tables from MySQL or MariaDB, formats each sensor series as tagged KairosDB JSON, compresses the payload with gzip, and posts it to http://localhost:8080/api/v1/datapoints.

This tutorial shows how to set up an isolated Python virtual environment, grant a read-only database user instead of root, run the exporter directly with Python 3, verify ingested points in KairosDB, and handle the script's connection, schema, and error-checking limits.

TL;DR

Create a read-only MySQL or MariaDB user, install PyMySQL and requests in .venv, run bin/scadabr_database_to_kairosdb.py with MySQL and KairosDB listening on localhost, and query KairosDB afterward to confirm sample counts:

curl -fsSL https://github.com/paladini/ScadaBR_to_KairosDB/archive/5a625f1bae68be12bb145b09557045afd7092365.tar.gz | tar -xz
cd ScadaBR_to_KairosDB-5a625f1bae68be12bb145b09557045afd7092365

python3 -m venv .venv
.venv/bin/pip install pymysql requests

.venv/bin/python bin/scadabr_database_to_kairosdb.py scadabr scadabr_exporter replace_with_local_pw
curl -fsS http://localhost:8080/api/v1/metricnames
Enter fullscreen mode Exit fullscreen mode

Do not treat terminal status lines alone as proof of delivery: scadabr_database_to_kairosdb.py does not inspect HTTP status codes from requests.post(), so always verify metric names and sample_size in KairosDB after the export completes.

Prerequisites

You need:

  • Python 3.8+ with venv (smoke-tested on Python 3.14.3 with PyMySQL 2.2.8 and requests 2.34.2).
  • MySQL or MariaDB on localhost:3306 with a ScadaBR schema (dataPoints and pointValues).
  • KairosDB listening on http://localhost:8080 (pushing data docs).
  • curl and tar (or git) to download the repository.

Release and source facts:

1. Download the exporter and install dependencies

README.md lists PyMySQL, while bin/scadabr_database_to_kairosdb.py also imports requests alongside Python's standard gzip, json, and sys modules. Install both inside a virtual environment:

curl -fsSL https://github.com/paladini/ScadaBR_to_KairosDB/archive/5a625f1bae68be12bb145b09557045afd7092365.tar.gz -o scadabr-to-kairosdb.tar.gz
tar -xzf scadabr-to-kairosdb.tar.gz
cd ScadaBR_to_KairosDB-5a625f1bae68be12bb145b09557045afd7092365

python3 -m venv .venv
.venv/bin/pip install pymysql requests
Enter fullscreen mode Exit fullscreen mode

2. Prepare a read-only MySQL user and test fixture

export.sh defaults to MySQL root. Because the script only runs SELECT against dataPoints and pointValues, grant a temporary read-only user instead. To test safely without a live ScadaBR server, create a minimal scadabr_demo database matching ScadaBR's leading columns (id and xid on dataPoints; dataPointId, dataType, pointValue, and millisecond ts on pointValues):

CREATE DATABASE IF NOT EXISTS scadabr_demo;
CREATE TABLE scadabr_demo.dataPoints (
  id INT PRIMARY KEY,
  xid VARCHAR(50) NOT NULL,
  dataSourceId INT NOT NULL
);
CREATE TABLE scadabr_demo.pointValues (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  dataPointId INT NOT NULL,
  dataType INT NOT NULL,
  pointValue DOUBLE NOT NULL,
  ts BIGINT NOT NULL
);

INSERT INTO scadabr_demo.dataPoints (id, xid, dataSourceId) VALUES
  (1, 'DP_boiler_temp_c', 10),
  (2, 'DP_line_pressure_bar', 10);

INSERT INTO scadabr_demo.pointValues (dataPointId, dataType, pointValue, ts) VALUES
  (1, 3, 78.4, 1732795200000),
  (1, 3, 79.1, 1732795260000),
  (2, 3, 4.25, 1732795200000);

CREATE USER 'scadabr_exporter'@'localhost' IDENTIFIED BY 'replace_with_local_pw';
GRANT SELECT ON scadabr_demo.dataPoints TO 'scadabr_exporter'@'localhost';
GRANT SELECT ON scadabr_demo.pointValues TO 'scadabr_exporter'@'localhost';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

For an existing scadabr database, grant SELECT on scadabr.dataPoints and scadabr.pointValues instead.

3. Run the Python exporter directly

Calling bin/scadabr_database_to_kairosdb.py directly avoids chmod edits, runs inside .venv, and works across Linux, macOS, and Windows. Pass <database> <username> [password]:

.venv/bin/python bin/scadabr_database_to_kairosdb.py scadabr_demo scadabr_exporter replace_with_local_pw
Enter fullscreen mode Exit fullscreen mode

4. Expected output and KairosDB verification

With the two-sensor fixture above, the exporter prints:

For comparison purposes, we've waited for about 15 minutes to export 1.7 million point values in a low-end notebook.
Number of data points detected: 2.

    [STATUS] Exporting 2 point values from DP_boiler_temp_c.
    [STATUS] Sending data to KairosDB. This may take some minutes...
    [STATUS] 2 values from data point DP_boiler_temp_c has sent to KairosDB.
    [STATUS] Exporting 1 point values from DP_line_pressure_bar.
    [STATUS] Sending data to KairosDB. This may take some minutes...
    [STATUS] 1 values from data point DP_line_pressure_bar has sent to KairosDB.

[STATUS] Finished!
Enter fullscreen mode Exit fullscreen mode

Each POST sends content-type: application/gzip to http://localhost:8080/api/v1/datapoints. Decompressing the first request payload shows the exact KairosDB JSON object produced:

{
  "name": "DP_boiler_temp_c",
  "tags": {
    "sensor_name": "DP_boiler_temp_c"
  },
  "datapoints": [
    [1732795200000, 78.4],
    [1732795260000, 79.1]
  ]
}
Enter fullscreen mode Exit fullscreen mode

Confirm that KairosDB stored every point by listing metric names and querying the window:

curl -fsS http://localhost:8080/api/v1/metricnames

curl -fsS -X POST http://localhost:8080/api/v1/datapoints/query \
  -H "Content-Type: application/json" \
  -d '{
    "start_absolute": 1732795000000,
    "end_absolute": 1732795500000,
    "metrics": [
      {"name": "DP_boiler_temp_c"},
      {"name": "DP_line_pressure_bar"}
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

Check that queries[0].sample_size equals 2 and queries[1].sample_size equals 1. Afterward, revoke the temporary MySQL user (DROP USER 'scadabr_exporter'@'localhost';).

Why It Works

The 62-line script in bin/scadabr_database_to_kairosdb.py bridges three format details between ScadaBR and KairosDB:

  • Direct millisecond timestamps: ScadaBR stores pointValues.ts as Unix epoch milliseconds (BIGINT), and KairosDB's REST API requires timestamps in milliseconds since 1970-01-01T00:00:00Z, so [pv[2], pv[1]] pairs ([ts, pointValue]) need no unit scaling.
  • Per-sensor reconnection: After reading SELECT * FROM dataPoints;, each loop iteration opens mariadb.connect(...).cursor() before querying pointValues. Reconnecting per sensor avoids MySQL wait_timeout drops when uploading large point series.
  • Tagged gzipped payloads: KairosDB requires at least one tag per metric. Setting "name": dp[1] and "tags": {"sensor_name": dp[1]} from ScadaBR's xid column satisfies tag validation, while gzip.compress() with content-type: application/gzip cuts HTTP payload size.

Failure Modes, Limitations, and Security Boundaries

Account for these source-verified constraints before migrating real archives:

  • Unchecked HTTP status codes: requests.post(...) does not check response.status_code (204 No Content on success vs 400 Bad Request on invalid names or tags). Always verify sample_size via /api/v1/datapoints/query.
  • Positional SELECT * columns and numeric scope: dp[0] (id, formatted via %u) and dp[1] (xid) must be the first two columns of dataPoints. Numeric readings in pointValues.pointValue (DOUBLE) export cleanly, whereas text or image values in pointValueAnnotations are ignored, and xid values with spaces can fail KairosDB metric validation.
  • Unclosed connections per sensor: Creating a fresh connection per dataPoint without calling .close() keeps sockets open until process exit; ensure MySQL max_connections exceeds COUNT(*) + 10 on dataPoints.
  • In-memory sensor batches and localhost endpoints: Each sensor's full pointValues history loads into one Python list before compression, and both localhost:3306 and http://localhost:8080 are hardcoded. Use local SSH tunnels (ssh -L) for remote servers rather than exposing unauthenticated KairosDB ports.
  • CLI password exposure and export.sh limits: Passing a password in sys.argv[3] exposes it in ps aux on multi-user hosts, and export.sh uses GNU Bash read -e -i (Bash 4+) plus unquoted arguments. Use a temporary read-only MySQL account on a single-tenant machine, and treat the printed 15-minute / 1.7-million-row banner only as a 2015 author note, not a benchmark.

FAQ

Why skip export.sh in scripts or non-GNU shells?

export.sh runs clear, requires Bash 4+ read -e -i, and passes $DATABASE_PASSWORD unquoted to Python. Running bin/scadabr_database_to_kairosdb.py directly inside .venv avoids shell-compatibility and word-splitting bugs.

Is re-running the exporter idempotent?

KairosDB overwrites points that share the same metric name, tag set, and millisecond timestamp. Re-running against static ScadaBR tables does not duplicate samples, though it re-uploads every batch.

How can I reach remote databases without editing the script?

Forward local ports over SSH (ssh -N -L 3306:127.0.0.1:3306 scada-db and ssh -N -L 8080:127.0.0.1:8080 kairos-node) when local ports 3306 and 8080 are free.

Takeaway

ScadaBR_to_KairosDB offers a straightforward Python 3 path from ScadaBR's relational dataPoints and pointValues tables to KairosDB's gzipped REST API. Pair it with an isolated virtual environment, a temporary read-only MySQL user, and a post-migration /api/v1/datapoints/query verification step.

When migrating legacy SCADA or IoT histories out of relational tables, do you stream directly into your target time-series API, or export intermediate CSV or Parquet files first?

AI assistance disclosure: This tutorial was researched, smoke-tested in an isolated Python 3.14.3 virtual environment (PyMySQL 2.2.8 and requests 2.34.2), and drafted with AI assistance. Source behavior, gzipped JSON payloads, limitations, and safety boundaries were verified against paladini/ScadaBR_to_KairosDB (5a625f1) and KairosDB's REST documentation.

Top comments (1)

Collapse
 
ahmetozel profile image
Ahmet Özel •

Explicitly calling out that the exporter does not inspect HTTP status codes from requests.post()—and therefore requiring a post-run verification query against KairosDB metric names and sample_size—saves engineers from trusting silent partial migrations. Too many legacy ETL tutorials treat a zero exit code as proof of ingestion.

Granting a dedicated read-only MySQL user instead of root is also good operational hygiene when pulling from a live ScadaBR instance. If you ever run incremental backfills over multi-year pointValues tables, checkpointing by timestamp and chunking queries avoids locking live alarm writes and makes re-runs idempotent when KairosDB returns a transient 5xx.