Skip to content

Commit dc60cc5

Browse files
chore: update sample used in notebooks (#606)
1 parent e4860a8 commit dc60cc5

File tree

3 files changed

+99
-102
lines changed

3 files changed

+99
-102
lines changed

samples/notebooks/mysql_python_connector.ipynb

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"nbformat_minor": 0,
44
"metadata": {
55
"colab": {
6-
"name": "mysql_python_connector.ipynb",
7-
"provenance": [],
8-
"collapsed_sections": []
6+
"provenance": []
97
},
108
"kernelspec": {
119
"name": "python3",
@@ -196,7 +194,7 @@
196194
"Configure the following in your Google Cloud Project.\n",
197195
"\n",
198196
"1. IAM principal (user, service account, etc.) with the\n",
199-
"[Cloud SQL Client][client-role] role. \n",
197+
"[Cloud SQL Client][client-role] role.\n",
200198
"\n",
201199
"> 🚨 The user logged into this notebook will be used as the IAM principal and will be granted the Cloud SQL Client role.\n",
202200
"\n",
@@ -304,16 +302,16 @@
304302
"id": "nzb0dFO6C4h6"
305303
},
306304
"source": [
307-
"### 🎬 Create a Movies Database\n",
308-
"A `movies` database will be used in later steps when connecting to and querying a Cloud SQL database.\n",
305+
"### 🥪 Create a Sandwiches Database\n",
306+
"A `sandwiches` database will be used in later steps when connecting to and querying a Cloud SQL database.\n",
309307
"\n",
310-
"To create a `movies` database within your Cloud SQL instance run the below command:"
308+
"To create a `sandwiches` database within your Cloud SQL instance run the below command:"
311309
]
312310
},
313311
{
314312
"cell_type": "code",
315313
"source": [
316-
"!gcloud sql databases create movies --instance={instance_name}"
314+
"!gcloud sql databases create sandwiches --instance={instance_name}"
317315
],
318316
"metadata": {
319317
"id": "0q5uFF0sJnWK"
@@ -327,8 +325,8 @@
327325
"id": "lcdZH4rANv6C"
328326
},
329327
"source": [
330-
"### 🥷 Create Batman Database User\n",
331-
"To create the `batman` database user that is used throughout the notebook, run the following `gcloud` command."
328+
"### 🧑‍🍳 Create a Chef Database User\n",
329+
"To create the `chef` database user that is used throughout the notebook, run the following `gcloud` command."
332330
]
333331
},
334332
{
@@ -337,9 +335,9 @@
337335
"id": "9NYmcepFOM12"
338336
},
339337
"source": [
340-
"!gcloud sql users create batman \\\n",
338+
"!gcloud sql users create chef \\\n",
341339
" --instance={instance_name} \\\n",
342-
" --password=\"robin\""
340+
" --password=\"food\""
343341
],
344342
"execution_count": null,
345343
"outputs": []
@@ -370,9 +368,9 @@
370368
},
371369
"source": [
372370
"### 🎟 **Configuring Credentials**\n",
373-
"The Cloud SQL Python Connector uses [**Application Default Credentials (ADC)**](https://cloud.google.com/docs/authentication) strategy for resolving credentials. \n",
371+
"The Cloud SQL Python Connector uses [**Application Default Credentials (ADC)**](https://cloud.google.com/docs/authentication) strategy for resolving credentials.\n",
374372
"\n",
375-
"> 💡 Using the Python Connector in Cloud Run, App Engine, or Cloud Functions will automatically use the service account deployed with each service, allowing this step to be skipped. ✅ \n",
373+
"> 💡 Using the Python Connector in Cloud Run, App Engine, or Cloud Functions will automatically use the service account deployed with each service, allowing this step to be skipped. ✅\n",
376374
"\n",
377375
"Please see the [google.auth](https://google-auth.readthedocs.io/en/master/reference/google.auth.html) package documentation for more information on how these credentials are sourced.\n",
378376
"\n",
@@ -391,7 +389,7 @@
391389
},
392390
"source": [
393391
"### 💻 **Install Code Dependencies**\n",
394-
"It is recommended to use the Connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). \n",
392+
"It is recommended to use the Connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/).\n",
395393
"This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed\n",
396394
"\n",
397395
"Let's `pip install` the [Cloud SQL Python Connector](https://github.com/GoogleCloudPlatform/cloud-sql-python-connector) as well as [SQLAlchemy](https://www.sqlalchemy.org/), using the below command."
@@ -405,7 +403,7 @@
405403
"source": [
406404
"# install dependencies\n",
407405
"import sys\n",
408-
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pymysql\"] SQLAlchemy"
406+
"!{sys.executable} -m pip install cloud-sql-python-connector[\"pymysql\"] SQLAlchemy==2.0.2"
409407
],
410408
"execution_count": null,
411409
"outputs": []
@@ -442,9 +440,9 @@
442440
"# initialize parameters\n",
443441
"INSTANCE_CONNECTION_NAME = f\"{project_id}:{region}:{instance_name}\" # i.e demo-project:us-central1:demo-instance\n",
444442
"print(f\"Your instance connection name is: {INSTANCE_CONNECTION_NAME}\")\n",
445-
"DB_USER = \"batman\"\n",
446-
"DB_PASS = \"robin\"\n",
447-
"DB_NAME = \"movies\""
443+
"DB_USER = \"chef\"\n",
444+
"DB_PASS = \"food\"\n",
445+
"DB_NAME = \"sandwiches\""
448446
],
449447
"execution_count": null,
450448
"outputs": []
@@ -458,12 +456,12 @@
458456
"### ✅ **Basic Usage**\n",
459457
"To connect to Cloud SQL using the connector, initialize a `Connector` object and call its `connect` method with the proper input parameters.\n",
460458
"\n",
461-
"The `connect` method takes in the parameters we previously defined, as well as a few additional parameters such as: \n",
459+
"The `connect` method takes in the parameters we previously defined, as well as a few additional parameters such as:\n",
462460
"* `driver`: The name of the database driver to connect with.\n",
463461
"* `ip_type` (optional): The IP type (public or private) used to connect. IP types can be either `IPTypes.PUBLIC` or `IPTypes.PRIVATE`. ([Example](#scrollTo=yjAPpIDdRfu2))\n",
464462
"* `enable_iam_auth`: (optional) Boolean enabling IAM based authentication. ([Example](#scrollTo=GpVKrv0TCXje))\n",
465463
"\n",
466-
"Let's show an example! 🤘 🙌 "
464+
"Let's show an example! 🤘 🙌"
467465
]
468466
},
469467
{
@@ -517,25 +515,27 @@
517515
"source": [
518516
"# connect to connection pool\n",
519517
"with pool.connect() as db_conn:\n",
520-
" # create ratings table in our movies database\n",
518+
" # create ratings table in our sandwiches database\n",
521519
" db_conn.execute(\n",
520+
" sqlalchemy.text(\n",
522521
" \"CREATE TABLE IF NOT EXISTS ratings \"\n",
523-
" \"( id SERIAL NOT NULL, title VARCHAR(255) NOT NULL, \"\n",
524-
" \"genre VARCHAR(255) NOT NULL, rating FLOAT NOT NULL, \"\n",
522+
" \"( id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, \"\n",
523+
" \"origin VARCHAR(255) NOT NULL, rating FLOAT NOT NULL, \"\n",
525524
" \"PRIMARY KEY (id));\"\n",
525+
" )\n",
526526
" )\n",
527527
" # insert data into our ratings table\n",
528528
" insert_stmt = sqlalchemy.text(\n",
529-
" \"INSERT INTO ratings (title, genre, rating) VALUES (:title, :genre, :rating)\",\n",
529+
" \"INSERT INTO ratings (name, origin, rating) VALUES (:name, :origin, :rating)\",\n",
530530
" )\n",
531531
"\n",
532532
" # insert entries into table\n",
533-
" db_conn.execute(insert_stmt, title=\"Batman Begins\", genre=\"Action\", rating=8.5)\n",
534-
" db_conn.execute(insert_stmt, title=\"Star Wars: Return of the Jedi\", genre=\"Action\", rating=9.1)\n",
535-
" db_conn.execute(insert_stmt, title=\"The Breakfast Club\", genre=\"Drama\", rating=8.3)\n",
533+
" db_conn.execute(insert_stmt, parameters={\"name\": \"HOTDOG\", \"origin\": \"Germany\", \"rating\": 7.5})\n",
534+
" db_conn.execute(insert_stmt, parameters={\"name\": \"BÀNH MÌ\", \"origin\": \"Vietnam\", \"rating\": 9.1})\n",
535+
" db_conn.execute(insert_stmt, parameters={\"name\": \"CROQUE MADAME\", \"origin\": \"France\", \"rating\": 8.3})\n",
536536
"\n",
537537
" # query and fetch ratings table\n",
538-
" results = db_conn.execute(\"SELECT * FROM ratings\").fetchall()\n",
538+
" results = db_conn.execute(sqlalchemy.text(\"SELECT * FROM ratings\")).fetchall()\n",
539539
"\n",
540540
" # show results\n",
541541
" for row in results:\n",
@@ -582,13 +582,13 @@
582582
"id": "GCsS4f5UCYUa"
583583
},
584584
"source": [
585-
"### 🪪 IAM Database Authentication \n",
585+
"### 🪪 IAM Database Authentication\n",
586586
"\n",
587-
"[Automatic IAM database authentication](https://cloud.google.com/sql/docs/mysql/authentication#automatic) is supported for **MySQL** Cloud SQL instances. \n",
587+
"[Automatic IAM database authentication](https://cloud.google.com/sql/docs/mysql/authentication#automatic) is supported for **MySQL** Cloud SQL instances.\n",
588588
"\n",
589589
"> 💡 This allows an IAM user to establish an authenticated connection to a MySQL database without having to set a password and enabling the `enable_iam_auth` parameter in the connector's `connect` method.\n",
590590
"\n",
591-
"> 🚨 If you are using a pre-existing Cloud SQL instance within this notebook you may need to [configure Cloud SQL instance to allow IAM authentication](https://cloud.google.com/sql/docs/mysql/create-edit-iam-instances#configuring_existing_instances_for) by setting the `cloudsql_iam_authentication` database flag to `On`. \n",
591+
"> 🚨 If you are using a pre-existing Cloud SQL instance within this notebook you may need to [configure Cloud SQL instance to allow IAM authentication](https://cloud.google.com/sql/docs/mysql/create-edit-iam-instances#configuring_existing_instances_for) by setting the `cloudsql_iam_authentication` database flag to `On`.\n",
592592
"(Cloud SQL instances created within this notebook already have it enabled)\n"
593593
]
594594
},
@@ -689,7 +689,7 @@
689689
"# connect to connection pool\n",
690690
"with pool.connect() as db_conn:\n",
691691
" # get current datetime from database\n",
692-
" results = db_conn.execute(\"SELECT NOW()\").fetchone()\n",
692+
" results = db_conn.execute(sqlalchemy.text(\"SELECT NOW()\")).fetchone()\n",
693693
"\n",
694694
" # output time\n",
695695
" print(\"Current time: \", results[0])\n",
@@ -715,7 +715,7 @@
715715
"cell_type": "markdown",
716716
"source": [
717717
"## 🗑 Clean Up Notebook Resources\n",
718-
"Make sure to delete your Cloud SQL instance when your are finished with this notebook to avoid further costs. 💸 💰 "
718+
"Make sure to delete your Cloud SQL instance when your are finished with this notebook to avoid further costs. 💸 💰"
719719
],
720720
"metadata": {
721721
"id": "2giQFUUCttsK"
@@ -796,7 +796,7 @@
796796
"# connect to connection pool\n",
797797
"with pool.connect() as db_conn:\n",
798798
" # query database and fetch results\n",
799-
" results = db_conn.execute(\"SELECT * FROM ratings\").fetchall()\n",
799+
" results = db_conn.execute(sqlalchemy.text(\"SELECT * FROM ratings\")).fetchall()\n",
800800
"\n",
801801
" # show results\n",
802802
" for row in results:\n",
@@ -809,4 +809,4 @@
809809
"outputs": []
810810
}
811811
]
812-
}
812+
}

0 commit comments

Comments
 (0)