-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathquery_tags_example.py
More file actions
118 lines (97 loc) · 4 KB
/
Copy pathquery_tags_example.py
File metadata and controls
118 lines (97 loc) · 4 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import databricks.sql as sql
"""
This example demonstrates how to use Query Tags.
Query Tags are key-value pairs that can be attached to SQL executions and will appear
in the system.query.history table for analytical purposes.
There are two ways to set query tags:
1. Connection-level: Pass query_tags parameter to sql.connect() (applies to all queries in the session)
2. Per-query level: Pass query_tags parameter to execute() or execute_async() (applies to specific query)
Format: Dictionary with string keys and optional string values
Example: {"team": "engineering", "application": "etl", "priority": "high"}
Special cases:
- If a value is None, only the key is included (no colon or value)
- Special characters (comma, colon and backslash) in values are automatically escaped
- Backslashes in keys are automatically escaped; other special characters in keys are not allowed
"""
print("=== Query Tags Example ===\n")
# Example 1: Connection-level query tags
print("Example 1: Connection-level query tags")
with sql.connect(
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
access_token=os.getenv("DATABRICKS_TOKEN"),
query_tags={"team": "engineering", "application": "etl"},
) as connection:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
result = cursor.fetchone()
print(f" Result: {result[0]}")
print()
# Example 2: Per-query query tags
print("Example 2: Per-query query tags")
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:
# Query 1: Tags for a critical ETL job
cursor.execute(
"SELECT 1",
query_tags={"team": "data-eng", "application": "etl", "priority": "high"}
)
result = cursor.fetchone()
print(f" ETL Query Result: {result[0]}")
# Query 2: Tags with None value (key-only tag)
cursor.execute(
"SELECT 2",
query_tags={"team": "analytics", "experimental": None}
)
result = cursor.fetchone()
print(f" Experimental Query Result: {result[0]}")
# Query 3: Tags with special characters (automatically escaped)
cursor.execute(
"SELECT 3",
query_tags={"description": "test:with:colons,and,commas"}
)
result = cursor.fetchone()
print(f" Special Chars Query Result: {result[0]}")
# Query 4: No tags (demonstrates tags don't persist from previous queries)
cursor.execute("SELECT 4")
result = cursor.fetchone()
print(f" No Tags Query Result: {result[0]}")
print()
# Example 3: Async execution with query tags
print("Example 3: Async execution with query tags")
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:
cursor.execute_async(
"SELECT 5",
query_tags={"team": "data-eng", "mode": "async"}
)
cursor.get_async_execution_result()
result = cursor.fetchone()
print(f" Async Query Result: {result[0]}")
print()
# Example 4: executemany with query tags
print("Example 4: executemany with query tags")
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:
# Execute multiple queries with the same tags
cursor.executemany(
"SELECT ?",
[[6], [7], [8]],
query_tags={"team": "data-eng", "batch": "executemany"}
)
result = cursor.fetchone()
print(f" Executemany Query Result (last): {result[0]}")
print("\n=== Query Tags Example Complete ===")