Created
August 31, 2021 21:29
-
-
Save JerryNixon/12f6d367d3799714a49128c02d135486 to your computer and use it in GitHub Desktop.
Databricks reusable routine to create External tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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