Skip to content

Commit

Permalink
ML/AutoML: Reworked connection string for easier usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ckurze authored and amotl committed Dec 2, 2023
1 parent 91617d5 commit 89abd38
Showing 1 changed file with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,48 @@
"sign up for a free account at https://console.cratedb.cloud and \n",
"[deploy a cluster].\n",
"\n",
"[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To populate your environment with corresponding database access credentials,\n",
"create an `.env` file with the following content:\n",
"\n",
"```env\n",
"CRATE_HOST=<your-crate-host> # set this to localhost if you're running crate locally\n",
"CRATE_USER=<your-crate-user> # set this to crate if you're running crate locally\n",
"CRATE_PASSWORD=<your-crate-password> # set this to \"\" if you're running crate locally\n",
"CRATE_SSL=true # set this to false if you're running crate locally\n",
"# use this string for a connection to CrateDB Cloud\n",
"CONNECTION_STRING=crate://username:password@hostname/?ssl=true \n",
"\n",
"# use this string for a local connection to CrateDB\n",
"# CONNECTION_STRING=crate://crate@localhost/?ssl=false \n",
"```\n",
"\n",
"You can find your CrateDB credentials in the [CrateDB Cloud Console].\n",
"\n",
"[CrateDB Cloud Console]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#cluster\n",
"[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster\n"
"[CrateDB Cloud Console]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#cluster"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# For CrateDB Cloud, use:\n",
"CONNECTION_STRING = os.environ.get(\n",
" \"CRATEDB_CONNECTION_STRING\",\n",
" \"crate://username:password@hostname/?ssl=true\",\n",
")\n",
"\n",
"# For an self-deployed CrateDB, e.g. via Docker, please use:\n",
"# CONNECTION_STRING = os.environ.get(\n",
"# \"CRATEDB_CONNECTION_STRING\",\n",
"# \"crate://crate@localhost/?ssl=false\",\n",
"# )"
]
},
{
Expand Down Expand Up @@ -203,8 +231,7 @@
"data[\"date\"] = pd.to_datetime(data[\"date\"])\n",
"\n",
"# Insert the data into CrateDB\n",
"dburi = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}\"\n",
"engine = sa.create_engine(dburi, echo=os.environ.get(\"DEBUG\"))\n",
"engine = sa.create_engine(CONNECTION_STRING, echo=os.environ.get(\"DEBUG\"))\n",
"\n",
"with engine.connect() as conn:\n",
" data.to_sql(\n",
Expand Down Expand Up @@ -253,7 +280,7 @@
"data[\"month\"] = pd.to_datetime(data['month'], unit='ms')\n",
"\n",
"# We set the MLFLOW_TRACKING_URI to our CrateDB instance. We'll see later why\n",
"os.environ[\"MLFLOW_TRACKING_URI\"] = f\"{dburi}&schema=mlflow\""
"os.environ[\"MLFLOW_TRACKING_URI\"] = f\"{CONNECTION_STRING}&schema=mlflow\""
]
},
{
Expand Down Expand Up @@ -1277,28 +1304,28 @@
"output_type": "display_data"
},
{
"name": "stdout",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stdout",
"output_type": "stream",
"text": [
"Fitting 3 folds for each of 10 candidates, totalling 30 fits\n"
]
},
{
"name": "stderr",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stderr",
"output_type": "stream",
"text": [
"[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n",
Expand Down Expand Up @@ -1433,28 +1460,28 @@
"output_type": "display_data"
},
{
"name": "stdout",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stdout",
"output_type": "stream",
"text": [
"Fitting 3 folds for each of 10 candidates, totalling 30 fits\n"
]
},
{
"name": "stderr",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stderr",
"output_type": "stream",
"text": [
"[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n",
Expand Down Expand Up @@ -1583,28 +1610,28 @@
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stdout",
"output_type": "stream",
"text": [
"Fitting 3 folds for each of 10 candidates, totalling 30 fits\n"
]
},
{
"name": "stderr",
"metadata": {
"nbreg": {
"diff_ignore": [
"/outputs"
]
}
},
"name": "stderr",
"output_type": "stream",
"text": [
"[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n",
Expand Down Expand Up @@ -2086,7 +2113,7 @@
"source": [
"os.environ[\n",
" \"MLFLOW_TRACKING_URI\"\n",
"] = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}&schema=mlflow\""
"] = f\"{CONNECTION_STRING}&schema=mlflow\""
]
},
{
Expand Down Expand Up @@ -2182,7 +2209,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "crate",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -2196,7 +2223,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 89abd38

Please sign in to comment.