From 9e66e2cfac7184848a7c47922ea258559c768a18 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 19 Apr 2024 10:20:48 +0200 Subject: [PATCH] TSML: Improve `timeseries-anomaly-detection.ipynb` notebook - Use `REFRESH TABLE` to synchronize data - Improve formatting around `CONNECTION_STRING` --- .../timeseries-anomaly-detection.ipynb | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/topic/timeseries/timeseries-anomaly-detection.ipynb b/topic/timeseries/timeseries-anomaly-detection.ipynb index ebcb7015..85a70de1 100644 --- a/topic/timeseries/timeseries-anomaly-detection.ipynb +++ b/topic/timeseries/timeseries-anomaly-detection.ipynb @@ -88,7 +88,7 @@ "In this step, we will create the table and populate it with the dataset from [nab-machine-failure.csv]. If you are using CrateDB Cloud, you can use the [URL import] available in the console, otherwise, use the `COPY FROM` statement used below. You can run it in the console in the Admin UI or you can use [Crash], in this case, we are using SQLAlchemy.\n", "\n", "[URL import]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#import-from-url\n", - "[nab-machine-failure.csv]: https://github.com/crate/cratedb-datasets/raw/main/timeseries/nab-machine-failure.csv\",\n", + "[nab-machine-failure.csv]: https://github.com/crate/cratedb-datasets/raw/main/timeseries/nab-machine-failure.csv\n", "[Crash]: https://cratedb.com/docs/crate/crash/en/latest/getting-started.html" ] }, @@ -98,25 +98,22 @@ "metadata": {}, "outputs": [], "source": [ - "# Use this connection string template for CrateDB Cloud Clusters\n", + "# crate://:@\n", "CONNECTION_STRING = os.environ.get(\n", " \"CRATEDB_CONNECTION_STRING\",\n", - " \"crate://:@\",\n", - ")\n", - "\n", - "# Use this connection string template for CrateDB running locally\n", - "#CONNECTION_STRING = os.environ.get(\n", - "# \"CRATEDB_CONNECTION_STRING\",\n", - "# \"crate://localhost:4200\",\n", - "# )\n", + " \"crate://localhost:4200\",\n", + " )\n", "\n", "engine = sa.create_engine(CONNECTION_STRING, echo=os.environ.get('DEBUG'))\n", "\n", - "query_create_table = 'CREATE TABLE machine_data (\"timestamp\" TIMESTAMP, \"value\" DOUBLE PRECISION)' \n", - "query_copy_from = \"COPY machine_data FROM 'https://github.com/crate/cratedb-datasets/raw/main/timeseries/nab-machine-failure.csv';\"\n", + "sql_ddl = 'CREATE TABLE machine_data (\"timestamp\" TIMESTAMP, \"value\" DOUBLE PRECISION)'\n", + "sql_load = \"COPY machine_data FROM 'https://github.com/crate/cratedb-datasets/raw/main/timeseries/nab-machine-failure.csv';\"\n", + "sql_refresh = \"REFRESH TABLE machine_data;\"\n", + "\n", "with engine.connect() as conn:\n", - " conn.execute(sa.text(query_create_table))\n", - " conn.execute(sa.text(query_copy_from))\n" + " conn.execute(sa.text(sql_ddl))\n", + " conn.execute(sa.text(sql_load))\n", + " conn.execute(sa.text(sql_refresh))" ] }, {