Skip to main content

Using Upsolver with DBAPI in python

What is Upsolver

Upsolver 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.

SQLake

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.

What is DB API

Python's DB API 2.0 is defined in pep-249. 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.

Getting started

Install Upsolver SDK for Python

To use Upsolver SDK for Python you'll need Python interpreter of version greater than 3.7

# For release version:
pip install upsolver-sdk-python
# for latest development version
pip install git+https://github.com/Upsolver/upsolver-sdk-python

Register Upsolver account

To register just navigate to SQL Lake Sign Up form. You'll have access to SQL workbench with examples and tutorials after completing the registration.

Create API token

After login navigate to "Settings" and then to "API Tokens"

You will need API token and API Url to access Upsolver programatically.

API Tokens screen

Then click "Generate" new token and save it for future use.

Connections and cursors

Connecting to SQLake using the python SDK involves a few simple steps:

  • create a Connection
  • create a Cursor
  • execute query
# import upsolver DB API
import upsolver.dbapi as upsolver

# Configure your token and URL
token=...
api_url=...

#create connection and cursor
con = upsolver.connect(token=token,api_url=api_url)
cur = upsolver.Cursor(con)

# execute query
res = cur.execute('''
        select
            customer.firstname,
            customer.lastname,
            nettotal as total,
            taxrate
        from default_glue_catalog.database_8edc49.orders_raw_data
        limit 5;
''')

# now we can iterate the results
for r in res:
    print(r)

['John', 'Williams', '415.04', '0.12']
['Richard', 'Miller', '842.1', '0.12']
['Charles', 'Martinez', '1994.6', '0.12']
['Roy', 'Hughes', '0.0', '0.12']
['Teresa', 'Reed', '1080.72', '0.12']

We can use libraries to print the pretty-print the results:

from beautifultable import BeautifulTable

res = cur.execute('''
        select
            customer.firstname,
            customer.lastname,
            nettotal as total,
            taxrate
        from default_glue_catalog.database_8edc49.orders_raw_data
        limit 5;
''')

table = BeautifulTable()
table.column_headers = [c[0] for c in cur.description]
for r in res:
    table.append_row(r)
print(table)
+-----------+----------+---------+---------+
| firstname | lastname |  total  | taxrate |
+-----------+----------+---------+---------+
| Samantha  |  Green   | 607.53  |  0.12   |
+-----------+----------+---------+---------+
| Virginia  |  Evans   | 270.02  |  0.12   |
+-----------+----------+---------+---------+
|  Abigail  |  Watson  | 1194.39 |  0.12   |
+-----------+----------+---------+---------+
|    Ann    |  Bailey  | 1655.7  |  0.12   |
+-----------+----------+---------+---------+
|   Kelly   | Edwards  | 1368.78 |  0.12   |
+-----------+----------+---------+---------+

Note: The examples above use the sample data provided by the template "S3 to Athena" in SQLake

We can use pandas too

pandas is very popular library for data maipulations. It's possible to rewrite the above example with pandas

import pandas as pd

df = pd.read_sql(query,con=con)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
 #   Column     Non-Null Count  Dtype 
---  ------     --------------  ----- 
 0   firstname  5 non-null      object
 1   lastname   5 non-null      object
 2   total      5 non-null      object
 3   taxrate    5 non-null      object
dtypes: object(4)

df.head()

Upsolver SQL

See Upsolver's SQL Command Reference for the supported SQL commands and syntax.

Further reading

upsolver.com

Documentation of Upsolver SQL

upsolver-sdk-python - GitHub repository with upsolver SDK for Python language

SQLake workbench main page

Python examples from this README

Upsolver Comunity Slack

Metadata

Release files for upsolver-sdk-python 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for upsolver-sdk-python 0.1.12
File Size Uploaded
upsolver_sdk_python-0.1.12.tar.gz 16.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for upsolver-sdk-python 0.1.12
File Interpreter ABI Platform
upsolver_sdk_python-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 37.5 kB

Release files / upsolver_sdk_python-0.1.12.tar.gz

Download URL upsolver_sdk_python-0.1.12.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0c630d6864bb73dc93542d12f87eb4212a09d45225d53ed07ae6e29bf8731400
BLAKE2b-256 checksum
How to use checksums
e2e43ffad780fdda73aa4e6560d0d7043876f0299a3b69fa001856b14c004028
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / upsolver_sdk_python-0.1.12-py3-none-any.whl

Download URL upsolver_sdk_python-0.1.12-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb3f8060b446e812440238629e5ea88bd26560809a4bf8e99063cb1e91a96bcc
BLAKE2b-256 checksum
How to use checksums
7c53214408e11e3b1e3878290573641dab0d508c146519386b1166ddf4ae50fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page