Skip to main content

Python SDK for Upsolver

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

upsolver_sdk_python-0.1.12.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

upsolver_sdk_python-0.1.12-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file upsolver_sdk_python-0.1.12.tar.gz.

File metadata

  • Download URL: upsolver_sdk_python-0.1.12.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for upsolver_sdk_python-0.1.12.tar.gz
Algorithm Hash digest
SHA256 0c630d6864bb73dc93542d12f87eb4212a09d45225d53ed07ae6e29bf8731400
MD5 a6d5176a3c597d327dc7c00a941c5ec0
BLAKE2b-256 e2e43ffad780fdda73aa4e6560d0d7043876f0299a3b69fa001856b14c004028

See more details on using hashes here.

File details

Details for the file upsolver_sdk_python-0.1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for upsolver_sdk_python-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 bb3f8060b446e812440238629e5ea88bd26560809a4bf8e99063cb1e91a96bcc
MD5 b68972ecb2194acb689469e6dbacfcda
BLAKE2b-256 7c53214408e11e3b1e3878290573641dab0d508c146519386b1166ddf4ae50fb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page