Skip to content

Instantly share code, notes, and snippets.

@garethbrickman
Created June 24, 2024 17:12
Show Gist options
  • Save garethbrickman/d5fccc0bb378a602c9b97949e7c48c90 to your computer and use it in GitHub Desktop.
Save garethbrickman/d5fccc0bb378a602c9b97949e7c48c90 to your computer and use it in GitHub Desktop.
Databricks - Example querying using Databricks SQL Connector for Python.
from databricks import sql
import os
# Retrieve environment variables
server_hostname = os.getenv("DATABRICKS_SERVER_HOSTNAME")
http_path = os.getenv("DATABRICKS_HTTP_PATH")
access_token = os.getenv("DATABRICKS_TOKEN")
# Establish a connection to the Databricks SQL warehouse
with sql.connect(server_hostname=server_hostname, http_path=http_path, access_token=access_token) as connection:
# Create a cursor object using the connection
with connection.cursor() as cursor:
# Execute the SELECT query
cursor.execute("SELECT * FROM your_table_name LIMIT 10")
# Fetch all results from the executed query
result = cursor.fetchall()
# Print the results
for row in result:
print(row)
https://docs.databricks.com/en/dev-tools/python-sql-connector.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment