<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Online Table: create only if it does not exist in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110064#M43475</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/133340"&gt;@lauraxyz&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;from databricks.sdk import WorkspaceClient&lt;BR /&gt;from databricks.sdk.service.catalog import OnlineTable, OnlineTableSpec&lt;BR /&gt;from databricks.sdk.service import catalog&lt;/P&gt;
&lt;P&gt;w = WorkspaceClient()&lt;/P&gt;
&lt;P&gt;try:&lt;BR /&gt;# Attempt to get the online table&lt;BR /&gt;existing_table = w.online_tables.get('main.default.my_online_table')&lt;BR /&gt;print("Online table already exists.")&lt;BR /&gt;except catalog.NotFound:&lt;BR /&gt;# If the table doesn't exist, create it&lt;BR /&gt;spec = OnlineTableSpec(&lt;BR /&gt;primary_key_columns=["pk_col"],&lt;BR /&gt;source_table_full_name="main.default.source_table",&lt;BR /&gt;run_triggered=OnlineTableSpec.TriggeredSchedulingPolicy()&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;online_table = OnlineTable(&lt;BR /&gt;name="main.default.my_online_table",&lt;BR /&gt;spec=spec&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;w.online_tables.create_and_wait(table=online_table)&lt;BR /&gt;print("Online table created successfully.")&lt;/P&gt;</description>
    <pubDate>Thu, 13 Feb 2025 02:44:15 GMT</pubDate>
    <dc:creator>Alberto_Umana</dc:creator>
    <dc:date>2025-02-13T02:44:15Z</dc:date>
    <item>
      <title>Online Table: create only if it does not exist</title>
      <link>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110063#M43474</link>
      <description>&lt;P&gt;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?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or, is there another way to programatic way to check existence of an Online Table?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 00:54:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110063#M43474</guid>
      <dc:creator>lauraxyz</dc:creator>
      <dc:date>2025-02-13T00:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: Online Table: create only if it does not exist</title>
      <link>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110064#M43475</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/133340"&gt;@lauraxyz&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;from databricks.sdk import WorkspaceClient&lt;BR /&gt;from databricks.sdk.service.catalog import OnlineTable, OnlineTableSpec&lt;BR /&gt;from databricks.sdk.service import catalog&lt;/P&gt;
&lt;P&gt;w = WorkspaceClient()&lt;/P&gt;
&lt;P&gt;try:&lt;BR /&gt;# Attempt to get the online table&lt;BR /&gt;existing_table = w.online_tables.get('main.default.my_online_table')&lt;BR /&gt;print("Online table already exists.")&lt;BR /&gt;except catalog.NotFound:&lt;BR /&gt;# If the table doesn't exist, create it&lt;BR /&gt;spec = OnlineTableSpec(&lt;BR /&gt;primary_key_columns=["pk_col"],&lt;BR /&gt;source_table_full_name="main.default.source_table",&lt;BR /&gt;run_triggered=OnlineTableSpec.TriggeredSchedulingPolicy()&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;online_table = OnlineTable(&lt;BR /&gt;name="main.default.my_online_table",&lt;BR /&gt;spec=spec&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;w.online_tables.create_and_wait(table=online_table)&lt;BR /&gt;print("Online table created successfully.")&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 02:44:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110064#M43475</guid>
      <dc:creator>Alberto_Umana</dc:creator>
      <dc:date>2025-02-13T02:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: Online Table: create only if it does not exist</title>
      <link>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110153#M43501</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/106294"&gt;@Alberto_Umana&lt;/a&gt;&amp;nbsp;,&amp;nbsp; that's a good way to go when there's no built-in creat-if-not-exist feature.&lt;/P&gt;&lt;P&gt;i also tried a different way to use&amp;nbsp; information_schema, i think it should work too&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 22:13:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/online-table-create-only-if-it-does-not-exist/m-p/110153#M43501</guid>
      <dc:creator>lauraxyz</dc:creator>
      <dc:date>2025-02-13T22:13:46Z</dc:date>
    </item>
  </channel>
</rss>

