-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforeign_table_refresh.py
More file actions
55 lines (39 loc) · 1.81 KB
/
Copy pathforeign_table_refresh.py
File metadata and controls
55 lines (39 loc) · 1.81 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
# Databricks notebook source
dbutils.widgets.text("catalog", "your_catalog")
catalog = dbutils.widgets.get("catalog")
# COMMAND ----------
import concurrent.futures
dbutils.widgets.text("catalog", "your_catalog")
catalog = dbutils.widgets.get("catalog")
def get_foreign_tables(catalog):
"""
Return a list of tuples (schema_name, table_name) for all foreign tables in a catalog.
Queries system.information_schema.tables to filter by table type = 'FOREIGN'.
"""
schemas = [schema_name.databaseName for schema_name in spark.sql(f"SHOW SCHEMAS IN {catalog}").collect() if schema_name.databaseName not in ('sys', 'information_schema')]
tables = []
for schema in schemas:
tables.extend([f"{catalog}.{schema}.{table['tableName']}" for table in spark.sql(f"SHOW TABLES IN {catalog}.{schema}").collect()])
return tables
def refresh_table(fqn):
"""Refresh a single foreign table with error handling."""
full_table_name = fqn
try:
spark.sql(f"REFRESH FOREIGN TABLE {full_table_name}")
print(f"[SUCCESS] Refreshed {full_table_name}")
except Exception as e:
print(f"[FAILED] Could not refresh {full_table_name}: {e}")
def main(catalog, max_workers=8):
tables = get_foreign_tables(catalog)
if not tables:
print(f"No foreign tables found in catalog {catalog}")
return
print(f"Refreshing {len(tables)} foreign tables in catalog {catalog}...")
# Run refresh in parallel
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(refresh_table, t) for t in tables]
concurrent.futures.wait(futures)
print("Refresh process complete.")
if __name__ == "__main__":
catalog_name = catalog # Replace with your catalog
main(catalog_name)