-
Notifications
You must be signed in to change notification settings - Fork 16
/
dbsql-with-aad-token-spn.py
41 lines (32 loc) · 1.41 KB
/
dbsql-with-aad-token-spn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from databricks import sql
from databricks.sdk.core import azure_service_principal, Config
import os
def get_env_or_raise(name: str) -> str:
value = os.environ[name]
if not value:
raise Exception(f"Environment variable {name} isn't set or empty")
return value
# Variables to fill
# Host name without https://
host_name="adb-xxx.17.azuredatabricks.net"
# Path obtained as per instructions https://docs.databricks.com/dev-tools/python-sql-connector.html#get-started
http_path="/sql/1.0/warehouses/xxx"
# Your query to execute
query = "select 42, current_timestamp(), current_catalog(), current_database(), current_user()"
# Explicit initialization of the authenticator
tenant_id = get_env_or_raise("ARM_TENANT_ID")
client_id = get_env_or_raise("ARM_CLIENT_ID")
client_secret = get_env_or_raise("ARM_CLIENT_SECRET")
creds = azure_service_principal(Config(azure_client_secret=client_secret, azure_client_id=client_id,
azure_tenant_id=tenant_id, host=f"https://{host_name}"))
# Implicit initialization - you need to specify corresponding environment variables
# creds = azure_service_principal(Config())
with sql.connect(server_hostname=host_name, http_path=http_path,
credentials_provider=lambda: creds) as connection:
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
for row in result:
print(row)
cursor.close()
connection.close()