Alberto_Umana
Databricks Employee
Databricks Employee

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.")