Created
June 24, 2024 17:12
-
-
Save garethbrickman/d5fccc0bb378a602c9b97949e7c48c90 to your computer and use it in GitHub Desktop.
Databricks - Example querying using Databricks SQL Connector for Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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