Online Table: create only if it does not exist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-12-2025 04:54 PM
i'm following this Doc to create online table using Databricks SDK. How can i set it to create ONLY when it doesn't exist, to avoid the failure of "table already exists" error?
Or, is there another way to programatic way to check existence of an Online Table?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-12-2025 06:44 PM
Hello @lauraxyz,
There isn't a direct way to check if an online table already exists, but you can try with a try-except block to handle it:
For example:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.catalog import OnlineTable, OnlineTableSpec
from databricks.sdk.service import catalog
w = WorkspaceClient()
try:
# Attempt to get the online table
existing_table = w.online_tables.get('main.default.my_online_table')
print("Online table already exists.")
except catalog.NotFound:
# If the table doesn't exist, create it
spec = OnlineTableSpec(
primary_key_columns=["pk_col"],
source_table_full_name="main.default.source_table",
run_triggered=OnlineTableSpec.TriggeredSchedulingPolicy()
)
online_table = OnlineTable(
name="main.default.my_online_table",
spec=spec
)
w.online_tables.create_and_wait(table=online_table)
print("Online table created successfully.")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-13-2025 02:13 PM
Thank you @Alberto_Umana , that's a good way to go when there's no built-in creat-if-not-exist feature.
i also tried a different way to use information_schema, i think it should work too
def table_exists(table_name):
return spark.sql(f"""
select count(*) as count
from system.information_schema.tables
where table_catalog = '{catalog}'
and table_schema = '{schema}'
and table_name = '{online_table_name}'
""").collect()[0]['count'] != 0

