|
1 | | -# upsolver-sdk-python |
| 1 | +# Using Upsolver with DBAPI in python |
2 | 2 |
|
| 3 | +## What is Upsolver |
| 4 | + |
| 5 | +[Upsolver](https://upsolver.com) enables you to use familiar SQL syntax to quickly build and deploy data pipelines, powered by a stream processing engine designed for cloud data lakes. |
| 6 | + |
| 7 | +## SQLake |
| 8 | + |
| 9 | +[SQLake](https://docs.upsolver.com/sqlake) is Upsolvers new UI and SQL console allowing to execute commands and monitor pipelines in the UI. It also includes freee trial and access to variety of examples and tutorials. |
| 10 | + |
| 11 | + |
| 12 | +## What is DB API |
| 13 | + |
| 14 | +Python's DB API 2.0 is defined in [pep-249](https://peps.python.org/pep-0249/). It defines an abstract API for connecting and working with databases in Python. Many python libraries support DB API v2.0 natively, for example `pandas`, `SQLAlchemy`, and more. |
| 15 | + |
| 16 | +## Getting started |
| 17 | + |
| 18 | +### Install Upsolver SDK for Python |
| 19 | + |
| 20 | +To use Upsolver SDK for Python you'll need Python interpreter of version greater than 3.7 |
| 21 | + |
| 22 | +```bash |
| 23 | +# For release version: |
| 24 | +pip install upsolver-sdk-python |
| 25 | +# for latest development version |
| 26 | +pip install git+https://github.com/Upsolver/upsolver-sdk-python |
| 27 | +``` |
| 28 | + |
| 29 | +### Register Upsolver account |
| 30 | + |
| 31 | +To register just navigate to [SQL Lake Sign Up form](https://sqlake.upsolver.com/signup). You'll have access to SQL workbench with examples and tutorials after completing the registration. |
| 32 | + |
| 33 | +### Create API token |
| 34 | + |
| 35 | +After login navigate to "[Settings](https://sqlake.upsolver.com/Settings)" and then to "[API Tokens](https://sqlake.upsolver.com/Settings/api-tokens)" |
| 36 | + |
| 37 | +You will need API token and API Url to access Upsolver programatically. |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +Then click "Generate" new token and save it for future use. |
| 42 | + |
| 43 | +## Connections and cursors |
| 44 | + |
| 45 | +Connecting to SQLake using the python SDK involves a few simple steps: |
| 46 | + |
| 47 | +- create a `Connection` |
| 48 | +- create a `Cursor` |
| 49 | +- execute query |
| 50 | + |
| 51 | +```python |
| 52 | +# import upsolver DB API |
| 53 | +import upsolver.dbapi as upsolver |
| 54 | + |
| 55 | +# Configure your token and URL |
| 56 | +token=... |
| 57 | +api_url=... |
| 58 | + |
| 59 | +#create connection and cursor |
| 60 | +con = upsolver.connect(token=token,api_url=api_url) |
| 61 | +cur = upsolver.Cursor(con) |
| 62 | + |
| 63 | +# execute query |
| 64 | +res = cur.execute(''' |
| 65 | + select |
| 66 | + customer.firstname, |
| 67 | + customer.lastname, |
| 68 | + nettotal as total, |
| 69 | + taxrate |
| 70 | + from default_glue_catalog.database_8edc49.orders_raw_data |
| 71 | + limit 5; |
| 72 | +''') |
| 73 | + |
| 74 | +# now we can iterate the results |
| 75 | +for r in res: |
| 76 | + print(r) |
| 77 | + |
| 78 | +['John', 'Williams', '415.04', '0.12'] |
| 79 | +['Richard', 'Miller', '842.1', '0.12'] |
| 80 | +['Charles', 'Martinez', '1994.6', '0.12'] |
| 81 | +['Roy', 'Hughes', '0.0', '0.12'] |
| 82 | +['Teresa', 'Reed', '1080.72', '0.12'] |
| 83 | +``` |
| 84 | + |
| 85 | +We can use libraries to print the pretty-print the results: |
| 86 | + |
| 87 | +```python |
| 88 | +from beautifultable import BeautifulTable |
| 89 | + |
| 90 | +res = cur.execute(''' |
| 91 | + select |
| 92 | + customer.firstname, |
| 93 | + customer.lastname, |
| 94 | + nettotal as total, |
| 95 | + taxrate |
| 96 | + from default_glue_catalog.database_8edc49.orders_raw_data |
| 97 | + limit 5; |
| 98 | +''') |
| 99 | + |
| 100 | +table = BeautifulTable() |
| 101 | +table.column_headers = [c[0] for c in cur.description] |
| 102 | +for r in res: |
| 103 | + table.append_row(r) |
| 104 | +print(table) |
| 105 | ++-----------+----------+---------+---------+ |
| 106 | +| firstname | lastname | total | taxrate | |
| 107 | ++-----------+----------+---------+---------+ |
| 108 | +| Samantha | Green | 607.53 | 0.12 | |
| 109 | ++-----------+----------+---------+---------+ |
| 110 | +| Virginia | Evans | 270.02 | 0.12 | |
| 111 | ++-----------+----------+---------+---------+ |
| 112 | +| Abigail | Watson | 1194.39 | 0.12 | |
| 113 | ++-----------+----------+---------+---------+ |
| 114 | +| Ann | Bailey | 1655.7 | 0.12 | |
| 115 | ++-----------+----------+---------+---------+ |
| 116 | +| Kelly | Edwards | 1368.78 | 0.12 | |
| 117 | ++-----------+----------+---------+---------+ |
| 118 | +``` |
| 119 | + |
| 120 | +Note: The examples above use the sample data provided by the template "S3 to Athena" in SQLake |
| 121 | + |
| 122 | +## We can use pandas too |
| 123 | + |
| 124 | +`pandas` is very popular library for data maipulations. |
| 125 | +It's possible to rewrite the above example with pandas |
| 126 | + |
| 127 | +```python |
| 128 | +import pandas as pd |
| 129 | + |
| 130 | +df = pd.read_sql(query,con=con) |
| 131 | +df.info() |
| 132 | +<class 'pandas.core.frame.DataFrame'> |
| 133 | +RangeIndex: 5 entries, 0 to 4 |
| 134 | +Data columns (total 4 columns): |
| 135 | + # Column Non-Null Count Dtype |
| 136 | +--- ------ -------------- ----- |
| 137 | + 0 firstname 5 non-null object |
| 138 | + 1 lastname 5 non-null object |
| 139 | + 2 total 5 non-null object |
| 140 | + 3 taxrate 5 non-null object |
| 141 | +dtypes: object(4) |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +## Upsolver SQL |
| 147 | + |
| 148 | +See Upsolver's [SQL Command Reference](https://docs.upsolver.com/sqlake/sql-command-reference) for the supported SQL commands and syntax. |
| 149 | + |
| 150 | +## Further reading |
| 151 | + |
| 152 | +[upsolver.com](https://upsolver.com) |
| 153 | + |
| 154 | +[Documentation](https://docs.upsolver.com/sqlake/sql-command-reference) of Upsolver SQL |
| 155 | + |
| 156 | +[upsolver-sdk-python](https://github.com/Upsolver/upsolver-sdk-python) - GitHub repository with upsolver SDK for Python language |
| 157 | + |
| 158 | +[SQLake workbench](https://sqlake.upsolver.com/) main page |
| 159 | + |
| 160 | +[Python examples from this README](doc/dbapi-ex.py) |
0 commit comments