|
| 1 | +########################### |
| 2 | +Using CrateDB with turbodbc |
| 3 | +########################### |
| 4 | + |
| 5 | + |
| 6 | +***** |
| 7 | +About |
| 8 | +***** |
| 9 | + |
| 10 | +This section of the documentation describes how to connect to `CrateDB`_ |
| 11 | +with `turbodbc`_, by providing a few example programs. |
| 12 | + |
| 13 | +The examples use the `unixODBC`_ implementation of `ODBC`_, and the `PostgreSQL |
| 14 | +ODBC driver`_, for connecting to the `PostgreSQL wire protocol`_ interface of |
| 15 | +`CrateDB`_. |
| 16 | + |
| 17 | +This folder also contains ``Dockerfile`` files providing environments to |
| 18 | +exercise the examples on different operating systems, like Arch Linux, |
| 19 | +Red Hat (CentOS), Debian, and SUSE Linux. |
| 20 | + |
| 21 | + |
| 22 | +************ |
| 23 | +Introduction |
| 24 | +************ |
| 25 | + |
| 26 | +`Turbodbc`_ is a Python module to access relational databases via the `Open |
| 27 | +Database Connectivity (ODBC)`_ interface. In addition to complying with |
| 28 | +the `Python Database API Specification 2.0`_, turbodbc offers built-in `NumPy`_ |
| 29 | +and `Apache Arrow`_ support for improved performance. Their slogan is: |
| 30 | + |
| 31 | + Don’t wait minutes for your results, just blink. |
| 32 | + |
| 33 | +*Note: The description texts have been taken from turbodbc's documentation 1:1.* |
| 34 | + |
| 35 | +Description |
| 36 | +=========== |
| 37 | + |
| 38 | +Its primary target audience are data scientists that use databases for which no |
| 39 | +efficient native Python drivers are available. |
| 40 | + |
| 41 | +For maximum compatibility, turbodbc complies with the `Python Database API |
| 42 | +Specification 2.0`_ (PEP 249). For maximum performance, turbodbc internally |
| 43 | +relies on batched data transfer instead of single-record communication as |
| 44 | +other popular ODBC modules do. |
| 45 | + |
| 46 | +Why should I use turbodbc instead of other ODBC modules? |
| 47 | +======================================================== |
| 48 | + |
| 49 | +- Short answer: turbodbc is faster. |
| 50 | +- Slightly longer answer: turbodbc is faster, *much* faster if you want to |
| 51 | + work with NumPy. |
| 52 | +- Medium-length answer: The author has tested turbodbc and pyodbc (probably |
| 53 | + the most popular Python ODBC module) with various databases (Exasol, |
| 54 | + PostgreSQL, MySQL) and corresponding ODBC drivers. He found turbodbc to be |
| 55 | + consistently faster. |
| 56 | + |
| 57 | +Smooth. What is the trick? |
| 58 | +========================== |
| 59 | + |
| 60 | +Turbodbc exploits buffering. |
| 61 | + |
| 62 | +- Turbodbc implements both sending parameters and retrieving result sets using |
| 63 | + buffers of multiple rows/parameter sets. This avoids round trips to the ODBC |
| 64 | + driver and (depending how well the ODBC driver is written) to the database. |
| 65 | +- Multiple buffers are used for asynchronous I/O. This allows to interleave |
| 66 | + Python object conversion and direct database interaction (see performance |
| 67 | + options below). |
| 68 | +- Buffers contain binary representations of data. NumPy arrays contain binary |
| 69 | + representations of data. Good thing they are often the same, so instead of |
| 70 | + converting, the driver can just copy data. |
| 71 | + |
| 72 | + |
| 73 | +***** |
| 74 | +Setup |
| 75 | +***** |
| 76 | + |
| 77 | +Install prerequisites |
| 78 | +===================== |
| 79 | + |
| 80 | +Arch Linux:: |
| 81 | + |
| 82 | + # See `dockerfiles/archlinux.Dockerfile`. |
| 83 | + |
| 84 | +CentOS Stream:: |
| 85 | + |
| 86 | + dnf install --enablerepo=crb -y boost-devel g++ postgresql-odbc python3 python3-devel python3-pip unixODBC-devel |
| 87 | + |
| 88 | +Debian:: |
| 89 | + |
| 90 | + apt-get install --yes build-essential libboost-dev odbc-postgresql unixodbc-dev |
| 91 | + |
| 92 | +macOS/Homebrew:: |
| 93 | + |
| 94 | + brew install psqlodbc unixodbc |
| 95 | + |
| 96 | +SUSE Linux Enterprise Server:: |
| 97 | + |
| 98 | + # See `dockerfiles/sles.Dockerfile`. |
| 99 | + |
| 100 | +Install Python sandbox |
| 101 | +====================== |
| 102 | +:: |
| 103 | + |
| 104 | + # Create Python virtualenv and install dependency packages. |
| 105 | + python3 -m venv .venv |
| 106 | + source .venv/bin/activate |
| 107 | + pip install --upgrade --requirement=requirements-prereq.txt |
| 108 | + pip install --upgrade --requirement=requirements.txt --verbose |
| 109 | + |
| 110 | +.. note:: |
| 111 | + |
| 112 | + The `turbodbc pip installation documentation`_ says: |
| 113 | + Please ``pip install numpy`` before installing turbodbc, because turbodbc |
| 114 | + will search for the ``numpy`` Python package at installation/compile time. |
| 115 | + If NumPy is not installed, turbodbc will not compile the `NumPy |
| 116 | + support`_ features. Similarly, please ``pip install pyarrow`` before |
| 117 | + installing turbodbc if you would like to use the `Apache Arrow |
| 118 | + support`_. |
| 119 | + |
| 120 | + |
| 121 | +***** |
| 122 | +Usage |
| 123 | +***** |
| 124 | + |
| 125 | +Run CrateDB:: |
| 126 | + |
| 127 | + docker run --rm -it --publish=4200:4200 --publish=5432:5432 crate \ |
| 128 | + -Cdiscovery.type=single-node -Ccluster.routing.allocation.disk.threshold_enabled=false |
| 129 | + |
| 130 | +Invoke demo program on workstation:: |
| 131 | + |
| 132 | + python demo.py |
| 133 | + |
| 134 | +Exercise demo program using Docker, on different operating systems:: |
| 135 | + |
| 136 | + docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/archlinux.Dockerfile . |
| 137 | + docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/centos.Dockerfile . |
| 138 | + docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/debian.Dockerfile . |
| 139 | + docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/sles.Dockerfile . |
| 140 | + |
| 141 | + docker run --rm -it --volume=$(pwd):/src --network=host local/python-turbodbc-demo python3 /src/demo.py |
| 142 | + |
| 143 | + |
| 144 | +******* |
| 145 | +Backlog |
| 146 | +******* |
| 147 | + |
| 148 | +The patch just contains a basic example within ``demo.py``. Advanced usage |
| 149 | +examples to be exercised are tracked within the `backlog`_. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +.. _Apache Arrow: https://en.wikipedia.org/wiki/Apache_Arrow |
| 154 | +.. _Apache Arrow support: https://turbodbc.readthedocs.io/en/latest/pages/advanced_usage.html#advanced-usage-arrow |
| 155 | +.. _backlog: https://github.com/crate/cratedb-examples/blob/main/python-turbodbc/backlog.rst |
| 156 | +.. _CrateDB: https://github.com/crate/crate |
| 157 | +.. _NumPy: https://en.wikipedia.org/wiki/NumPy |
| 158 | +.. _NumPy support: https://turbodbc.readthedocs.io/en/latest/pages/advanced_usage.html#advanced-usage-numpy |
| 159 | +.. _ODBC: https://en.wikipedia.org/wiki/Open_Database_Connectivity |
| 160 | +.. _Open Database Connectivity (ODBC): https://en.wikipedia.org/wiki/Open_Database_Connectivity |
| 161 | +.. _PostgreSQL ODBC driver: https://odbc.postgresql.org/ |
| 162 | +.. _PostgreSQL wire protocol: https://crate.io/docs/crate/reference/en/latest/interfaces/postgres.html |
| 163 | +.. _Python Database API Specification 2.0: https://peps.python.org/pep-0249/ |
| 164 | +.. _turbodbc: https://turbodbc.readthedocs.io/ |
| 165 | +.. _turbodbc pip installation documentation: https://turbodbc.readthedocs.io/en/latest/pages/getting_started.html#pip |
| 166 | +.. _unixODBC: https://www.unixodbc.org/ |
0 commit comments