From 816daef6005a58e368aad56bc64da2cf87882c03 Mon Sep 17 00:00:00 2001 From: Oleg Ovcharuk Date: Wed, 6 Nov 2024 11:11:14 +0300 Subject: [PATCH] notebook example --- .../integrations/sqlalchemy example.ipynb | 862 ++++++++++++++++++ 1 file changed, 862 insertions(+) create mode 100644 examples/integrations/sqlalchemy example.ipynb diff --git a/examples/integrations/sqlalchemy example.ipynb b/examples/integrations/sqlalchemy example.ipynb new file mode 100644 index 00000000..07f0eea1 --- /dev/null +++ b/examples/integrations/sqlalchemy example.ipynb @@ -0,0 +1,862 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "86850a2b-3753-4de5-99bd-eccc834c3b26", + "metadata": {}, + "source": [ + "### Install dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "baab9af8-190d-49c6-b159-4905bae6a830", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Processing /Users/ovcharuk/work/ydb-sqlalchemy/dist/ydb_sqlalchemy-0.0.1b23-py2.py3-none-any.whl\n", + "Collecting sqlalchemy<3.0.0,>=2.0.7 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached SQLAlchemy-2.0.36-cp39-cp39-macosx_11_0_arm64.whl.metadata (9.7 kB)\n", + "Collecting ydb>=3.18.8 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached ydb-3.18.8-py2.py3-none-any.whl.metadata (2.1 kB)\n", + "Collecting ydb-dbapi>=0.0.1b8 (from ydb-sqlalchemy==0.0.1b23)\n", + " Using cached ydb_dbapi-0.0.1b8-py3-none-any.whl.metadata (1.7 kB)\n", + "Collecting typing-extensions>=4.6.0 (from sqlalchemy<3.0.0,>=2.0.7->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)\n", + "Collecting grpcio>=1.42.0 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached grpcio-1.67.1-cp39-cp39-macosx_10_9_universal2.whl.metadata (3.9 kB)\n", + "Collecting packaging (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached packaging-24.1-py3-none-any.whl.metadata (3.2 kB)\n", + "Collecting protobuf<5.0.0,>=3.13.0 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached protobuf-4.25.5-cp37-abi3-macosx_10_9_universal2.whl.metadata (541 bytes)\n", + "Collecting aiohttp<4 (from ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiohttp-3.10.10-cp39-cp39-macosx_11_0_arm64.whl.metadata (7.6 kB)\n", + "Collecting aiohappyeyeballs>=2.3.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiohappyeyeballs-2.4.3-py3-none-any.whl.metadata (6.1 kB)\n", + "Collecting aiosignal>=1.1.2 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached aiosignal-1.3.1-py3-none-any.whl.metadata (4.0 kB)\n", + "Collecting attrs>=17.3.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached attrs-24.2.0-py3-none-any.whl.metadata (11 kB)\n", + "Collecting frozenlist>=1.1.1 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached frozenlist-1.5.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (13 kB)\n", + "Collecting multidict<7.0,>=4.5 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached multidict-6.1.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (5.0 kB)\n", + "Collecting yarl<2.0,>=1.12.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached yarl-1.17.1-cp39-cp39-macosx_11_0_arm64.whl.metadata (64 kB)\n", + "Collecting async-timeout<5.0,>=4.0 (from aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached async_timeout-4.0.3-py3-none-any.whl.metadata (4.2 kB)\n", + "Collecting idna>=2.0 (from yarl<2.0,>=1.12.0->aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n", + "Collecting propcache>=0.2.0 (from yarl<2.0,>=1.12.0->aiohttp<4->ydb>=3.18.8->ydb-sqlalchemy==0.0.1b23)\n", + " Using cached propcache-0.2.0-cp39-cp39-macosx_11_0_arm64.whl.metadata (7.7 kB)\n", + "Using cached SQLAlchemy-2.0.36-cp39-cp39-macosx_11_0_arm64.whl (2.1 MB)\n", + "Using cached ydb-3.18.8-py2.py3-none-any.whl (793 kB)\n", + "Using cached ydb_dbapi-0.0.1b8-py3-none-any.whl (14 kB)\n", + "Using cached aiohttp-3.10.10-cp39-cp39-macosx_11_0_arm64.whl (391 kB)\n", + "Using cached grpcio-1.67.1-cp39-cp39-macosx_10_9_universal2.whl (11.0 MB)\n", + "Using cached protobuf-4.25.5-cp37-abi3-macosx_10_9_universal2.whl (394 kB)\n", + "Using cached typing_extensions-4.12.2-py3-none-any.whl (37 kB)\n", + "Using cached packaging-24.1-py3-none-any.whl (53 kB)\n", + "Using cached aiohappyeyeballs-2.4.3-py3-none-any.whl (14 kB)\n", + "Using cached aiosignal-1.3.1-py3-none-any.whl (7.6 kB)\n", + "Using cached async_timeout-4.0.3-py3-none-any.whl (5.7 kB)\n", + "Using cached attrs-24.2.0-py3-none-any.whl (63 kB)\n", + "Using cached frozenlist-1.5.0-cp39-cp39-macosx_11_0_arm64.whl (52 kB)\n", + "Using cached multidict-6.1.0-cp39-cp39-macosx_11_0_arm64.whl (29 kB)\n", + "Using cached yarl-1.17.1-cp39-cp39-macosx_11_0_arm64.whl (91 kB)\n", + "Using cached idna-3.10-py3-none-any.whl (70 kB)\n", + "Using cached propcache-0.2.0-cp39-cp39-macosx_11_0_arm64.whl (46 kB)\n", + "Installing collected packages: typing-extensions, protobuf, propcache, packaging, idna, grpcio, frozenlist, attrs, async-timeout, aiohappyeyeballs, sqlalchemy, multidict, aiosignal, yarl, aiohttp, ydb, ydb-dbapi, ydb-sqlalchemy\n", + " Attempting uninstall: typing-extensions\n", + " Found existing installation: typing_extensions 4.12.2\n", + " Uninstalling typing_extensions-4.12.2:\n", + " Successfully uninstalled typing_extensions-4.12.2\n", + " Attempting uninstall: protobuf\n", + " Found existing installation: protobuf 4.25.5\n", + " Uninstalling protobuf-4.25.5:\n", + " Successfully uninstalled protobuf-4.25.5\n", + " Attempting uninstall: propcache\n", + " Found existing installation: propcache 0.2.0\n", + " Uninstalling propcache-0.2.0:\n", + " Successfully uninstalled propcache-0.2.0\n", + " Attempting uninstall: packaging\n", + " Found existing installation: packaging 24.1\n", + " Uninstalling packaging-24.1:\n", + " Successfully uninstalled packaging-24.1\n", + " Attempting uninstall: idna\n", + " Found existing installation: idna 3.10\n", + " Uninstalling idna-3.10:\n", + " Successfully uninstalled idna-3.10\n", + " Attempting uninstall: grpcio\n", + " Found existing installation: grpcio 1.67.1\n", + " Uninstalling grpcio-1.67.1:\n", + " Successfully uninstalled grpcio-1.67.1\n", + " Attempting uninstall: frozenlist\n", + " Found existing installation: frozenlist 1.5.0\n", + " Uninstalling frozenlist-1.5.0:\n", + " Successfully uninstalled frozenlist-1.5.0\n", + " Attempting uninstall: attrs\n", + " Found existing installation: attrs 24.2.0\n", + " Uninstalling attrs-24.2.0:\n", + " Successfully uninstalled attrs-24.2.0\n", + " Attempting uninstall: async-timeout\n", + " Found existing installation: async-timeout 4.0.3\n", + " Uninstalling async-timeout-4.0.3:\n", + " Successfully uninstalled async-timeout-4.0.3\n", + " Attempting uninstall: aiohappyeyeballs\n", + " Found existing installation: aiohappyeyeballs 2.4.3\n", + " Uninstalling aiohappyeyeballs-2.4.3:\n", + " Successfully uninstalled aiohappyeyeballs-2.4.3\n", + " Attempting uninstall: sqlalchemy\n", + " Found existing installation: SQLAlchemy 2.0.36\n", + " Uninstalling SQLAlchemy-2.0.36:\n", + " Successfully uninstalled SQLAlchemy-2.0.36\n", + " Attempting uninstall: multidict\n", + " Found existing installation: multidict 6.1.0\n", + " Uninstalling multidict-6.1.0:\n", + " Successfully uninstalled multidict-6.1.0\n", + " Attempting uninstall: aiosignal\n", + " Found existing installation: aiosignal 1.3.1\n", + " Uninstalling aiosignal-1.3.1:\n", + " Successfully uninstalled aiosignal-1.3.1\n", + " Attempting uninstall: yarl\n", + " Found existing installation: yarl 1.17.1\n", + " Uninstalling yarl-1.17.1:\n", + " Successfully uninstalled yarl-1.17.1\n", + " Attempting uninstall: aiohttp\n", + " Found existing installation: aiohttp 3.10.10\n", + " Uninstalling aiohttp-3.10.10:\n", + " Successfully uninstalled aiohttp-3.10.10\n", + " Attempting uninstall: ydb\n", + " Found existing installation: ydb 3.18.8\n", + " Uninstalling ydb-3.18.8:\n", + " Successfully uninstalled ydb-3.18.8\n", + " Attempting uninstall: ydb-dbapi\n", + " Found existing installation: ydb-dbapi 0.0.1b8\n", + " Uninstalling ydb-dbapi-0.0.1b8:\n", + " Successfully uninstalled ydb-dbapi-0.0.1b8\n", + " Attempting uninstall: ydb-sqlalchemy\n", + " Found existing installation: ydb-sqlalchemy 0.0.1b23\n", + " Uninstalling ydb-sqlalchemy-0.0.1b23:\n", + " Successfully uninstalled ydb-sqlalchemy-0.0.1b23\n", + "Successfully installed aiohappyeyeballs-2.4.3 aiohttp-3.10.10 aiosignal-1.3.1 async-timeout-4.0.3 attrs-24.2.0 frozenlist-1.5.0 grpcio-1.67.1 idna-3.10 multidict-6.1.0 packaging-24.1 propcache-0.2.0 protobuf-4.25.5 sqlalchemy-2.0.36 typing-extensions-4.12.2 yarl-1.17.1 ydb-3.18.8 ydb-dbapi-0.0.1b8 ydb-sqlalchemy-0.0.1b23\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "# Change into ydb-sqlalchemy from pypi after release\n", + "%pip install --force-reinstall /Users/ovcharuk/work/ydb-sqlalchemy/dist/ydb_sqlalchemy-0.0.1b23-py2.py3-none-any.whl" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1bac5cad-31e1-4dcb-a8c9-5cac24899220", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pandas in ./.venv/lib/python3.9/site-packages (2.2.3)\n", + "Requirement already satisfied: jupysql in ./.venv/lib/python3.9/site-packages (0.10.14)\n", + "Requirement already satisfied: matplotlib in ./.venv/lib/python3.9/site-packages (3.9.2)\n", + "Requirement already satisfied: numpy>=1.22.4 in ./.venv/lib/python3.9/site-packages (from pandas) (2.0.2)\n", + "Requirement already satisfied: python-dateutil>=2.8.2 in ./.venv/lib/python3.9/site-packages (from pandas) (2.9.0.post0)\n", + "Requirement already satisfied: pytz>=2020.1 in ./.venv/lib/python3.9/site-packages (from pandas) (2024.2)\n", + "Requirement already satisfied: tzdata>=2022.7 in ./.venv/lib/python3.9/site-packages (from pandas) (2024.2)\n", + "Requirement already satisfied: prettytable in ./.venv/lib/python3.9/site-packages (from jupysql) (3.12.0)\n", + "Requirement already satisfied: sqlalchemy in ./.venv/lib/python3.9/site-packages (from jupysql) (2.0.36)\n", + "Requirement already satisfied: sqlparse in ./.venv/lib/python3.9/site-packages (from jupysql) (0.5.1)\n", + "Requirement already satisfied: ipython-genutils>=0.1.0 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.2.0)\n", + "Requirement already satisfied: jinja2 in ./.venv/lib/python3.9/site-packages (from jupysql) (3.1.4)\n", + "Requirement already satisfied: sqlglot>=11.3.7 in ./.venv/lib/python3.9/site-packages (from jupysql) (25.28.0)\n", + "Requirement already satisfied: jupysql-plugin>=0.4.2 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.4.5)\n", + "Requirement already satisfied: ploomber-core>=0.2.7 in ./.venv/lib/python3.9/site-packages (from jupysql) (0.2.25)\n", + "Requirement already satisfied: contourpy>=1.0.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (1.3.0)\n", + "Requirement already satisfied: cycler>=0.10 in ./.venv/lib/python3.9/site-packages (from matplotlib) (0.12.1)\n", + "Requirement already satisfied: fonttools>=4.22.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (4.54.1)\n", + "Requirement already satisfied: kiwisolver>=1.3.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (1.4.7)\n", + "Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (24.1)\n", + "Requirement already satisfied: pillow>=8 in ./.venv/lib/python3.9/site-packages (from matplotlib) (11.0.0)\n", + "Requirement already satisfied: pyparsing>=2.3.1 in ./.venv/lib/python3.9/site-packages (from matplotlib) (3.2.0)\n", + "Requirement already satisfied: importlib-resources>=3.2.0 in ./.venv/lib/python3.9/site-packages (from matplotlib) (6.4.5)\n", + "Requirement already satisfied: zipp>=3.1.0 in ./.venv/lib/python3.9/site-packages (from importlib-resources>=3.2.0->matplotlib) (3.20.2)\n", + "Requirement already satisfied: pyyaml in ./.venv/lib/python3.9/site-packages (from ploomber-core>=0.2.7->jupysql) (6.0.2)\n", + "Requirement already satisfied: posthog in ./.venv/lib/python3.9/site-packages (from ploomber-core>=0.2.7->jupysql) (3.7.0)\n", + "Requirement already satisfied: six>=1.5 in ./.venv/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.9/site-packages (from jinja2->jupysql) (3.0.2)\n", + "Requirement already satisfied: wcwidth in ./.venv/lib/python3.9/site-packages (from prettytable->jupysql) (0.2.13)\n", + "Requirement already satisfied: typing-extensions>=4.6.0 in ./.venv/lib/python3.9/site-packages (from sqlalchemy->jupysql) (4.12.2)\n", + "Requirement already satisfied: requests<3.0,>=2.7 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (2.32.3)\n", + "Requirement already satisfied: monotonic>=1.5 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (1.6)\n", + "Requirement already satisfied: backoff>=1.10.0 in ./.venv/lib/python3.9/site-packages (from posthog->ploomber-core>=0.2.7->jupysql) (2.2.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (3.4.0)\n", + "Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (2.2.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.9/site-packages (from requests<3.0,>=2.7->posthog->ploomber-core>=0.2.7->jupysql) (2024.8.30)\n", + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install pandas jupysql matplotlib" + ] + }, + { + "cell_type": "markdown", + "id": "b9b9db69-80a9-4ebb-b80c-18041ea01e37", + "metadata": {}, + "source": [ + "## Actions with YDB" + ] + }, + { + "cell_type": "markdown", + "id": "c025c448-f6f9-48a1-8926-3a67cf29f81e", + "metadata": {}, + "source": [ + "### Load DataFrame to work with" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "009110c9-10b1-4643-9609-a7d3dcc90529", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idsexageincomespending_score
01Male191539
12Male211581
23Female20166
34Female231677
45Female311740
..................
195196Female3512079
196197Female4512628
197198Male3212674
198199Male3213718
199200Male3013783
\n", + "

200 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " id sex age income spending_score\n", + "0 1 Male 19 15 39\n", + "1 2 Male 21 15 81\n", + "2 3 Female 20 16 6\n", + "3 4 Female 23 16 77\n", + "4 5 Female 31 17 40\n", + ".. ... ... ... ... ...\n", + "195 196 Female 35 120 79\n", + "196 197 Female 45 126 28\n", + "197 198 Male 32 126 74\n", + "198 199 Male 32 137 18\n", + "199 200 Male 30 137 83\n", + "\n", + "[200 rows x 5 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.read_csv('/Users/ovcharuk/work/ydb-python-examples/Mall_Customers.csv')\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "a85c8ff8-ea5b-45f0-8dfb-49f80530859d", + "metadata": {}, + "source": [ + "### Create SA Engine and connect" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0a04e4f2-dfa7-49d0-ae63-0111729712fa", + "metadata": {}, + "outputs": [], + "source": [ + "import sqlalchemy as sa\n", + "engine = sa.create_engine(\"yql+ydb://localhost:2136/local\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d6afc781-a4a0-4b87-a98d-ca898089c55f", + "metadata": {}, + "outputs": [], + "source": [ + "conn = engine.connect()" + ] + }, + { + "cell_type": "markdown", + "id": "c50ff1a7-d596-46d0-91b2-0351bd8e20ad", + "metadata": {}, + "source": [ + "### Prepare database to load dataframe into\n", + "There's no way to specify PK directly from `df.to_sql`, and `YDB` does not support tables without PK, so we have to prepare database first" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "625931ec-2502-4eda-b883-75ae60055acf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "conn.execute(sa.text(\"DROP TABLE IF EXISTS mall_customers\"))\n", + "conn.execute(sa.text(\n", + " \"\"\"\n", + " CREATE TABLE mall_customers (\n", + "\t`id` Int64, \n", + "\t`sex` UTF8, \n", + "\t`age` Int64, \n", + "\t`income` Int64, \n", + "\t`spending_score` Int64,\n", + " primary key (id)\n", + " )\n", + " \"\"\"\n", + "))" + ] + }, + { + "cell_type": "markdown", + "id": "76392855-0108-4c88-a884-e5bf225f7925", + "metadata": {}, + "source": [ + "### Load dataframe into YDB" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9db9b0b0-da6d-46b3-9060-80788ef027ed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.to_sql(\"mall_customers\", conn, if_exists='append', index=False)" + ] + }, + { + "cell_type": "markdown", + "id": "e46e9e2e-6319-4806-95eb-7f5080bf63db", + "metadata": {}, + "source": [ + "### Read YDB table into DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "7603f892-4fa6-4d5d-8952-0e55c3266b39", + "metadata": {}, + "outputs": [], + "source": [ + "new_df = pd.read_sql(\"SELECT * FROM mall_customers\", conn)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "929f398c-b321-473f-a701-594f67c704d8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ageidincomesexspending_score
019115Male39
121215Male81
220316Female6
323416Female77
431517Female40
..................
19535196120Female79
19645197126Female28
19732198126Male74
19832199137Male18
19930200137Male83
\n", + "

200 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " age id income sex spending_score\n", + "0 19 1 15 Male 39\n", + "1 21 2 15 Male 81\n", + "2 20 3 16 Female 6\n", + "3 23 4 16 Female 77\n", + "4 31 5 17 Female 40\n", + ".. ... ... ... ... ...\n", + "195 35 196 120 Female 79\n", + "196 45 197 126 Female 28\n", + "197 32 198 126 Male 74\n", + "198 32 199 137 Male 18\n", + "199 30 200 137 Male 83\n", + "\n", + "[200 rows x 5 columns]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_df" + ] + }, + { + "cell_type": "markdown", + "id": "532931a6-1cb0-43f6-b321-df2c897088e7", + "metadata": {}, + "source": [ + "### Using `sql` magic to work with YDB" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1af48d58-8970-4f6b-976d-0f2ffb81f914", + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext sql" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "186cd040-f100-4572-ba2d-3f1691a11ffc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "Connecting to 'yql+ydb://localhost:2136/local'" + ], + "text/plain": [ + "Connecting to 'yql+ydb://localhost:2136/local'" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%sql yql+ydb://localhost:2136/local" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "a180403e-2bab-41b4-a2b7-6c97c5a1f60b", + "metadata": {}, + "outputs": [], + "source": [ + "%config SqlMagic.style = '_DEPRECATED_DEFAULT'\n", + "# https://community.deeplearning.ai/t/error-while-querying-db-from-jupyter-notebook/716351" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "6a3465f4-be68-45e1-8886-1e6837de8915", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "Running query in 'yql+ydb://localhost:2136/local'" + ], + "text/plain": [ + "Running query in 'yql+ydb://localhost:2136/local'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "138 rows affected." + ], + "text/plain": [ + "138 rows affected." + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sexspending_score
Female40
Female6
Male3
Male14
Female99
Female15
Male13
Female35
Male29
Female98
\n", + "Truncated to displaylimit of 10." + ], + "text/plain": [ + "+--------+----------------+\n", + "| sex | spending_score |\n", + "+--------+----------------+\n", + "| Female | 40 |\n", + "| Female | 6 |\n", + "| Male | 3 |\n", + "| Male | 14 |\n", + "| Female | 99 |\n", + "| Female | 15 |\n", + "| Male | 13 |\n", + "| Female | 35 |\n", + "| Male | 29 |\n", + "| Female | 98 |\n", + "+--------+----------------+\n", + "Truncated to displaylimit of 10." + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%%sql\n", + "SELECT sex, spending_score \n", + " FROM mall_customers\n", + " WHERE age > 30" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "706d17da-976d-46e6-9dce-7e09ead413c8", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.19" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}