Created
May 13, 2025 05:56
-
-
Save djouallah/1a53309ec0ee3de57b6149f6c0785869 to your computer and use it in GitHub Desktop.
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
def duckdb_attach_lakehouse(workspace,LH): | |
--- needed only outside Fabric Notebook | |
duckdb.sql(f""" CREATE or replace PERSISTENT SECRET onelake (TYPE azure,PROVIDER credential_chain, CHAIN 'cli',ACCOUNT_NAME 'onelake'); """) | |
sql_schema = set() | |
sql_statements = set() | |
duckdb.sql(f""" SELECT * FROM glob ("abfss://{workspace}@onelake.dfs.fabric.microsoft.com/{LH}.Lakehouse/Tables/dbo/*") """).df()['file'].tolist() | |
list_tables = con.sql(f""" SELECT distinct(split_part(file, '_delta_log', 1)) as tables FROM glob ("abfss://{workspace}@onelake.dfs.fabric.microsoft.com/{LH}.Lakehouse/Tables/*/*/_delta_log/*.json") """).df()['tables'].tolist() | |
for table_path in list_tables: | |
parts = table_path.strip("/").split("/") | |
schema = parts[-2] | |
table = parts[-1] | |
sql_schema.add(f"CREATE SCHEMA IF NOT EXISTS {schema};") | |
sql_statements.add(f"""CREATE OR REPLACE view {schema}.{table} AS SELECT * | |
FROM delta_scan('abfss://{workspace}@onelake.dfs.fabric.microsoft.com/{LH}.Lakehouse/Tables/{schema}/{table}');""") | |
duckdb.sql(" ".join(sql_schema)) | |
duckdb.sql(" ".join(sql_statements)) | |
duckdb.sql("SHOW ALL TABLES").show(max_width=150) | |
return 'done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment