-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathquery_async_execute.py
More file actions
32 lines (25 loc) · 934 Bytes
/
query_async_execute.py
File metadata and controls
32 lines (25 loc) · 934 Bytes
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
from databricks import sql
import os
import time
with sql.connect(
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
access_token=os.getenv("DATABRICKS_TOKEN"),
) as connection:
with connection.cursor() as cursor:
long_running_query = """
SELECT COUNT(*) FROM RANGE(10000 * 16) x
JOIN RANGE(10000) y
ON FROM_UNIXTIME(x.id * y.id, 'yyyy-MM-dd') LIKE '%not%a%date%'
"""
# Non-blocking call
cursor.execute_async(long_running_query)
# Polling every 5 seconds until the query is no longer pending
while cursor.is_query_pending():
print("POLLING")
time.sleep(5)
# Blocking call: fetch results when execution completes
cursor.get_async_execution_result()
result = cursor.fetchall()
for res in result:
print(res)