Package: upsolver-sdk-python
Using Upsolver with DBAPI in pythonWhat is UpsolverUpsolverenables you to use familiar SQL syntax to quickly build and deploy data pipelines, powered by a stream processing engine designed for cloud data lakes.SQLakeSQLakeis 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 APIPython's DB API 2.0 is defined inpep-249. It defines an abstract API for connecting and working with databases in Python. Many python libraries support DB API v2.0 natively, for examplepandas,SQLAlchemy, and more.Getting startedInstall Upsolver SDK for PythonTo use Upsolver SDK for Python you'll need Python interpreter of version greater than 3.7# For release version:pipinstallupsolver-sdk-python# for latest development versionpipinstallgit+https://github.com/Upsolver/upsolver-sdk-pythonRegister Upsolver accountTo register just navigate toSQL Lake Sign Up form. You'll have access to SQL workbench with examples and tutorials after completing the registration.Create API tokenAfter login navigate to "Settings" and then to "API Tokens"You will need API token and API Url to access Upsolver programatically.Then click "Generate" new token and save it for future use.Connections and cursorsConnecting to SQLake using the python SDK involves a few simple steps:create aConnectioncreate aCursorexecute query# import upsolver DB APIimportupsolver.dbapiasupsolver# Configure your token and URLtoken=...api_url=...#create connection and cursorcon=upsolver.connect(token=token,api_url=api_url)cur=upsolver.Cursor(con)# execute queryres=cur.execute('''selectcustomer.firstname,customer.lastname,nettotal as total,taxratefrom default_glue_catalog.database_8edc49.orders_raw_datalimit 5;''')# now we can iterate the resultsforrinres: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:frombeautifultableimportBeautifulTableres=cur.execute('''selectcustomer.firstname,customer.lastname,nettotal as total,taxratefrom default_glue_catalog.database_8edc49.orders_raw_datalimit 5;''')table=BeautifulTable()table.column_headers=[c[0]forcincur.description]forrinres: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 SQLakeWe can use pandas toopandasis very popular library for data maipulations.
It's possible to rewrite the above example with pandasimportpandasaspddf=pd.read_sql(query,con=con)df.info()<class'pandas.core.frame.DataFrame'>RangeIndex:5entries,0to4Datacolumns(total4columns):#   Column     Non-Null Count  Dtype----------------------------0firstname5non-nullobject1lastname5non-nullobject2total5non-nullobject3taxrate5non-nullobjectdtypes:object(4)Upsolver SQLSee Upsolver'sSQL Command Referencefor the supported SQL commands and syntax.Further readingupsolver.comDocumentationof Upsolver SQLupsolver-sdk-python- GitHub repository with upsolver SDK for Python languageSQLake workbenchmain pagePython examples from this README