Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Created August 31, 2021 21:29
Show Gist options
  • Save JerryNixon/12f6d367d3799714a49128c02d135486 to your computer and use it in GitHub Desktop.
Save JerryNixon/12f6d367d3799714a49128c02d135486 to your computer and use it in GitHub Desktop.
Databricks reusable routine to create External tables
class Data(dict):
__getattr__, __setattr__ = dict.get, dict.__setitem__
tables = [
Data(
path = "abfss://name@acct/path1",
database = "my_db",
table = "table1",
),
Data(
path = "abfss://name@acct/path2",
database = "my_db",
table = "table2",
),
Data(
path = "abfss://name@acct/path3",
database = "my_db",
table = "table3",
),
]
for item in tables:
table = f"{item.database}.{item.table}"
print(f"Table = {table}"")
spark.sql(f"DROP TABLE IF EXISTS {table}")
spark.sql(f"CREATE DATABASE IF NOT EXISTS {item.database}")
spark.catalog.createExternalTable(table, path=item.path, source="delta")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment