We provide example scripts so you can see the connector in action for basic usage. You need a Databricks account to run them. The scripts expect to find your Databricks account credentials in these environment variables:
- DATABRICKS_SERVER_HOSTNAME
- DATABRICKS_HTTP_PATH
- DATABRICKS_TOKEN
Follow the quick start in our README to install databricks-sql-connector and see
how to find the hostname, http path, and access token. Note that for the OAuth examples below a
personal access token is not needed.
To run all of these examples you can clone the entire repository to your disk. Or you can use curl to fetch an individual script.
- Clone this repository to your local system
- Follow the quick start in the README to install the connector and obtain authentication credentials.
cd examples/- Then run any script using the
pythonCLI. For examplepython query_execute.py
- Follow the quick start in the README to install the connector and obtain authentication credentials.
- Use the GitHub UI to find the URL to the Raw version of one of these examples. For example:
https://raw.githubusercontent.com/databricks/databricks-sql-python/main/examples/query_execute.py curlthis URL to your local file-system:curl https://raw.githubusercontent.com/databricks/databricks-sql-python/main/examples/query_execute.py > query_execute.py- Then run the script with the
pythonCLI.python query_execute.py
query_execute.pyconnects to thesamplesdatabase of your default catalog, runs a small query, and prints the result to screen.insert_data.pyadds a tables calledsquaresto your default catalog and inserts one hundred rows of example data. Then it fetches this data and prints it to the screen.transactions.pydemonstrates multi-statement transaction support with explicit commit/rollback control. Shows how to group multiple SQL statements into an atomic unit that either succeeds completely or fails completely.query_cancel.pyshows how to cancel a query assuming that you can access theCursorexecuting that query from a different thread. This is necessary becausedatabricks-sql-connectordoes not yet implement an asynchronous API; calling.execute()blocks the current thread until execution completes. Therefore, the connector can't cancel queries from the same thread where they began.interactive_oauth.pyshows the simplest example of authenticating by OAuth (no need for a PAT generated in the DBSQL UI) while Bring Your Own IDP is in public preview. When you run the script it will open a browser window so you can authenticate. Afterward, the script fetches some sample data from Databricks and prints it to the screen. For this script, the OAuth token is not persisted which means you need to authenticate every time you run the script.m2m_oauth.pyshows the simplest example of authenticating by using OAuth M2M (machine-to-machine) for service principal.persistent_oauth.pyshows a more advanced example of authenticating by OAuth while Bring Your Own IDP is in public preview. In this case, it shows how to use a sublcass ofOAuthPersistenceto reuse an OAuth token across script executions.set_user_agent.pyshows how to customize the user agent header used for Thrift commands. In this example the stringExamplePartnerTagwill be added to the the user agent on every request.staging_ingestion.pyshows how the connector handles Databricks' experimental staging ingestion commandsGET,PUT, andREMOVE.sqlalchemy.pyshows a basic example of connecting to Databricks with SQLAlchemy 2.0.custom_cred_provider.pyshows how to pass a custom credential provider to bypass connector authentication. Please install databricks-sdk prior to running this example.v3_retries_query_execute.pyshows how to enable v3 retries in connector version 2.9.x including how to enable retries for non-default retry cases.parameters.pyshows how to use parameters in native and inline modes.proxy_authentication.pydemonstrates how to connect through proxy servers using different authentication methods including basic authentication and Kerberos/Negotiate authentication.