Create Tables From Folder in Databricks

Problem

You have a folder in ADLS, where all subfolders need to be a table in databricks.

Solution

def recreate_tables(container, db, subpath="", engine="delta"):
  tables = dbutils.fs.ls(f"abfss://{container}@accountname.dfs.core.windows.net/{subpath}")
  for table in tables:
    name = table.name.strip("/")
    print(f"making sure table '{name}' exists at '{table.path}'")
    spark.sql(f"create table if not exists {db}.{name} using {engine} location '{table.path}'")

Using:

recreate_table("mycontainer", "mydb", "subfolder")
Have a question⁉ Contact me.