|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Extracting the data locally\n", |
| 8 | + "\n", |
| 9 | + "To statistically analyse the entire EUBUCCO database is too data-intensive for most local machines.\n", |
| 10 | + "\n", |
| 11 | + "To make the analysis possible on a local machine, this notebook downloads a semi-random sample of the database (currently 10%) with a reduced number of columns in chunks to parquet files. Note that the dbmods module needs to be updated with current IP Address of server, which often changes." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 3, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from src import dbmods" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 4, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "Server connected.\n", |
| 33 | + "{'database': 'eubucco', 'user': 'readonly', 'password': 'readonly', 'host': 'localhost', 'port': 49690, 'connect_timeout': 10}\n" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "name": "stderr", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "2023-11-12 19:28:33,046| ERROR | Could not establish connection from local ('127.0.0.1', 49690) to remote ('192.168.48.2', 5432) side of the tunnel: open new channel ssh error: Timeout opening channel.\n" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "ename": "OperationalError", |
| 45 | + "evalue": "connection to server at \"localhost\" (::1), port 49690 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?\nconnection to server at \"localhost\" (127.0.0.1), port 49690 failed: server closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n\tbefore or while processing the request.\n", |
| 46 | + "output_type": "error", |
| 47 | + "traceback": [ |
| 48 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 49 | + "\u001b[0;31mOperationalError\u001b[0m Traceback (most recent call last)", |
| 50 | + "\u001b[1;32m/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql copy.ipynb Cell 3\u001b[0m line \u001b[0;36m8\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m query \u001b[39m=\u001b[39m \u001b[39m'''\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m \u001b[39m select db.id, db.height, db.age, db.geometry\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m from data_building db\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39m tablesample system (10)\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m repeatable (22);\u001b[39m\n\u001b[1;32m <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39m \u001b[39m\u001b[39m'''\u001b[39m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/chrishedemann/coding/eubucco_data_quality/01a_extract-sql%20copy.ipynb#W2sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m dbmods\u001b[39m.\u001b[39;49mSQL_to_parquet(query,fpath\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m./data/parquet/sample_10percent_\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", |
| 51 | + "File \u001b[0;32m~/coding/eubucco_data_quality/src/dbmods.py:44\u001b[0m, in \u001b[0;36mSQL_to_parquet\u001b[0;34m(query, chunksize, fpath, chunk_per_file)\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[39mprint\u001b[39m(params)\n\u001b[1;32m 43\u001b[0m \u001b[39m# 2. connect to database and start query iterator\u001b[39;00m\n\u001b[0;32m---> 44\u001b[0m conn \u001b[39m=\u001b[39m psycopg2\u001b[39m.\u001b[39;49mconnect(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mparams)\n\u001b[1;32m 45\u001b[0m conn\u001b[39m.\u001b[39mset_session(readonly\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n\u001b[1;32m 47\u001b[0m data \u001b[39m=\u001b[39m gpd\u001b[39m.\u001b[39mread_postgis(\n\u001b[1;32m 48\u001b[0m sql\u001b[39m=\u001b[39mquery,\n\u001b[1;32m 49\u001b[0m con\u001b[39m=\u001b[39mconn,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 53\u001b[0m chunksize\u001b[39m=\u001b[39mchunksize,\n\u001b[1;32m 54\u001b[0m )\n", |
| 52 | + "File \u001b[0;32m~/coding/eubucco_data_quality/.venv/lib/python3.11/site-packages/psycopg2/__init__.py:122\u001b[0m, in \u001b[0;36mconnect\u001b[0;34m(dsn, connection_factory, cursor_factory, **kwargs)\u001b[0m\n\u001b[1;32m 119\u001b[0m kwasync[\u001b[39m'\u001b[39m\u001b[39masync_\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m kwargs\u001b[39m.\u001b[39mpop(\u001b[39m'\u001b[39m\u001b[39masync_\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m 121\u001b[0m dsn \u001b[39m=\u001b[39m _ext\u001b[39m.\u001b[39mmake_dsn(dsn, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m--> 122\u001b[0m conn \u001b[39m=\u001b[39m _connect(dsn, connection_factory\u001b[39m=\u001b[39;49mconnection_factory, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwasync)\n\u001b[1;32m 123\u001b[0m \u001b[39mif\u001b[39;00m cursor_factory \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 124\u001b[0m conn\u001b[39m.\u001b[39mcursor_factory \u001b[39m=\u001b[39m cursor_factory\n", |
| 53 | + "\u001b[0;31mOperationalError\u001b[0m: connection to server at \"localhost\" (::1), port 49690 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?\nconnection to server at \"localhost\" (127.0.0.1), port 49690 failed: server closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n\tbefore or while processing the request.\n" |
| 54 | + ] |
| 55 | + } |
| 56 | + ], |
| 57 | + "source": [ |
| 58 | + "\n", |
| 59 | + "query = '''\n", |
| 60 | + " select db.id, db.height, db.age, db.geometry\n", |
| 61 | + " from data_building db\n", |
| 62 | + " tablesample system (10)\n", |
| 63 | + " repeatable (22);\n", |
| 64 | + " '''\n", |
| 65 | + "\n", |
| 66 | + "dbmods.SQL_to_parquet(query,fpath=\"./data/parquet/sample_10percent_\")\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "metadata": { |
| 71 | + "kernelspec": { |
| 72 | + "display_name": "Python 3 (ipykernel)", |
| 73 | + "language": "python", |
| 74 | + "name": "python3" |
| 75 | + }, |
| 76 | + "language_info": { |
| 77 | + "codemirror_mode": { |
| 78 | + "name": "ipython", |
| 79 | + "version": 3 |
| 80 | + }, |
| 81 | + "file_extension": ".py", |
| 82 | + "mimetype": "text/x-python", |
| 83 | + "name": "python", |
| 84 | + "nbconvert_exporter": "python", |
| 85 | + "pygments_lexer": "ipython3", |
| 86 | + "version": "3.11.3" |
| 87 | + } |
| 88 | + }, |
| 89 | + "nbformat": 4, |
| 90 | + "nbformat_minor": 4 |
| 91 | +} |
0 commit comments