<?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: Create Databricks tables dynamically in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23920#M16597</link>
    <description>&lt;P&gt;The end solution looked like this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;first compute the variable part of the location (one month back from today in my case)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;#first compute the archive date (or whatever else you need to be dynamic)
&amp;nbsp;
import datetime
&amp;nbsp;
from dateutil.relativedelta import relativedelta
&amp;nbsp;
start_date = datetime.date.today()
&amp;nbsp;
archive_date = start_date - relativedelta(months=1)
&amp;nbsp;
archive_date = archive_date.strftime('%Y-%m')
&amp;nbsp;
#build the table using the previously computed variable 
&amp;nbsp;
import pyspark.sql.session
&amp;nbsp;
folder = 'container/folder'+archive_date+'.parquet'
&amp;nbsp;
spark.sql(f"CREATE TABLE Archive using parquet location '/mnt/{folder}'")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;couldn't get the SQL approach to work for some reason, but this is absolutely fine for what I need.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks for your help, this will prove v useful in the future.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an observation might it be worth updating Databricks documentation as it currently states location must be a string literal? Just a thought.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Nov 2022 11:56:09 GMT</pubDate>
    <dc:creator>labromb</dc:creator>
    <dc:date>2022-11-07T11:56:09Z</dc:date>
    <item>
      <title>Create Databricks tables dynamically</title>
      <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23914#M16591</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to be able to do something like this...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;create table if not exists table1&lt;/P&gt;&lt;P&gt;using parquet&lt;/P&gt;&lt;P&gt;location = '/mnt/somelocation&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;some location needs to be a concatenation of static and code generated string. Documentation suggests that location only accepts a string literal and based on my attempts to do this, seems to be the case.  Is there any way round this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 15:17:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23914#M16591</guid>
      <dc:creator>labromb</dc:creator>
      <dc:date>2022-11-03T15:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create Databricks tables dynamically</title>
      <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23915#M16592</link>
      <description>&lt;P&gt;Hi @Brian Labrom​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;One you can do it with Spark.sql&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%python
tables = ["table1", "table2"]
for table in tables:
  spark.sql(f"""
  create table if not exists {table}
  using parquet
  location = '/mnt/{table}'""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or maybe with sql:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;create table if not exists table1
using parquet
location = '/mnt/${somelocation}'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use widget for example to get somelocation:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/notebooks/widgets.html" alt="https://docs.databricks.com/notebooks/widgets.html" target="_blank"&gt;https://docs.databricks.com/notebooks/widgets.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 21:09:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23915#M16592</guid>
      <dc:creator>Pat</dc:creator>
      <dc:date>2022-11-03T21:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create Databricks tables dynamically</title>
      <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23916#M16593</link>
      <description>&lt;P&gt;FString Python can be used. example  &amp;gt; spark.sql(f"CREATE TABLE {table_name} (id INT, name STRING, value DOUBLE, state STRING)")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2022 02:03:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23916#M16593</guid>
      <dc:creator>PrasanthM</dc:creator>
      <dc:date>2022-11-04T02:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create Databricks tables dynamically</title>
      <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23918#M16595</link>
      <description>&lt;P&gt;Hi, I will check the suggestions this morning and share the results. Thanks @Pat Sienkiewicz​&amp;nbsp;and @Prasanth Mathesh​&amp;nbsp;for the suggestions&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2022 09:53:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23918#M16595</guid>
      <dc:creator>labromb</dc:creator>
      <dc:date>2022-11-04T09:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create Databricks tables dynamically</title>
      <link>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23920#M16597</link>
      <description>&lt;P&gt;The end solution looked like this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;first compute the variable part of the location (one month back from today in my case)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;#first compute the archive date (or whatever else you need to be dynamic)
&amp;nbsp;
import datetime
&amp;nbsp;
from dateutil.relativedelta import relativedelta
&amp;nbsp;
start_date = datetime.date.today()
&amp;nbsp;
archive_date = start_date - relativedelta(months=1)
&amp;nbsp;
archive_date = archive_date.strftime('%Y-%m')
&amp;nbsp;
#build the table using the previously computed variable 
&amp;nbsp;
import pyspark.sql.session
&amp;nbsp;
folder = 'container/folder'+archive_date+'.parquet'
&amp;nbsp;
spark.sql(f"CREATE TABLE Archive using parquet location '/mnt/{folder}'")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;couldn't get the SQL approach to work for some reason, but this is absolutely fine for what I need.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks for your help, this will prove v useful in the future.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an observation might it be worth updating Databricks documentation as it currently states location must be a string literal? Just a thought.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2022 11:56:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-databricks-tables-dynamically/m-p/23920#M16597</guid>
      <dc:creator>labromb</dc:creator>
      <dc:date>2022-11-07T11:56:09Z</dc:date>
    </item>
  </channel>
</rss>

