Skip to content

Commit

Permalink
TSML: Improve timeseries-anomaly-detection.ipynb notebook
Browse files Browse the repository at this point in the history
- Use `REFRESH TABLE` to synchronize data
- Improve formatting around `CONNECTION_STRING`
  • Loading branch information
amotl committed Apr 19, 2024
1 parent dfe15cf commit b290bae
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions topic/timeseries/timeseries-anomaly-detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand All @@ -98,25 +98,22 @@
"metadata": {},
"outputs": [],
"source": [
"# Use this connection string template for CrateDB Cloud Clusters\n",
"# crate://<USER>:<PASSWORD>@<HOST>\n",
"CONNECTION_STRING = os.environ.get(\n",
" \"CRATEDB_CONNECTION_STRING\",\n",
" \"crate://<USER>:<PASSWORD>@<HOST>\",\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))"
]
},
{
Expand Down

0 comments on commit b290bae

Please sign in to comment.